swag
swag copied to clipboard
Swaggo's boolean params cause rendering errors in the UI
Describe the bug
I was attempting to add one field to my set of parameters as a boolean - for the data type I tried both bool
and boolean
. If I change the same param to a string type, it renders without issue. When the field is set to bool, it causes the rendering of all parameters for that endpoint to fail to render.
To Reproduce Steps to reproduce the behavior:
- Add a param to some endpoint's body
@param hold_gateway_ip body bool (or boolean) false "comment"
- Run swag init (in my case with
swag init -d services/http,services/http/handlers -g http_server.go --parseInternal --parseDependency --md services/http/handlers
- (re-)Start server running swagger
- See error
Expected behavior I expect the parameter fields to render.
Screenshots
This screenshot is what it looks like when I use bool
or boolean
and stops the whole set of params from rendering
Params enders without issue when I change the type from bool
to string
If it's helpful, here's the rough stack trace. I'm happy to get more information if specifically directed, but I don't know much about javascript.
Your swag version 1.8.4 (latest as of writing)
Your go version 1.18.2 darwin/amd64
Desktop (please complete the following information):
- OS: MacOS Big Sur 11.6.7
- Browser: Chrome
- Version: Version 103.0.5060.114 (Official Build) (x86_64)
Additional context The error message in the UI is "could not render n, see the console"
If the body is a JSON payload, please explain how boolean should be marshaled.
Worst case, just display it as text: https://www.w3schools.com/js/js_json_datatypes.asp "value": true, or something similar
How to solve it, I don't understand where the problem is
Does anyone know how to solve this problem?
I'm having the same issue with swaggo v1.8.12 go version go1.20.4 linux/amd64
Any updates or ideas here?
I just ran into this so I hope this helps someone in the future. The issue seems to be confusion around how to document a body parameter.
Our model has 3 members and they were documented like this:
// @Param ids body []string true "ids of objects"
// @Param labels body map[string]string true "labels to update"
// @Param overwrite body boolean false "overwrite labels if true"
It produced reasonable documentation for the first 2 but when the boolean was added we encountered this error.
It appears that the correct way is to document the body is as a single parameter like this:
// @Param body body model.LabelsPayload true "request body containing ids and labels"
And then where model.LabelsPayload
is defined
// @Description Body of the request to update labels
type LabelsPayload struct {
// IDs of objects
IDs []string `json:"ids" example:"agent1,agent2"`
// Labels to update
Labels map[string]string `json:"labels" example:"env:production,region:us-west-1,team:"`
// Overwrite labels if true
Overwrite bool `json:"overwrite"`
}
I ran into same issue today. Any param with data type boolean
(or bool
) will cause this issue.
// @Param active body boolean false "Set account status." Enums(true, false)
Web browser reports error:
Expected `string` for value, got `true`
Change data type from boolean
to string
works:
// @Param active body string false "Set account status." Enums(true, false)
I just ran into this so I hope this helps someone in the future. The issue seems to be confusion around how to document a body parameter.
Our model has 3 members and they were documented like this:
// @Param ids body []string true "ids of objects" // @Param labels body map[string]string true "labels to update" // @Param overwrite body boolean false "overwrite labels if true"
It produced reasonable documentation for the first 2 but when the boolean was added we encountered this error.
It appears that the correct way is to document the body is as a single parameter like this:
// @Param body body model.LabelsPayload true "request body containing ids and labels"
And then where
model.LabelsPayload
is defined// @Description Body of the request to update labels type LabelsPayload struct { // IDs of objects IDs []string `json:"ids" example:"agent1,agent2"` // Labels to update Labels map[string]string `json:"labels" example:"env:production,region:us-west-1,team:"` // Overwrite labels if true Overwrite bool `json:"overwrite"` }
There can be only one body param.