首頁 > 後端開發 > Golang > 主體

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

WBOY
發布: 2024-02-08 20:54:16
轉載
758 人瀏覽過

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

問題內容

我建立了一個用 gin 寫的簡單 api。我使用 fizz 產生 openapi 3 規範。這是我的 post 端點:

// 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))
}
登入後複製

這是處理程序方法:

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

    return nil
}
登入後複製

問題在於,在產生的 json 規格中,「register」模型顯示為「createuserinput」:

有什麼辦法可以解決這個問題嗎?還是這是正常現象嗎?


正確答案


根據實作,模式名稱由以下語句產生: 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      }
登入後複製

您可以使用 fizz.id 自訂操作 id,但沒有辦法去掉input後綴。如果您確實想以不同的方式產生模式名稱,則需要分叉儲存庫並修改實作。

以上是Gin 的 Fizz OpenAPi 生成器正在重新命名類型的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!