swag icon indicating copy to clipboard operation
swag copied to clipboard

[BUG] - Param in body as string is treated as object instead of string

Open sujit-baniya opened this issue 2 years ago • 11 comments

Describe the bug In comments, I've defined as follow.

// ExecuteFlow godoc
// @Summary      Run flow with data
// @description.markdown execute-flow
// @Tags         flow
// @Accept       json
// @Produce      json
// @Param        id   path string  required  "Slug of the flow. For e.g. caaefc93ol0khqmaor2g"
// @Param        message body string false "Message example"
// @Param        from body string  false "Sender information"
// @Param        to body string  false  "Recipient information"
// @Router       /flows/{id}/execute [post]
// @Success      200  {object}  response.SuccessResponse{data=dto.Collection}

But the generated code have object type instead of string.

image

Expected behavior

"parameters": [
                    {
                        "type": "string",
                        "description": "Slug of the flow. For e.g. caaefc93ol0khqmaor2g",
                        "name": "id",
                        "in": "path",
                        "required": true
                    },
                    {
                        "description": "Message example",
                        "name": "message",
                        "in": "body",
                        "type": "string"
                    },
                    {
                        "description": "Sender information",
                        "name": "from",
                        "in": "body",
                        "type": "string"
                    },
                    {
                        "description": "Recipient information",
                        "name": "to",
                        "in": "body",
                        "type": "string"
                    }
                ],

Actual Behavior

"parameters": [
                    {
                        "type": "string",
                        "description": "Slug of the flow. For e.g. caaefc93ol0khqmaor2g",
                        "name": "id",
                        "in": "path",
                        "required": true
                    },
                    {
                        "description": "Message example",
                        "name": "message",
                        "in": "body",
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "description": "Sender information",
                        "name": "from",
                        "in": "body",
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "description": "Recipient information",
                        "name": "to",
                        "in": "body",
                        "schema": {
                            "type": "string"
                        }
                    }
                ],

sujit-baniya avatar Jun 18 '22 02:06 sujit-baniya

The annotation // @Accept json means your payload is JSON payload and you can't have multiple body param definitions.

ubogdan avatar Jun 18 '22 12:06 ubogdan

@ubogdan Please suggest me how would I add comment for body with following structure

{
    "name": "John",
    "age": 23
}

sujit-baniya avatar Jun 18 '22 13:06 sujit-baniya

Take a look at https://github.com/swaggo/swag/blob/master/example/basic/api/api.go

ubogdan avatar Jun 18 '22 13:06 ubogdan

@ubogdan didn't receive expected result when using like this (as suggest above)

// ExecuteFlow godoc
// @Summary      Run flow with data
// @description.markdown execute-flow
// @Tags         flow
// @Accept       json
// @Produce      json
// @Param        id   path string  required  "Slug of the flow. For e.g. caaefc93ol0khqmaor2g"
// @Param        id body dto.FlowSampleData required "Sample Flow Data"
// @Router       /flows/{id}/execute [post]
// @Success      200  {object}  response.SuccessResponse{data=dto.Collection}

It resulted in params as below

"parameters": [
                    {
                        "type": "string",
                        "description": "Slug of the flow. For e.g. caaefc93ol0khqmaor2g",
                        "name": "id",
                        "in": "path",
                        "required": true
                    },
                    {
                        "description": "Sample Flow Data",
                        "name": "id",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/dto.FlowSampleData"
                        }
                    }
                ],

Am I missing something?

sujit-baniya avatar Jun 18 '22 13:06 sujit-baniya

I see it generates the correct output

                   {
                        "description": "Sample Flow Data",
                        "name": "id",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/dto.FlowSampleData"
                        }
                    }

ubogdan avatar Jun 18 '22 13:06 ubogdan

@ubogdan image

sujit-baniya avatar Jun 18 '22 13:06 sujit-baniya

My expection with multiple body params was like this image

sujit-baniya avatar Jun 18 '22 13:06 sujit-baniya

Your expectation should conform to OAS2 specs, but it doesn't.

ubogdan avatar Jun 18 '22 13:06 ubogdan

I got it know :) Thanks

sujit-baniya avatar Jun 18 '22 13:06 sujit-baniya

@ubogdan If you don't mind please help me understand this part. image

According to this documentation, user is just a placeholder and not used. But on swagger UI, I see user on the top level

sujit-baniya avatar Jun 18 '22 14:06 sujit-baniya

in this example, the user is defined in the body param, in your user is a reference to the user model. Using models and refs is more convenient because it allows you to reuse them.

ubogdan avatar Jun 18 '22 15:06 ubogdan