From 16d623ed1a62ef41873e1916e3d38784c041e3b6 Mon Sep 17 00:00:00 2001 From: Wes Date: Thu, 15 Feb 2024 17:01:49 -0700 Subject: [PATCH] fix: only extract the field_name from `json:"field_name"` (#945) --- go-runtime/compile/schema.go | 10 +++++++++- integration/testdata/go/httpingress/echo.go | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/go-runtime/compile/schema.go b/go-runtime/compile/schema.go index c982789afd..6c88f8f896 100644 --- a/go-runtime/compile/schema.go +++ b/go-runtime/compile/schema.go @@ -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) diff --git a/integration/testdata/go/httpingress/echo.go b/integration/testdata/go/httpingress/echo.go index 6650dbafeb..96d028f3c9 100644 --- a/integration/testdata/go/httpingress/echo.go +++ b/integration/testdata/go/httpingress/echo.go @@ -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 {