diff --git a/net/goai/goai_parameter_ref.go b/net/goai/goai_parameter_ref.go index 2abdc1c10..0388f4aad 100644 --- a/net/goai/goai_parameter_ref.go +++ b/net/goai/goai_parameter_ref.go @@ -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), diff --git a/net/goai/goai_shema.go b/net/goai/goai_shema.go index 41466929d..62532e287 100644 --- a/net/goai/goai_shema.go +++ b/net/goai/goai_shema.go @@ -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(), diff --git a/net/goai/goai_z_unit_test.go b/net/goai/goai_z_unit_test.go index 51716fd62..4f52c7c25 100644 --- a/net/goai/goai_z_unit_test.go +++ b/net/goai/goai_z_unit_test.go @@ -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) + }) +}