Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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