【问题标题】:gin-swagger can not find schema type: "handler.component"gin-swagger 找不到架构类型:“handler.component”
【发布时间】:2021-03-10 18:56:40
【问题描述】:

我正在尝试使用我创建并在我的代码中用作参数的结构,但在运行 swag init 后,我收到以下错误:

ParseComment error in file /src/handler/handler.go :can not find schema type: "handler.component"

我的结构:

package types

// Component defines the structure of the json that user will send for a log search
// swagger:parameters component
type Component struct {
    // in: query
    // example: {"envID":"default", "podID":"log-gen-6d776dc797-bnbm9", "follow":false, "tail":5}
    EnvID  string `json:"envID"`  // The env-id
    PodID  string `json:"podID"`  // The podID
    Tail   int    `json:"tail"`   // Number of lines for tailing the logs
    Follow bool   `json:"follow"` // If the we want to follow the logs or not
    Site   string `json:"site"`   // The cluster/site which hosts the component --> local is pointing to the local cluster
}

我的处理程序:

package handler

import (
    "src/types"
)

// FollowLogsSSE is ...
// @Summary Return logs
// @Accept  json
// @Produce  json
// @Param q query component true "{'envID':'default', 'podID':'log-gen-6d776dc797-bnbm9', 'follow':false, 'tail':5}"
// @Success 200 {object} string string
func FollowLogsSSE(comp types.Component) gin.HandlerFunc {
}

我也试过// @Param q query types.component true "{'envID':'default', 'podID':'log-gen-6d776dc797-bnbm9', 'follow':false, 'tail':5}",但我得到了完全相同的错误。

我该如何解决这个问题?

【问题讨论】:

    标签: go swagger go-gin


    【解决方案1】:

    解决方案是使用types.Component(大写C),一切都会正常。

    一些额外的点:

    • 需要任何类型的装饰器等,swagger 会毫无问题地找到结构,并显示每个字段的注释
    • 要根据需要定义字段,您可以将binding:"required" 添加到该字段:
        EnvID  string `json:"envID" binding:"required"` // The env-id: this can be an env that has created by the user or any other K8s namespace
    

    【讨论】:

      猜你喜欢
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      相关资源
      最近更新 更多