grape-swagger icon indicating copy to clipboard operation
grape-swagger copied to clipboard

Bug report: omitted array parameters with `array_use_braces: true`

Open olivier-thatch opened this issue 8 months ago • 2 comments

When array_use_braces is set to true, array parameters are omitted when they are body parameters but don't explicitly specify documentation: { param_type: "body" }.

Example:

# with add_swagger_documentation(array_use_braces: true)

params do
  requires :param1, type: Array[String]
  requires :param2, type: Array[Integer], documentation: { param_type: "body" }
end
post :foo do
  { declared_params: declared(params)
end

The spec will look like:

"postFoo": {
  "required": ["param2"],
  "type": "object",
  "properties": {
    "param2": {
      "type": "array",
      "items": { "type": "integer", "format": "int32" }
    }
  }
}

It is particularly annoying if one wants to share some params between GET and POST/PUT/PATCH endpoints and rely on the implicit behavior to have the params be query params or body params as appropriate.

olivier-thatch avatar May 08 '25 18:05 olivier-thatch

I didn't spend too much time looking into this, but I think this happens because:

  • array_use_braces: true causes [] to be appended to array parameter names: https://github.com/ruby-grape/grape-swagger/blob/3e842a138b0f5edd2538d9da4aa23276e63e5d5c/lib/grape-swagger/endpoint/params_parser.rb#L26

  • which in turn causes array parameters to be treated as nested params: https://github.com/ruby-grape/grape-swagger/blob/3e842a138b0f5edd2538d9da4aa23276e63e5d5c/lib/grape-swagger/doc_methods/move_params.rb#L41

Probably the check needs to be updated to exclude foo[] and only treat foo[bar] as nested params?

olivier-thatch avatar May 08 '25 22:05 olivier-thatch

Please do try to PR a fix!

dblock avatar May 18 '25 12:05 dblock