From bb72dc27ac3970e51d38ee52fc3dc1465ae25b9d Mon Sep 17 00:00:00 2001 From: Deng Ming Date: Tue, 19 Nov 2024 23:28:37 +0800 Subject: [PATCH] fix 5720: the formValue should read the first value --- server/web/context/form.go | 15 +----- server/web/context/form_test.go | 88 +++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 server/web/context/form_test.go diff --git a/server/web/context/form.go b/server/web/context/form.go index 07a1b0b2..caa58af3 100644 --- a/server/web/context/form.go +++ b/server/web/context/form.go @@ -170,20 +170,9 @@ func formTagName(fieldT reflect.StructField) (string, bool) { func formValue(tag string, form url.Values, fieldT reflect.StructField) (string, bool) { formValues := form[tag] - var value string if len(formValues) == 0 { defaultValue := fieldT.Tag.Get("default") - if defaultValue != "" { - value = defaultValue - } else { - return "", false - } + return defaultValue, defaultValue != "" } - if len(formValues) == 1 { - value = formValues[0] - if value == "" { - return "", false - } - } - return value, true + return formValues[0], true } diff --git a/server/web/context/form_test.go b/server/web/context/form_test.go new file mode 100644 index 00000000..4ebdac1a --- /dev/null +++ b/server/web/context/form_test.go @@ -0,0 +1,88 @@ +// Copyright 2024 beego +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package context + +import ( + "net/url" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFormValue(t *testing.T) { + typ := reflect.TypeOf(TestStruct{}) + defField, _ := typ.FieldByName("DefaultField") + noDefField, _ := typ.FieldByName("NoDefaultField") + testCases := []struct { + name string + tag string + + form url.Values + field reflect.StructField + + wantRes string + wantOk bool + }{ + { + name: "use value", + tag: "defaultField", + field: defField, + form: map[string][]string{ + "defaultField": {"abc", "bcd"}, + }, + wantRes: "abc", + wantOk: true, + }, + { + name: "empty value", + tag: "defaultField", + field: defField, + form: map[string][]string{ + "defaultField": {"", "bcd"}, + }, + wantRes: "", + wantOk: true, + }, + { + name: "use default value", + tag: "defaultField", + field: defField, + form: map[string][]string{}, + wantRes: "Tom", + wantOk: true, + }, + { + name: "no value", + tag: "no", + field: noDefField, + form: map[string][]string{}, + wantOk: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + val, ok := formValue(tc.tag, tc.form, tc.field) + assert.Equal(t, tc.wantRes, val) + assert.Equal(t, tc.wantOk, ok) + }) + } +} + +type TestStruct struct { + DefaultField string `form:"defaultField" default:"Tom"` + NoDefaultField string `form:"noDefaultField"` +}