swag icon indicating copy to clipboard operation
swag copied to clipboard

Swaggo's boolean params cause rendering errors in the UI

Open wburnett opened this issue 2 years ago • 8 comments

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"

wburnett avatar Aug 25 '22 15:08 wburnett

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

ubogdan avatar Sep 17 '22 20:09 ubogdan

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

wburnett avatar Apr 20 '23 19:04 wburnett

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

zsdyx avatar May 17 '23 07:05 zsdyx

Does anyone know how to solve this problem?

zelozzz avatar May 24 '23 08:05 zelozzz

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

Any updates or ideas here?

MikeCongdon1 avatar Aug 02 '23 15:08 MikeCongdon1

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

andykellr avatar Apr 19 '24 20:04 andykellr

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)

iredmail avatar Jun 30 '24 04:06 iredmail

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.

sdghchj avatar Jul 01 '24 02:07 sdghchj