fix issue in empty json name along with omitempty tag in package oai (#2500)

This commit is contained in:
John Guo
2023-03-07 14:17:14 +08:00
committed by GitHub
parent d0d41a63a6
commit e8051bad9a
3 changed files with 32 additions and 2 deletions

View File

@ -38,7 +38,10 @@ func (oai *OpenApiV3) newParameterRefWithStructMethod(field gstructs.Field, path
break
}
}
fieldName = gstr.SplitAndTrim(fieldName, ",")[0]
fieldName = gstr.Split(gstr.Trim(fieldName), ",")[0]
if fieldName == "" {
fieldName = field.Name()
}
var parameter = &Parameter{
Name: fieldName,
XExtensions: make(XExtensions),

View File

@ -180,7 +180,10 @@ func (oai *OpenApiV3) structToSchema(object interface{}) (*Schema, error) {
break
}
}
fieldName = gstr.SplitAndTrim(fieldName, ",")[0]
fieldName = gstr.Split(gstr.Trim(fieldName), ",")[0]
if fieldName == "" {
fieldName = structField.Name()
}
schemaRef, err := oai.newSchemaRefWithGolangType(
structField.Type().Type,
structField.TagMap(),

View File

@ -1099,3 +1099,27 @@ func TestOpenApiV3_PathSecurity(t *testing.T) {
t.Assert(len(oai.Paths["/index"].Put.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 3)
})
}
func Test_EmptyJsonNameWithOmitEmpty(t *testing.T) {
type CreateResourceReq struct {
gmeta.Meta `path:"/CreateResourceReq" method:"POST" tags:"default"`
Name string `description:"实例名称" json:",omitempty"`
Product string `description:"业务类型" json:",omitempty"`
}
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)
var reqKey = "github.com.gogf.gf.v2.net.goai_test.CreateResourceReq"
t.AssertNE(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Name"), nil)
t.AssertNE(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Product"), nil)
t.Assert(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("None"), nil)
})
}