Skip to content

Document array defaults on the array instead of its items - #1013

Merged
numbata merged 3 commits into
masterfrom
issue-950-array-default
Nov 9, 2026
Merged

numbata merged 3 commits into
masterfrom
issue-950-array-default

Conversation

@numbata

@numbata numbata commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Array parameters with an array default, like type: [String], default: %w[8.8.8.8 8.8.4.4], now document that default on the array itself. Previously the whole list was placed under items, so Swagger tools showed a nested array and the schema was invalid. Scalar defaults keep their current placement.

Root cause

ParseParams#parse_array_item copied default into items for every array param, while the array-level default was skipped. An Array value is therefore never valid there (Swagger 2.0 requires items.default to be a single item value). Body params go through the same method and MoveParams only copied minItems/maxItems onto the array property, so the fix covers query, formData and body.

Drawbacks

Output changes for anyone with an Array default on an array param; consumers reading items.default for it will no longer find it.

Not covered

Defaults that are not plain values are unchanged and still go through the scalar path: a lambda default (default: -> { %w[a b] }) still emits items.default: {}, and default: false on a boolean array is still omitted. Both behave this way on master and are separate from where an Array default is placed.

Alternatives considered

Moving every default to the array level was rejected: #650 intentionally documents scalar defaults in items, and its specs pin that.

Closes #950

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Danger Report

No issues found.

View run

@numbata
numbata marked this pull request as ready for review November 3, 2026 12:35
@numbata
numbata requested a lite review from Copilot November 3, 2026 12:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain, and the change includes targeted parsing and parameter-move updates with regression coverage for both query and body array defaults.

No open issues 🎉

What changed in this PR

This pull request fixes Swagger 2.0 documentation for array-valued defaults by placing array defaults on the array schema rather than within items, while preserving scalar item-default behavior.

Changes:

  • Stores array defaults on the parsed array parameter.
  • Preserves array defaults when moving body parameters into schema properties.
  • Adds regression coverage for query and body parameters.
  • Documents the fix in the changelog.
File Description
spec/​issues/​950_array_default_spec.rb Verifies correct array-default output for query and body parameters.
lib/​grape-swagger/​doc_methods/​parse_params.rb Distinguishes array defaults from scalar item defaults during parsing.
lib/​grape-swagger/​doc_methods/​move_params.rb Preserves array-level defaults when converting body parameters into schema properties.
CHANGELOG.md Documents the array-default placement fix.

Review effort: Lite

A default such as `%w[8.8.8.8 8.8.4.4]` on a `type: [String]` param was
written to `items.default`, where Swagger 2.0 expects a single item value,
so tools rendered a nested array. Array defaults now land on the array
parameter, or on the array property of a body definition. Scalar defaults
keep their existing placement in `items`.
Pins formData, empty, scalar and Array[JSON] defaults, and documents that an Array default moves out of items.default.
@numbata
numbata force-pushed the issue-950-array-default branch from 1ad5780 to 93cf118 Compare November 8, 2026 12:08
@numbata
numbata merged commit 88b8111 into master Nov 9, 2026
64 checks passed
@numbata
numbata deleted the issue-950-array-default branch November 9, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: default for array params (type: [String]) is incorrectly placed inside items

2 participants