Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swaggo's boolean params cause rendering errors in the UI #1307

Open
wburnett opened this issue Aug 25, 2022 · 8 comments
Open

Swaggo's boolean params cause rendering errors in the UI #1307

wburnett opened this issue Aug 25, 2022 · 8 comments
Labels

Comments

@wburnett
Copy link

wburnett commented Aug 25, 2022

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:

  1. Add a param to some endpoint's body @param hold_gateway_ip body bool (or boolean) false "comment"
  2. 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
  3. (re-)Start server running swagger
  4. 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
Screen Shot 2022-08-24 at 6 32 16 PM

Params enders without issue when I change the type from bool to string
Screen Shot 2022-08-24 at 6 31 37 PM

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.
Screen Shot 2022-08-24 at 6 36 18 PM

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"

@ubogdan
Copy link
Contributor

ubogdan commented Sep 17, 2022

If the body is a JSON payload, please explain how boolean should be marshaled.

@wburnett
Copy link
Author

Worst case, just display it as text: https://www.w3schools.com/js/js_json_datatypes.asp
"value": true, or something similar

@zsdyx
Copy link

zsdyx commented May 17, 2023

How to solve it, I don't understand where the problem is

@zelozzz
Copy link

zelozzz commented May 24, 2023

Does anyone know how to solve this problem?

@MikeCongdon1
Copy link

I'm having the same issue with
swaggo v1.8.12
go version go1.20.4 linux/amd64

Any updates or ideas here?

@andykellr
Copy link

andykellr commented Apr 19, 2024

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"`
}

@iredmail
Copy link

iredmail commented Jun 30, 2024

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)

@sdghchj
Copy link
Member

sdghchj commented Jul 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants