Skip to content

Commit

Permalink
fix: only extract the field_name from json:"field_name" (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman authored Feb 16, 2024
1 parent fc305a3 commit 16d623e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
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

0 comments on commit 16d623e

Please sign in to comment.