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: only extract the field_name from json:"field_name" #945

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion go-runtime/compile/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,19 @@ func visitStruct(pctx *parseContext, node ast.Node, tnode types.Type) (*schema.D
return nil, fmt.Errorf("field %s: %w", f.Name(), err)
}

// Extract the JSON tag and split it to get just the field name
tagContent := reflect.StructTag(s.Tag(i)).Get(aliasFieldTag)
tagParts := strings.Split(tagContent, ",")
jsonFieldName := ""
if len(tagParts) > 0 {
jsonFieldName = tagParts[0]
}

out.Fields = append(out.Fields, &schema.Field{
Pos: goPosToSchemaPos(node.Pos()),
Name: strcase.ToLowerCamel(f.Name()),
Type: ft,
JSONAlias: reflect.StructTag(s.Tag(i)).Get(aliasFieldTag),
JSONAlias: jsonFieldName,
})
}
pctx.module.AddData(out)
Expand Down
4 changes: 2 additions & 2 deletions integration/testdata/go/httpingress/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

type GetRequest struct {
UserID string `json:"userId"`
PostID string `json:"postId"`
UserID string `json:"userId,omitempty"`
PostID string `json:"postId,something,else"`
}

type Nested struct {
Expand Down