Swag: struct as body without any param

Created on 18 Nov 2019  路  8Comments  路  Source: swaggo/swag

I would like to be able to use a struct as body for a POST request.

If I have a struct like

type User struct {
    Username string `json:"username"`
    Password string `json:"password"`
}

and i want to post {"username:":"user","password":"pass"} i can't describe that using the structure.

An hack to workaround this is using a body param named "body" and extracting data from that param in the API.

Maybe I'm only missing a configuration somewhere.

Most helpful comment

As per operations.go:

// ParseParamComment parses params return []string of param properties
//              [param name]    [paramType] [data type]  [is mandatory?]   [Comment]

Assuming you want to send a json payload , the definition should loook like

// @Param   payload   body    User    false  "Payload description"

All 8 comments

As per operations.go:

// ParseParamComment parses params return []string of param properties
//              [param name]    [paramType] [data type]  [is mandatory?]   [Comment]

Assuming you want to send a json payload , the definition should loook like

// @Param   payload   body    User    false  "Payload description"

This creates a variable named payload in the swagger, is not in the root of the payload

                "parameters": [
                    {
                        "description": "New User",
                        "name": "payload",
                        "in": "body",
                        "schema": {
                            "$ref": "#/definitions/model.User"
                        }
                    }
                ],

The request payload according to the documentation generated will be something like

{"payload":{"username:":"user","password":"pass"}}

not the real

{"username:":"user","password":"pass"}

I can bet you are just starting a new project and you have no clue what is it about. Please put your doc.json into swagger editor to play with generated documentation.

The body param should be declared once. See doc :
The POST, PUT and PATCH requests can have the request body (payload), such as JSON or XML data. In Swagger terms, the request body is called a body parameter. There can be only one body parameter, although the operation may have other parameters (path, query, header).

@tomaluca95 Have you found any solution?

@shenzhigang I followed the last comment.

If you place only one parameter in the body is rightly interpreted. Please try the swagger editor as suggested, I done that and understood my error.

@tomaluca95 how did you treat required parameters?

e.g.

type User struct {
    Username string `json:"username"` <----required
    Password string `json:"password"` <----required
        Name string `json:"name"` <----not-required
}

Because the formation

// @Param payload body model.User true "payload"

will just mark _payload_ as required ignoring the internal model.

@gkech I think https://github.com/swaggo/swag#available there is the answer

type User struct {
    Username string `json:"username" validate:"required"`
    Password string `json:"password" validate:"required"`
        Name string `json:"name"`
}

I haven't used this annotation but is a good idea to add that, thanks!

Yeap, it works like a charm! Thanks a lot @tomaluca95!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dz0ny picture dz0ny  路  5Comments

kirillDanshin picture kirillDanshin  路  3Comments

chiragg6 picture chiragg6  路  7Comments

padurean picture padurean  路  6Comments

JimChung0403 picture JimChung0403  路  3Comments