Fizz OpenAPI生成器针对Gin正在重命名类型。

huangapple go评论85阅读模式
英文:

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”:

Fizz OpenAPI生成器针对Gin正在重命名类型。

有没有办法修复这个问题,还是这是正常的?

英文:

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":

Fizz OpenAPI生成器针对Gin正在重命名类型。

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.

huangapple
  • 本文由 发表于 2023年6月30日 08:04:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76585245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定