Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🩹 fix: Embedded struct binding #3203

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,39 @@ func Benchmark_Bind_Body_XML(b *testing.B) {
require.Equal(b, "john", d.Name)
}

// go test -run Test_Bind_Body_Form_Embedded
func Test_Bind_Body_Form_Embedded(b *testing.T) {
var err error

app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
type EmbededDemo struct {
EmbededStrings []string
EmbededString string
}

type Demo struct {
SomeString string
SomeOtherString string
Strings []string
EmbededDemo
}
gaby marked this conversation as resolved.
Show resolved Hide resolved
body := []byte("SomeString=john%2Clong&SomeOtherString=long%2Cjohn&Strings=long%2Cjohn&EmbededStrings=john%2Clong&EmbededString=johny%2Cwalker")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix form parameter names to match struct tags.

The request body parameters don't match the struct field tags, which could lead to binding failures.

Apply this diff to fix the parameter names:

-body := []byte("SomeString=john%2Clong&SomeOtherString=long%2Cjohn&Strings=long%2Cjohn&EmbededStrings=john%2Clong&EmbededString=johny%2Cwalker")
+body := []byte("some_string=john%2Clong&some_other_string=long%2Cjohn&strings=long%2Cjohn&embedded_strings=john%2Clong&embedded_string=johny%2Cwalker")

Committable suggestion skipped: line range outside the PR's diff.

c.Request().SetBody(body)
c.Request().Header.SetContentType(MIMEApplicationForm)
c.Request().Header.SetContentLength(len(body))
d := new(Demo)

err = c.Bind().Body(d)

require.NoError(b, err)
require.Equal(b, []string{"long", "john"}, d.Strings)
require.Equal(b, []string{"john", "long"}, d.EmbededStrings)
require.Equal(b, "johny,walker", d.EmbededString)
require.Equal(b, "john,long", d.SomeString)
require.Equal(b, "long,john", d.SomeOtherString)
}

// go test -v -run=^$ -bench=Benchmark_Bind_Body_Form -benchmem -count=4
func Benchmark_Bind_Body_Form(b *testing.B) {
var err error
Expand Down
8 changes: 7 additions & 1 deletion binder/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,15 @@ func equalFieldType(out any, kind reflect.Kind, key string) bool {
// Does the field type equals input?
if structFieldKind != kind {
// Is the field an embedded struct?
if structFieldKind == reflect.Struct {
if structFieldKind == reflect.Struct && typeField.Anonymous {
// Loop over embedded struct fields
for j := 0; j < structField.NumField(); j++ {
fNm := utils.ToLower(structField.Type().Field(j).Name)
if fNm != key {
//this is not the field that we are looking for
continue
}

structFieldField := structField.Field(j)

// Can this embedded field be changed?
Expand Down