Skip to content

Commit

Permalink
fix(net/goai): cannot customize OpenAPIv3 type for request parameters (
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn authored Oct 9, 2024
1 parent ae3ae8b commit 4d29939
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 98 deletions.
15 changes: 7 additions & 8 deletions net/goai/goai_shema_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
}
}

Expand All @@ -70,15 +71,15 @@ 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 {
schema.Type = TypeArray
}
}
schemaRef.Value = schema
switch oaiType {
switch schema.Type {
case TypeString, TypeFile:
// Nothing to do.
case TypeInteger:
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
}
}
}
Expand Down
51 changes: 49 additions & 2 deletions net/goai/goai_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -39,15 +40,17 @@ 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
}

// 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(
Expand All @@ -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)
})
}
Loading

0 comments on commit 4d29939

Please sign in to comment.