improve enum handling for array property of goai schema

This commit is contained in:
John Guo
2022-05-17 11:15:29 +08:00
parent 84e75129a5
commit c4c3620c5f
3 changed files with 34 additions and 0 deletions

View File

@ -157,6 +157,10 @@ func (oai *OpenApiV3) structToSchema(object interface{}) (*Schema, error) {
return nil, err
}
schema.Items = subSchemaRef
if len(schema.Enum) > 0 {
schema.Items.Value.Enum = schema.Enum
schema.Enum = nil
}
return schema, nil
}
// struct.

View File

@ -51,6 +51,10 @@ func (oai *OpenApiV3) newSchemaRefWithGolangType(golangType reflect.Type, tagMap
return nil, err
}
schema.Items = subSchemaRef
if len(schema.Enum) > 0 {
schema.Items.Value.Enum = schema.Enum
schema.Enum = nil
}
case
TypeObject:

View File

@ -875,3 +875,29 @@ func TestOpenApiV3_Ignore_Parameter(t *testing.T) {
t.Assert(len(oai.Paths["/test"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 8)
})
}
func Test_EnumOfSchemaItems(t *testing.T) {
type CreateResourceReq struct {
gmeta.Meta `path:"/CreateResourceReq" method:"POST"`
Members []string `v:"required|in:a,b,c"`
}
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.protocol.goai_test.CreateResourceReq`).Value.
Properties.Get(`Members`).Value.
Items.Value.Enum,
g.Slice{"a", "b", "c"},
)
})
}