swagger:parameters 一直显示为查询字符串参数

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

swagger:parameters keep showing as query string param

问题

我有以下处理程序:

// 创建数据库
// swagger:route POST /databases createDatabase
//
// 创建数据库
//
// 安全性:
//  oauth2:
//
// 响应:
//   202: Database
//   401: Error
//   ...
func (h Handler) Create(c *gin.Context) {
    var request CreateDatabaseRequest
    if err := handler.DataBinder(c, &request); err != nil {
        _ = c.Error(err)
        return
    }

    ...
}

我通过请求体发送以下结构体:

// CreateDatabaseRequest
// swagger:parameters createDatabase
// in: body
// required: true
//
// 创建数据库的输入参数
type CreateDatabaseRequest struct {
    Name    string `json:"name" binding:"required"`
    GroupId uint   `json:"groupId" binding:"required"`
}

但是当我生成文档时,CreateDatabaseRequest 结构体的属性显示为两个单独的查询参数。

我可以通过创建一个额外的虚拟结构体来解决这个问题,如下所示:

// swagger:parameters createDatabase
type _ struct {
    // 创建数据库参数
    // in: body
    // required: true
    Body database.CreateDatabaseRequest
}

但我不太喜欢这种方式,我更愿意直接在原地记录我的请求参数。

有什么办法可以正确地记录我的结构体,使其显示为请求体参数而不是查询字符串吗?

英文:

I have the following handler

// Create database
// swagger:route POST /databases createDatabase
//
// Create database
//
// Security:
//  oauth2:
//
// responses:
//   202: Database
//   401: Error
//   ...
func (h Handler) Create(c *gin.Context) {
	var request CreateDatabaseRequest
	if err := handler.DataBinder(c, &request); err != nil {
		_ = c.Error(err)
		return
	}

    ...

}

And the following struct which I post via the request body

// CreateDatabaseRequest
// swagger:parameters createDatabase
// in: body
// required: true
//
// Create database input parameter
type CreateDatabaseRequest struct {
	Name    string `json:"name" binding:"required"`
	GroupId uint   `json:"groupId" binding:"required"`
}

But when I generate my docs the properties of CreateDatabaseRequeststruct shows up as two individual query parameter.

I can fix it by creating an additional dummy struct as defined below

// swagger:parameters createDatabase
type _ struct {
	// Create database parameter
	// in: body
	// required: true
	Body database.CreateDatabaseRequest
}

But I don't really like that and would rather just document my request parameter in place.

Any clue about how I can document my struct properly so it shows as a request body parameter rather then query string?

答案1

得分: 0

当你使用swagger:parameters注解时,go-swagger将该结构视为API端点的所有参数的描述,包括头部、查询参数和请求体。因此,你需要一个结构来定义所有这些类型的参数,还需要另一个结构来定义实际的请求体结构。据我所知,没有办法摆脱包含请求体的第二个结构,因为请求体不是API的唯一参数。

英文:

When you use the swagger:parameters annotation, go-swagger treats that structure as the description of all the parameters to an API endpoint, that includes headers, query params, and the body. So you need a structure that defines all these types of parameters, and another structure to define the actual body structure. Thus, as far as I know, there is no way to get rid of the second struct that includes the body, because a body is not the only parameter to an API.

huangapple
  • 本文由 发表于 2021年12月30日 12:30:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/70527491.html
匿名

发表评论

匿名网友

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

确定