From 26e3c7aeb8a78de906f5434586105fe71334be8f Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 18 Aug 2022 21:05:58 +0800 Subject: [PATCH] fix issue 1914 (#2075) * CI updates * fix issue #1914 --- net/goai/goai_shema.go | 7 +++++-- net/goai/goai_z_unit_test.go | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/net/goai/goai_shema.go b/net/goai/goai_shema.go index 3a372f3a7..cb3b717d9 100644 --- a/net/goai/goai_shema.go +++ b/net/goai/goai_shema.go @@ -174,8 +174,11 @@ func (oai *OpenApiV3) structToSchema(object interface{}) (*Schema, error) { continue } var fieldName = structField.Name() - if jsonName := structField.TagJsonName(); jsonName != "" { - fieldName = jsonName + for _, tagName := range gconv.StructTagPriority { + if tagValue := structField.Tag(tagName); tagValue != "" { + fieldName = tagValue + break + } } schemaRef, err := oai.newSchemaRefWithGolangType( structField.Type().Type, diff --git a/net/goai/goai_z_unit_test.go b/net/goai/goai_z_unit_test.go index a13e9c49c..90589215d 100644 --- a/net/goai/goai_z_unit_test.go +++ b/net/goai/goai_z_unit_test.go @@ -936,3 +936,40 @@ func Test_EnumOfSchemaItems(t *testing.T) { ) }) } + +func Test_AliasNameOfAtrribute(t *testing.T) { + type CreateResourceReq struct { + gmeta.Meta `path:"/CreateResourceReq" method:"POST"` + Name string `p:"n"` + Age string `json:"a"` + } + + gtest.C(t, func(t *gtest.T) { + var ( + err error + oai = goai.New() + req = new(CreateResourceReq) + ) + err = oai.Add(goai.AddInput{ + Object: req, + }) + t.AssertNil(err) + + t.Assert( + oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value. + Properties.Get(`Name`), nil, + ) + t.Assert( + oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value. + Properties.Get(`Age`), nil, + ) + t.AssertNE( + oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value. + Properties.Get(`n`), nil, + ) + t.AssertNE( + oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value. + Properties.Get(`a`), nil, + ) + }) +}