swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.45k stars 1.19k forks source link

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

Open wburnett opened 2 years ago

wburnett commented 2 years ago

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):

Additional context The error message in the UI is "could not render n, see the console"

ubogdan commented 1 year ago

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

wburnett commented 1 year ago

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

zsdyx commented 1 year ago

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

zelozzz commented 1 year ago

Does anyone know how to solve this problem?

MikeCongdon1 commented 1 year ago

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

Any updates or ideas here?

andykellr commented 4 months ago

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 commented 2 months ago

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 commented 2 months ago

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.