Gin's Fizz OpenAPi generator is renaming types

WBOY
Release: 2024-02-08 20:54:16
forward
757 people have browsed it

Gin 的 Fizz OpenAPi 生成器正在重命名类型

Question content

I created a simple api written in gin. I use fizz to generate the openapi 3 spec. This 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))
}
Copy after login

This is the handler method:

// Creates new user
func CreateUser(c *gin.Context, register *models.Register) error {
    ...

    return nil
}
Copy after login

The problem is that in the generated json spec, the "register" model is shown as "createuserinput":

Is there any way to solve this problem? Or is this normal?


Correct answer


According to implementation, the schema name is generated by the following 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      }
Copy after login

You can use fizz.id to customize the operation id, but there is no way to remove the input suffix. If you really want to generate schema names differently, you'll need to fork the repository and modify the implementation.

The above is the detailed content of Gin's Fizz OpenAPi generator is renaming types. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!