swag
swag copied to clipboard
[BUG] - Param in body as string is treated as object instead of string
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.
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"
}
}
],
The annotation // @Accept json
means your payload is JSON payload and you can't have multiple body param definitions.
@ubogdan Please suggest me how would I add comment for body with following structure
{
"name": "John",
"age": 23
}
Take a look at https://github.com/swaggo/swag/blob/master/example/basic/api/api.go
@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?
I see it generates the correct output
{
"description": "Sample Flow Data",
"name": "id",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/dto.FlowSampleData"
}
}
@ubogdan
My expection with multiple body params was like this
Your expectation should conform to OAS2 specs, but it doesn't.
I got it know :) Thanks
@ubogdan If you don't mind please help me understand this part.
According to this documentation, user
is just a placeholder and not used. But on swagger UI, I see user
on the top level
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.