fix(net/goai): cannot customize OpenAPIv3 type for request parameters (#3845)

This commit is contained in:
John Guo
2024-10-09 13:15:46 +08:00
committed by GitHub
parent ae3ae8b177
commit 4d29939f87
7 changed files with 217 additions and 98 deletions

View File

@ -57,6 +57,7 @@ func (oai *OpenApiV3) newSchemaRefWithGolangType(golangType reflect.Type, tagMap
case reflect.Ptr, reflect.Array, reflect.Slice:
pkgPath = golangType.Elem().PkgPath()
typeName = golangType.Elem().Name()
default:
}
}
@ -70,7 +71,7 @@ func (oai *OpenApiV3) newSchemaRefWithGolangType(golangType reflect.Type, tagMap
}
if len(tagMap) > 0 {
if err := oai.tagMapToSchema(tagMap, schema); err != nil {
if err = oai.tagMapToSchema(tagMap, schema); err != nil {
return nil, err
}
if oaiType == TypeArray && schema.Type == TypeFile {
@ -78,7 +79,7 @@ func (oai *OpenApiV3) newSchemaRefWithGolangType(golangType reflect.Type, tagMap
}
}
schemaRef.Value = schema
switch oaiType {
switch schema.Type {
case TypeString, TypeFile:
// Nothing to do.
case TypeInteger:
@ -141,11 +142,9 @@ func (oai *OpenApiV3) newSchemaRefWithGolangType(golangType reflect.Type, tagMap
case reflect.Interface:
// Specially for interface type.
var (
structTypeName = oai.golangTypeToSchemaName(golangType)
)
var structTypeName = oai.golangTypeToSchemaName(golangType)
if oai.Components.Schemas.Get(structTypeName) == nil {
if err := oai.addSchema(reflect.New(golangType).Interface()); err != nil {
if err = oai.addSchema(reflect.New(golangType).Interface()); err != nil {
return nil, err
}
}
@ -164,12 +163,12 @@ func (oai *OpenApiV3) newSchemaRefWithGolangType(golangType reflect.Type, tagMap
} else {
var structTypeName = oai.golangTypeToSchemaName(golangType)
if oai.Components.Schemas.Get(structTypeName) == nil {
if err := oai.addSchema(golangTypeInstance); err != nil {
if err = oai.addSchema(golangTypeInstance); err != nil {
return nil, err
}
}
schemaRef.Ref = structTypeName
schemaRef.Value = nil
schemaRef.Value = schema
}
}
}

View File

@ -16,6 +16,7 @@ import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
)
var ctx = context.Background()
@ -39,7 +40,9 @@ func (Issue3664) Default(ctx context.Context, req *Issue3664DefaultReq) (res *Is
return
}
func (Issue3664) RequiredTag(ctx context.Context, req *Issue3664RequiredTagReq) (res *Issue3664RequiredTagRes, err error) {
func (Issue3664) RequiredTag(
ctx context.Context, req *Issue3664RequiredTagReq,
) (res *Issue3664RequiredTagRes, err error) {
res = &Issue3664RequiredTagRes{}
return
}
@ -47,7 +50,7 @@ func (Issue3664) RequiredTag(ctx context.Context, req *Issue3664RequiredTagReq)
// https://github.com/gogf/gf/issues/3664
func Test_Issue3664(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server()
s := g.Server(guid.S())
s.Use(ghttp.MiddlewareHandlerResponse)
s.Group("/", func(group *ghttp.RouterGroup) {
group.Bind(
@ -70,3 +73,47 @@ func Test_Issue3664(t *testing.T) {
t.Assert(j.Get(`paths./required-tag.post.requestBody.required`).String(), "true")
})
}
type Issue3135DefaultReq struct {
g.Meta `path:"/demo/colors" method:"POST" summary:"颜色 - 保存" tags:"颜色管理" description:"颜色 - 保存"`
ID uint64 `json:"id,string" dc:"ID" v:"id-zero"`
Color string `json:"color" dc:"颜色值16进制表示法" v:"required|max-length:10"`
Rgba *gjson.Json `json:"rgba" dc:"颜色值RGBA表示法" v:"required|json" type:"string"`
}
type Issue3135DefaultRes struct{}
type Issue3135 struct{}
func (Issue3135) Default(ctx context.Context, req *Issue3135DefaultReq) (res *Issue3135DefaultRes, err error) {
res = &Issue3135DefaultRes{}
return
}
// https://github.com/gogf/gf/issues/3135
func Test_Issue3135(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server(guid.S())
s.Use(ghttp.MiddlewareHandlerResponse)
s.Group("/", func(group *ghttp.RouterGroup) {
group.Bind(
new(Issue3135),
)
})
s.SetLogger(nil)
s.SetOpenApiPath("/api.json")
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
var (
api = s.GetOpenApi()
reqPath = "github.com.gogf.gf.v2.net.goai_test.Issue3135DefaultReq"
rgbType = api.Components.Schemas.Get(reqPath).Value.Properties.Get("rgba").Value.Type
requiredArray = api.Components.Schemas.Get(reqPath).Value.Required
)
t.Assert(rgbType, "string")
t.AssertIN("rgba", requiredArray)
})
}

View File

@ -54,16 +54,21 @@ func Test_Basic(t *testing.T) {
t.AssertNil(err)
// Schema asserts.
t.Assert(len(oai.Components.Schemas.Map()), 2)
t.Assert(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.Type, goai.TypeObject)
t.Assert(len(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.Properties.Map()), 7)
t.Assert(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.Properties.Get(`appId`).Value.Type, goai.TypeInteger)
t.Assert(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.Properties.Get(`resourceId`).Value.Type, goai.TypeString)
var (
schemaCreate = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`)
schemaSetSpecInfo = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.SetSpecInfo`)
)
t.Assert(len(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.SetSpecInfo`).Value.Properties.Map()), 3)
t.Assert(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.SetSpecInfo`).Value.Properties.Get(`Params`).Value.Type, goai.TypeArray)
t.Assert(schemaCreate.Value.Type, goai.TypeObject)
t.Assert(len(schemaCreate.Value.Properties.Map()), 7)
t.Assert(schemaCreate.Value.Properties.Get(`appId`).Value.Type, goai.TypeInteger)
t.Assert(schemaCreate.Value.Properties.Get(`resourceId`).Value.Type, goai.TypeString)
t.Assert(len(schemaSetSpecInfo.Value.Properties.Map()), 3)
t.Assert(schemaSetSpecInfo.Value.Properties.Get(`Params`).Value.Type, goai.TypeArray)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.Required, []string{"appId", "Region", "SetMap", "SetSlice"})
t.AssertEQ(schemaCreate.Value.Required, []string{"appId", "Region", "SetMap", "SetSlice"})
})
}
@ -117,20 +122,26 @@ func TestOpenApiV3_Add(t *testing.T) {
// fmt.Println(oai.String())
// Schema asserts.
t.Assert(len(oai.Components.Schemas.Map()), 3)
t.Assert(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.Type, goai.TypeObject)
t.Assert(len(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.Properties.Map()), 7)
t.Assert(len(oai.Paths["/test1/{appId}"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 5)
t.Assert(len(oai.Paths["/test1/{appId}"].Post.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 5)
var (
schemaCreate = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`)
schemaSetSpecInfo = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.SetSpecInfo`)
schemaTest1 = oai.Paths["/test1/{appId}"].Put.RequestBody.Value.Content["application/json"].Schema
)
t.Assert(schemaCreate.Value.Type, goai.TypeObject)
t.Assert(len(schemaCreate.Value.Properties.Map()), 7)
t.Assert(len(schemaTest1.Value.Properties.Map()), 5)
t.Assert(len(schemaTest1.Value.Properties.Map()), 5)
t.Assert(oai.Paths["/test1/{appId}"].Post.Parameters[0].Value.Schema.Value.Type, goai.TypeInteger)
t.Assert(oai.Paths["/test1/{appId}"].Post.Parameters[1].Value.Schema.Value.Type, goai.TypeString)
t.Assert(len(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.SetSpecInfo`).Value.Properties.Map()), 3)
t.Assert(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.SetSpecInfo`).Value.Properties.Get(`Params`).Value.Type, goai.TypeArray)
t.Assert(len(schemaSetSpecInfo.Value.Properties.Map()), 3)
t.Assert(schemaSetSpecInfo.Value.Properties.Get(`Params`).Value.Type, goai.TypeArray)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.Required, []string{"appId", "Region", "SetMap", "SetSlice"})
t.AssertEQ(schemaCreate.Value.Required, []string{"appId", "Region", "SetMap", "SetSlice"})
// Paths.
t.Assert(len(oai.Paths), 1)
@ -174,8 +185,11 @@ func TestOpenApiV3_Add_Recursive(t *testing.T) {
t.AssertNil(err)
// Schema asserts.
t.Assert(len(oai.Components.Schemas.Map()), 3)
t.Assert(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CategoryTreeItem`).Value.Type, goai.TypeObject)
t.Assert(len(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CategoryTreeItem`).Value.Properties.Map()), 3)
var (
schemaCategoryTreeItem = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CategoryTreeItem`)
)
t.Assert(schemaCategoryTreeItem.Value.Type, goai.TypeObject)
t.Assert(len(schemaCategoryTreeItem.Value.Properties.Map()), 3)
})
}
@ -291,11 +305,16 @@ func TestOpenApiV3_CommonRequest(t *testing.T) {
// Schema asserts.
t.Assert(len(oai.Components.Schemas.Map()), 3)
t.Assert(len(oai.Paths), 1)
t.Assert(len(oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 3)
var (
schemaPut = oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema
schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`)
schemaRes = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Res`)
)
t.Assert(len(schemaPut.Value.Properties.Map()), 3)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`).Value.Required, []string{"product", "name"})
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Res`).Value.Required, []string{"product", "templateName", "version", "txID"})
t.AssertEQ(schemaReq.Value.Required, []string{"product", "name"})
t.AssertEQ(schemaRes.Value.Required, []string{"product", "templateName", "version", "txID"})
})
}
@ -356,15 +375,23 @@ func TestOpenApiV3_CommonRequest_WithoutDataField_Setting(t *testing.T) {
// fmt.Println(oai.String())
t.Assert(len(oai.Components.Schemas.Map()), 4)
t.Assert(len(oai.Paths), 1)
var (
schemaIndexPut = oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema
schemaIndexPost = oai.Paths["/index"].Post.RequestBody.Value.Content["application/json"].Schema
schemaPutReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.PutReq`)
schemaRes = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Res`)
)
t.Assert(len(oai.Paths["/index"].Put.Parameters), 2)
t.Assert(len(oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 3)
t.Assert(len(schemaIndexPut.Value.Properties.Map()), 3)
t.Assert(len(oai.Paths["/index"].Post.Parameters), 0)
t.Assert(len(oai.Paths["/index"].Post.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 5)
t.Assert(len(schemaIndexPost.Value.Properties.Map()), 5)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.PutReq`).Value.Required, []string{"product", "name"})
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.PutReq`).Value.Required, []string{"product", "name"})
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Res`).Value.Required, []string{"product", "templateName", "version", "txID"})
t.AssertEQ(schemaPutReq.Value.Required, []string{"product", "name"})
t.AssertEQ(schemaPutReq.Value.Required, []string{"product", "name"})
t.AssertEQ(schemaRes.Value.Required, []string{"product", "templateName", "version", "txID"})
})
}
@ -404,10 +431,14 @@ func TestOpenApiV3_CommonRequest_EmptyRequest(t *testing.T) {
// fmt.Println(oai.String())
t.Assert(len(oai.Components.Schemas.Map()), 3)
t.Assert(len(oai.Paths), 1)
t.Assert(len(oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 3)
var (
schemaIndex = oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema
schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`)
)
t.Assert(len(schemaIndex.Value.Properties.Map()), 3)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`).Value.Required, []string{"product", "name"})
t.AssertEQ(schemaReq.Value.Required, []string{"product", "name"})
})
}
@ -478,15 +509,21 @@ func TestOpenApiV3_CommonRequest_SubDataField(t *testing.T) {
// fmt.Println(oai.String())
t.Assert(len(oai.Components.Schemas.Map()), 5)
t.Assert(len(oai.Paths), 1)
t.Assert(len(oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 1)
t.Assert(len(oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Get(`Request`).Value.Properties.Map()), 2)
t.Assert(len(oai.Paths["/index"].Post.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 1)
t.Assert(len(oai.Paths["/index"].Post.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Get(`Request`).Value.Properties.Map()), 4)
var (
schemaIndexPut = oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema
schemaIndexPost = oai.Paths["/index"].Post.RequestBody.Value.Content["application/json"].Schema
schemaPutReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.PutReq`)
schemaRes = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Res`)
)
t.Assert(len(schemaIndexPut.Value.Properties.Map()), 1)
t.Assert(len(schemaIndexPut.Value.Properties.Get(`Request`).Value.Properties.Map()), 2)
t.Assert(len(schemaIndexPost.Value.Properties.Map()), 1)
t.Assert(len(schemaIndexPost.Value.Properties.Get(`Request`).Value.Properties.Map()), 4)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.PutReq`).Value.Required, []string{"product", "name"})
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.PutReq`).Value.Required, []string{"product", "name"})
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Res`).Value.Required, []string{"product", "templateName", "version", "txID"})
t.AssertEQ(schemaPutReq.Value.Required, []string{"product", "name"})
t.AssertEQ(schemaPutReq.Value.Required, []string{"product", "name"})
t.AssertEQ(schemaRes.Value.Required, []string{"product", "templateName", "version", "txID"})
})
}
@ -515,9 +552,9 @@ func TestOpenApiV3_CommonRequest_Files(t *testing.T) {
})
t.AssertNil(err)
// fmt.Println(oai.String())
t.Assert(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`).Value.Properties.Get("files").Value.Type, goai.TypeArray)
t.Assert(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`).Value.Properties.Get("file").Value.Type, goai.TypeFile)
var schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`)
t.Assert(schemaReq.Value.Properties.Get("files").Value.Type, goai.TypeArray)
t.Assert(schemaReq.Value.Properties.Get("file").Value.Type, goai.TypeFile)
})
}
@ -560,19 +597,23 @@ func TestOpenApiV3_CommonResponse(t *testing.T) {
})
t.AssertNil(err)
// g.Dump(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map())
// Schema asserts.
t.Assert(len(oai.Components.Schemas.Map()), 3)
t.Assert(len(oai.Paths), 1)
t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 3)
var (
schemaIndexGet = oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema
schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`)
schemaRes = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Res`)
)
t.Assert(len(schemaIndexGet.Value.Properties.Map()), 3)
t.Assert(
oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Get("data").Value.Description,
schemaIndexGet.Value.Properties.Get("data").Value.Description,
`Result data for certain request according API definition`,
)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`).Value.Required, []string{"product", "name"})
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Res`).Value.Required, []string{"product", "templateName", "version", "txID"})
t.AssertEQ(schemaReq.Value.Required, []string{"product", "name"})
t.AssertEQ(schemaRes.Value.Required, []string{"product", "templateName", "version", "txID"})
})
}
@ -617,11 +658,16 @@ func TestOpenApiV3_CommonResponse_WithoutDataField_Setting(t *testing.T) {
fmt.Println(oai.String())
t.Assert(len(oai.Components.Schemas.Map()), 3)
t.Assert(len(oai.Paths), 1)
t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 8)
var (
schemaIndexGet = oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema
schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`)
schemaRes = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Res`)
)
t.Assert(len(schemaIndexGet.Value.Properties.Map()), 8)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`).Value.Required, []string{"product", "name"})
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Res`).Value.Required, []string{"product", "templateName", "version", "txID"})
t.AssertEQ(schemaReq.Value.Required, []string{"product", "name"})
t.AssertEQ(schemaRes.Value.Required, []string{"product", "templateName", "version", "txID"})
})
}
@ -661,11 +707,16 @@ func TestOpenApiV3_CommonResponse_EmptyResponse(t *testing.T) {
// fmt.Println(oai.String())
t.Assert(len(oai.Components.Schemas.Map()), 3)
t.Assert(len(oai.Paths), 1)
t.Assert(oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Ref, `github.com.gogf.gf.v2.net.goai_test.Req`)
t.Assert(len(oai.Paths["/index"].Put.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 3)
var (
schemaIndexPut = oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema
schemeIndexPut200 = oai.Paths["/index"].Put.Responses["200"].Value.Content["application/json"].Schema
schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`)
)
t.Assert(schemaIndexPut.Ref, `github.com.gogf.gf.v2.net.goai_test.Req`)
t.Assert(len(schemeIndexPut200.Value.Properties.Map()), 3)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`).Value.Required, []string{"product", "name"})
t.AssertEQ(schemaReq.Value.Required, []string{"product", "name"})
})
}
@ -720,12 +771,17 @@ func TestOpenApiV3_CommonResponse_SubDataField(t *testing.T) {
// fmt.Println(oai.String())
t.Assert(len(oai.Components.Schemas.Map()), 4)
t.Assert(len(oai.Paths), 1)
t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 1)
t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Get(`Response`).Value.Properties.Map()), 7)
var (
schemaGet = oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema
schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`)
schemaRes = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Res`)
)
t.Assert(len(schemaGet.Value.Properties.Map()), 1)
t.Assert(len(schemaGet.Value.Properties.Get(`Response`).Value.Properties.Map()), 7)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`).Value.Required, []string{"product", "name"})
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Res`).Value.Required, []string{"product", "templateName", "version", "txID"})
t.AssertEQ(schemaReq.Value.Required, []string{"product", "name"})
t.AssertEQ(schemaRes.Value.Required, []string{"product", "templateName", "version", "txID"})
})
}
@ -741,7 +797,7 @@ func TestOpenApiV3_ShortTags(t *testing.T) {
}
type CreateResourceReq struct {
CommonReq
gmeta.Meta `path:"/CreateResourceReq" method:"POST" tags:"default" sm:"CreateResourceReq sum" dc:"CreateResourceReq des"`
gmeta.Meta `path:"/CreateResourceReq" method:"POST" tags:"default" sm:"sm" dc:"CreateResourceReq des"`
Name string `dc:"实例名称"`
Product string `dc:"业务类型"`
Region string `v:"required" dc:"区域"`
@ -776,18 +832,21 @@ func TestOpenApiV3_ShortTags(t *testing.T) {
Object: f,
})
t.AssertNil(err)
// fmt.Println(oai.String())
// Schema asserts.
t.Assert(len(oai.Components.Schemas.Map()), 3)
t.Assert(oai.Paths[`/test1/{appId}`].Summary, ``)
t.Assert(oai.Paths[`/test1/{appId}`].Description, ``)
t.Assert(oai.Paths[`/test1/{appId}`].Put.Summary, `CreateResourceReq sum`)
t.Assert(oai.Paths[`/test1/{appId}`].Put.Summary, `sm`)
t.Assert(oai.Paths[`/test1/{appId}`].Put.Description, `CreateResourceReq des`)
t.Assert(oai.Paths[`/test1/{appId}`].Put.Parameters[1].Value.Schema.Value.Description, `资源Id`)
t.Assert(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.Properties.Get(`Name`).Value.Description, `实例名称`)
var (
schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`)
)
t.Assert(schemaReq.Value.Properties.Get(`Name`).Value.Description, `实例名称`)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.Required, []string{"appId", "Region", "SetMap", "SetSlice"})
t.AssertEQ(schemaReq.Value.Required, []string{"appId", "Region", "SetMap", "SetSlice"})
})
}
@ -918,7 +977,8 @@ func Test_Required_In_Schema(t *testing.T) {
})
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.Required, []string{"appId", "Region", "SetMap", "SetSlice"})
var schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`)
t.AssertEQ(schemaReq.Value.Required, []string{"appId", "Region", "SetMap", "SetSlice"})
})
}
@ -950,10 +1010,12 @@ func Test_Properties_In_Sequence(t *testing.T) {
Object: req,
})
t.AssertNil(err)
fmt.Println(oai)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.ResourceCreateReq`).Value.Required, []string{"AppId", "Uin", "CreateUin", "Product", "Region", "Zone", "Tenant"})
var schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.ResourceCreateReq`)
t.AssertEQ(schemaReq.Value.Required, []string{
"AppId", "Uin", "CreateUin", "Product", "Region", "Zone", "Tenant",
})
})
}
@ -997,7 +1059,8 @@ func TestOpenApiV3_Ignore_Parameter(t *testing.T) {
// fmt.Println(oai.String())
t.Assert(len(oai.Components.Schemas.Map()), 3)
t.Assert(len(oai.Paths), 1)
t.Assert(len(oai.Paths["/test"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 8)
var schemaTest = oai.Paths["/test"].Get.Responses["200"].Value.Content["application/json"].Schema
t.Assert(len(schemaTest.Value.Properties.Map()), 8)
})
}
@ -1018,15 +1081,16 @@ func Test_EnumOfSchemaItems(t *testing.T) {
})
t.AssertNil(err)
var schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`)
t.Assert(
oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.
schemaReq.Value.
Properties.Get(`Members`).Value.
Items.Value.Enum,
g.Slice{"a", "b", "c"},
)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`).Value.Required, []string{"Members"})
t.AssertEQ(schemaReq.Value.Required, []string{"Members"})
})
}
@ -1048,22 +1112,11 @@ func Test_AliasNameOfAttribute(t *testing.T) {
})
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,
)
var schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.CreateResourceReq`)
t.Assert(schemaReq.Value.Properties.Get(`Name`), nil)
t.Assert(schemaReq.Value.Properties.Get(`Age`), nil)
t.AssertNE(schemaReq.Value.Properties.Get(`n`), nil)
t.AssertNE(schemaReq.Value.Properties.Get(`a`), nil)
})
}
@ -1089,7 +1142,7 @@ func Test_EmbeddedStructAttribute(t *testing.T) {
b, err := json.Marshal(oai)
t.AssertNil(err)
t.Assert(b, `{"openapi":"3.0.0","components":{"schemas":{"github.com.gogf.gf.v2.net.goai_test.CreateResourceReq":{"properties":{"Name":{"description":"This is name.","format":"string","type":"string"},"Embedded":{"properties":{"Age":{"description":"This is embedded age.","format":"uint","type":"integer"}},"type":"object"}},"type":"object"}}},"info":{"title":"","version":""},"paths":null}`)
t.Assert(b, gtest.DataContent("EmbeddedStructAttribute", "expect.json"))
})
}
@ -1113,7 +1166,7 @@ func Test_NameFromJsonTag(t *testing.T) {
b, err := json.Marshal(oai)
t.AssertNil(err)
t.Assert(b, `{"openapi":"3.0.0","components":{"schemas":{"github.com.gogf.gf.v2.net.goai_test.CreateReq":{"properties":{"nick_name":{"format":"string","type":"string"}},"type":"object"}}},"info":{"title":"","version":""},"paths":null}`)
t.Assert(b, gtest.DataContent("NameFromJsonTag", "expect1.json"))
})
// GET
gtest.C(t, func(t *gtest.T) {
@ -1134,7 +1187,7 @@ func Test_NameFromJsonTag(t *testing.T) {
b, err := json.Marshal(oai)
t.AssertNil(err)
fmt.Println(string(b))
t.Assert(b, `{"openapi":"3.0.0","components":{"schemas":{"github.com.gogf.gf.v2.net.goai_test.CreateReq":{"properties":{"nick_name":{"format":"string","type":"string"}},"type":"object"}}},"info":{"title":"","version":""},"paths":null}`)
t.Assert(b, gtest.DataContent("NameFromJsonTag", "expect2.json"))
})
}
@ -1187,10 +1240,14 @@ func TestOpenApiV3_PathSecurity(t *testing.T) {
t.Assert(len(oai.Components.SecuritySchemes), 1)
t.Assert(oai.Components.SecuritySchemes["apiKey"].Value.Type, "apiKey")
t.Assert(len(oai.Paths), 1)
t.Assert(len(oai.Paths["/index"].Put.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 3)
var (
schemaIndex = oai.Paths["/index"].Put.Responses["200"].Value.Content["application/json"].Schema
schemaReq = oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`)
)
t.Assert(len(schemaIndex.Value.Properties.Map()), 3)
// Schema Required.
t.AssertEQ(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.net.goai_test.Req`).Value.Required, []string{"product", "name"})
t.AssertEQ(schemaReq.Value.Required, []string{"product", "name"})
})
}
@ -1250,10 +1307,22 @@ func Test_Enums(t *testing.T) {
t.AssertNil(err)
var reqKey = "github.com.gogf.gf.v2.net.goai_test.Req"
t.AssertNE(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Name"), nil)
t.Assert(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Status1").Value.Enum, g.Slice{"a", "b"})
t.Assert(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Status2").Value.Enum, g.Slice{"a", "b"})
t.Assert(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Status3").Value.Items.Value.Enum, g.Slice{"a", "b"})
t.Assert(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Status4").Value.Items.Value.Enum, g.Slice{"a", "b"})
t.Assert(
oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Status1").Value.Enum,
g.Slice{"a", "b"},
)
t.Assert(
oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Status2").Value.Enum,
g.Slice{"a", "b"},
)
t.Assert(
oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Status3").Value.Items.Value.Enum,
g.Slice{"a", "b"},
)
t.Assert(
oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Status4").Value.Items.Value.Enum,
g.Slice{"a", "b"},
)
})
}
@ -1273,6 +1342,6 @@ func Test_XExtension(t *testing.T) {
Object: req,
})
t.AssertNil(err)
t.Assert(oai.String(), `{"openapi":"3.0.0","components":{"schemas":{"github.com.gogf.gf.v2.net.goai_test.GetListReq":{"properties":{"Page":{"default":1,"description":"Page number","format":"int","type":"integer","x-sort":"1"},"Size":{"default":10,"description":"Size for per page.","format":"int","type":"integer","x-sort":"2"}},"type":"object","x-group":"User/Info"}}},"info":{"title":"","version":""},"paths":null}`)
t.Assert(oai.String(), gtest.DataContent("XExtension", "expect.json"))
})
}

View File

@ -0,0 +1 @@
{"openapi":"3.0.0","components":{"schemas":{"github.com.gogf.gf.v2.net.goai_test.CreateResourceReq":{"properties":{"Name":{"description":"This is name.","format":"string","type":"string"},"Embedded":{"properties":{"Age":{"description":"This is embedded age.","format":"uint","type":"integer"}},"type":"object"}},"type":"object"}}},"info":{"title":"","version":""},"paths":null}

View File

@ -0,0 +1 @@
{"openapi":"3.0.0","components":{"schemas":{"github.com.gogf.gf.v2.net.goai_test.CreateReq":{"properties":{"nick_name":{"format":"string","type":"string"}},"type":"object"}}},"info":{"title":"","version":""},"paths":null}

View File

@ -0,0 +1 @@
{"openapi":"3.0.0","components":{"schemas":{"github.com.gogf.gf.v2.net.goai_test.CreateReq":{"properties":{"nick_name":{"format":"string","type":"string"}},"type":"object"}}},"info":{"title":"","version":""},"paths":null}

View File

@ -0,0 +1 @@
{"openapi":"3.0.0","components":{"schemas":{"github.com.gogf.gf.v2.net.goai_test.GetListReq":{"properties":{"Page":{"default":1,"description":"Page number","format":"int","type":"integer","x-sort":"1"},"Size":{"default":10,"description":"Size for per page.","format":"int","type":"integer","x-sort":"2"}},"type":"object","x-group":"User/Info"}}},"info":{"title":"","version":""},"paths":null}