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

Fix issue where array params are sometimes omitted when `array_use_braces` is true

Open olivier-thatch opened this issue 7 months ago • 3 comments

Fixes #952.

The fix consists of removing the [] suffix at the end of a parameter's name if the parameter is an array and it's a body param.

This fix is very hacky: ideally, [] shouldn't be added in the first place. But I don't think that can be achieved without significantly refactoring the code.

@dblock @numbata Curious if you have any thoughts on this approach.

olivier-thatch avatar May 21 '25 00:05 olivier-thatch

CI was fixed in https://github.com/ruby-grape/grape-swagger/pull/958, rebase please?

dblock avatar May 24 '25 13:05 dblock

Hi @olivier-thatch, a quick fix for this PR could be:

diff --git a/lib/grape-swagger/doc_methods/move_params.rb b/lib/grape-swagger/doc_methods/move_params.rb
index c8417ce..12c6477 100644
--- a/lib/grape-swagger/doc_methods/move_params.rb
+++ b/lib/grape-swagger/doc_methods/move_params.rb
@@ -136,7 +136,7 @@ module GrapeSwagger
           return if value.blank?
 
           definition[:required] ||= []
-          definition[:required].push(*value)
+          definition[:required].push(*value).uniq!
         end
 
         def build_body_parameter(name, options)

I considered using a Set.new for definition[:required] to enforce uniqueness, but that would change the interface (as example, Set doesn’t support .push) and could introduce side effects for other grape-swagger-* gems.

numbata avatar May 31 '25 11:05 numbata

I looked more closely at the PR and noticed we have incorrect behavior for nested arrays. The type Array[Array[String]] collapses into String, so document_array_param only ever sees one dimension. As a result, a declaration like requires :matrix, type: Array[Array[String]] produces { type: 'array', items: { type: 'string' } }, which loses the inner array.

We need to either fix this to properly support multi-dimensional arrays or clearly document that they aren’t supported.

numbata avatar Oct 26 '25 21:10 numbata