英文:
Fizz OpenAPi generator for Gin is renaming types
问题
我已经创建了一个使用Gin编写的简单API。我使用Fizz生成了一个OpenApi 3规范。这是我的POST端点:
// 设置用户路由组
func UserRoute(grp *fizz.RouterGroup) {
// 创建新用户
grp.POST("", []fizz.OperationOption{
fizz.Summary("创建新用户并发送验证邮件。"),
...
}, tonic.Handler(handlers.CreateUser, 201))
}
这是处理程序方法:
// 创建新用户
func CreateUser(c *gin.Context, register *models.Register) error {
...
return nil
}
问题是在生成的JSON规范中,“Register”模型显示为“CreateUserInput”:
有没有办法修复这个问题,还是这是正常的?
英文:
I have created a simple API written in Gin. I used Fizz to generate an OpenApi 3 specification. Here is my POST endpoint:
// Sets user route group
func UserRoute(grp *fizz.RouterGroup) {
// create new user
grp.POST("", []fizz.OperationOption{
fizz.Summary("Creates new user and sends verification mail."),
...
}, tonic.Handler(handlers.CreateUser, 201))
}
And here is the handler method:
// Creates new user
func CreateUser(c *gin.Context, register *models.Register) error {
...
return nil
}
The problem is that in the generated JSON specification the "Register" model shows as "CreateUserInput":
Is there any way to fix it or is this normal?
答案1
得分: 2
根据实现代码,模式名称是通过以下语句生成的:name := strings.Title(op.ID) + "Input"
(请参见下面的第23行):
1 // setOperationParams adds the fields of the struct type t
2 // to the given operation.
3 func (g *Generator) setOperationParams(op *Operation, t, parent reflect.Type, allowBody bool, path string) error {
4 if t.Kind() != reflect.Struct {
5 return errors.New("input type is not a struct")
6 }
7 if err := g.buildParamsRecursive(op, t, parent, allowBody); err != nil {
8 return err
9 }
10 // Input fields that are neither path- nor query-bound
11 // have been extracted into the operation's RequestBody
12 // If the RequestBody is not nil, give it a name and
13 // move it to the openapi spec's components/schemas section
14 // Replace the RequestBody's schema with a reference
15 // to the named schema in components/schemas
16 if op.RequestBody != nil {
17 mt := tonic.MediaType()
18 if mt == "" {
19 mt = anyMediaType
20 }
21 sch := op.RequestBody.Content[mt].Schema
22 if sch != nil {
23 name := strings.Title(op.ID) + "Input"
24 g.api.Components.Schemas[name] = sch
25 op.RequestBody.Content[mt].Schema = &SchemaOrRef{Reference: &Reference{
26 Ref: componentsSchemaPath + name,
27 }}
28 }
29 }
30 }
你可以使用fizz.ID自定义操作ID,但无法删除Input
后缀。如果你真的想以不同的方式生成模式名称,你需要fork该存储库并修改实现。
英文:
According to the implementation, the schema name is generated by this statement: name := strings.Title(op.ID) + "Input"
(see line 23
below):
1 // setOperationParams adds the fields of the struct type t
2 // to the given operation.
3 func (g *Generator) setOperationParams(op *Operation, t, parent reflect.Type, allowBody bool, path string) error {
4 if t.Kind() != reflect.Struct {
5 return errors.New("input type is not a struct")
6 }
7 if err := g.buildParamsRecursive(op, t, parent, allowBody); err != nil {
8 return err
9 }
10 // Input fields that are neither path- nor query-bound
11 // have been extracted into the operation's RequestBody
12 // If the RequestBody is not nil, give it a name and
13 // move it to the openapi spec's components/schemas section
14 // Replace the RequestBody's schema with a reference
15 // to the named schema in components/schemas
16 if op.RequestBody != nil {
17 mt := tonic.MediaType()
18 if mt == "" {
19 mt = anyMediaType
20 }
21 sch := op.RequestBody.Content[mt].Schema
22 if sch != nil {
23 name := strings.Title(op.ID) + "Input"
24 g.api.Components.Schemas[name] = sch
25 op.RequestBody.Content[mt].Schema = &SchemaOrRef{Reference: &Reference{
26 Ref: componentsSchemaPath + name,
27 }}
28 }
29 }
You can customize the operation ID with fizz.ID, but there is no way to remove the Input
suffix. You need to fork the repository and modify the implementation if you really want to generate the schema name differently.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论