Search code examples
goswaggergo-swagger

swagger:parameters keep showing as query string param


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?


Solution

  • 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.