Repository navigation
Fix issue where array params are sometimes omitted when array_use_braces is true - #957
Conversation
|
CI was fixed in #958, rebase please? |
e4b3778 to
eba362d
Compare
array_use_braces is true
|
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 |
d19daf7 to
003b77f
Compare
|
I looked more closely at the PR and noticed we have incorrect behavior for nested arrays. The type We need to either fix this to properly support multi-dimensional arrays or clearly document that they aren’t supported. |
06ab743 to
e070862
Compare
Danger ReportNo issues found. |
There was a problem hiding this comment.
🟡 Changes recommended
A critical defect leaves deeply nested array schemas with invalid inner item types because array declarations are only unwrapped one level instead of being recursively normalized.
1 open issue
What changed in this PR
This pull request fixes omitted array parameters when array_use_braces is enabled and preserves nested array structure in generated Swagger 2.0 schemas.
Changes:
- Removes trailing
[]from array parameters converted to request-body properties. - Deduplicates parameters before body-definition generation.
- Adds nested-array schema handling and regression coverage.
- Updates the changelog.
The nested-array implementation still produces invalid leaf item types for declarations such as Array[Array[String]]; recursive type normalization is required before approval.
This pull request requires changes.
| File | Description |
|---|---|
spec/swagger_v2/params_array_spec.rb |
Adds regression coverage for body, query, form, shared, and nested array parameters. |
lib/grape-swagger/endpoint.rb |
Deduplicates parameters by name and location before moving body parameters into definitions. |
lib/grape-swagger/doc_methods/parse_params.rb |
Normalizes body array names and attempts to preserve nested array structure; nested leaf types require recursive normalization. |
CHANGELOG.md |
Documents the array-parameter and nested-array fixes. |
Review effort: Lite
b0a37cb to
537d179
Compare
With array_use_braces, the body parser adds a braced copy of every array param next to the original. Once the brace is stripped for body params the two collide, and the copy is merged over the group's object definition, turning items.type from object into string. Dropping the duplicate by name and location also removes the duplicate required entries, so the uniq! on required is no longer needed.
Grape passes the declared type as e.g. "[[String]]", but only the innermost type was documented, so Array[Array[String]] came out as a flat array of strings. Wrap the items in one array level per leading bracket.
537d179 to
0e02729
Compare

Fixes #952.
Summary
Sharing array parameters between GET and POST/PUT/PATCH endpoints no longer drops them from the generated body definition when
array_use_bracesis on. Nested arrays such asArray[Array[String]]are now documented with every level instead of being flattened to a single array of strings.Root cause
With
array_use_braces: true,RequestParamParsers::Body#parseappends[]to every array param name. It runs before the param's location is known, because that depends on the HTTP method andconsumes. Params that become body params (any non-GET without an explicitparam_type) kept the braced name, so the body definition got a bogus property or lost the array. The parser also adds the braced copy next to the original, so after renaming, the two collide and the copy was merged over the group's object definition (items.typeobjectbecamestring).Changes
ParseParamsstrips the[]suffix for array params that end up in the body.params_objectdrops duplicates by[name, in]before moving params into a body definition. This also removes the duplicaterequiredentries, so theuniq!suggested earlier is no longer needed.arraylevel per nesting (Array[Array[String]]becomes anarrayofarrayofstring).Drawbacks
Body#parseand removed later inParseParams. This is a patch over a decision made too early; see below.itemsused to be flattened and now has the inner array. Nested arrays in query/formData params are documented the same way, which is valid in the Swagger 2.0itemsschema but unusual for a query string.Rejected alternatives
Body#parse: the param'sinisn't known there (it depends on method andconsumes), so it would need a larger refactor.Setfordefinition[:required]: changes the interface that othergrape-swagger-*gems rely on (Sethas nopush).uniq!onrequired: it only hid the duplicate params.Not covered
Array[JSON]andJSONparams still documenttype: "json", which is not a Swagger type. That is unrelated toarray_use_bracesand tracked in #1014.