Fix Array[Object] documentation type parsing#86
Merged
numbata merged 10 commits intoJan 28, 2026
Conversation
Here's an example of a CHANGELOG.md entry: * [#86](https://github.com/ruby-grape/grape-swagger-entity/pull/86): Fix: properly parse custom array documentation - [@numbata](https://github.com/numbata).Generated by 🚫 Danger |
- Reproduces "Empty model" error when using Grape::Entity with all hidden properties - Tests custom documentation override with Array[Object] type - Includes working example showing expected behavior - Follows existing issue spec patterns in the codebase
This commit addresses a long-standing issue where array types in documentation were not correctly parsed unless the explicit `is_array` option was used. The primary changes are: - A new `array_type?` method is introduced in `attribute_parser.rb` to intelligently check for `is_array`, `type: 'array'`, or `type: 'Array[Object]'`. - The `parse_grape_entity_params` method in `parser.rb` is refactored to improve readability and reduce complexity
…oved from required)
a343211 to
7566ddb
Compare
Danger ReportNo issues found. |
e3580e9 to
2a91237
Compare
grape-swagger < 2.1.3 doesn't support empty model definitions. Support was added in grape-swagger PR #963 which will be released in version 2.1.3. Tests are skipped for older versions.
2a91237 to
3971c76
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes Swagger schema generation for Grape entities documented with custom array type syntax like type: 'Array[Object]', and refactors entity parsing for better readability and testability.
Changes:
- Introduces
array_type?inAttributeParserto interpretis_array,type: 'array', andtype: 'Array[object]'as arrays, and refines model/type resolution logic. - Refactors
Parser#parse_grape_entity_paramsinto smaller helpers (parse_params,skip_param?,parse_entity_options,handle_discriminator, etc.) while preserving the public result structure. - Adds regression specs for issue #962 (polymorphic entity with custom documentation type), wires shared contexts correctly, and updates test/config files (
.rspec,.rubocop_todo.yml,CHANGELOG.md).
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
lib/grape-swagger/entity/attribute_parser.rb |
Adds array_type?, improves primitive/model detection, and updates array handling for both documented primitives and entity models; contains a bug where array_type? is called with a potentially nil documentation hash in entity_model_type. |
lib/grape-swagger/entity/parser.rb |
Refactors entity param parsing into helper methods, centralizes required-field computation, and adjusts discriminator handling while keeping the external return shape compatible. |
spec/issues/962_polymorphic_entity_with_custom_documentation_spec.rb |
New regression specs ensuring entities with documentation: { type: 'Array[object]' } and empty/hidden properties generate the expected Swagger definitions; the skip message’s version text is inconsistent with the actual version check. |
spec/grape-swagger/entities/response_model_spec.rb |
Ensures the shared this api context is included for the first describe block so the API under test is properly defined. |
spec/support/shared_contexts/custom_type_parser.rb |
Replaces the anonymous CustomType = Class.new with an explicit empty CustomType class to satisfy style cops and keep the custom model parser registration intact. |
CHANGELOG.md |
Documents this fix as PR #86 in the “Fixes” section. |
.rubocop_todo.yml |
Updates RuboCop exclusions and offense counts to accommodate the new spec file, empty class, and issue-spec layout. |
.rspec |
Ensures spec_helper is always loaded via the RSpec configuration, simplifying spec setup. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Add nil guard for safety - Only wrap arrays when is_array: true or type: 'Array[ElementType]' - Bare type: 'Array' no longer double-wraps into array-of-arrays
- Test Array[Type] syntax detection - Test bare 'Array' type doesn't double-wrap - Test is_array: false is respected - Test case-insensitive array[type] syntax
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Address CoPilot review feedback for more explicit nil handling pattern.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes incorrect Swagger schema generation when using custom array documentation syntax like
type: 'Array[Object]'.Problem
When using
type: 'Array[Object]'with an entity, the Swagger schema was not correctly generated as an array type with$refitems.Solution
Add
array_type?method that detects array notations:is_array: true- explicit flag (existing behavior)type: 'Array[Object]'- new syntax for entity arraysUsage Example
Generates correct schema:
{ "type": "array", "description": "List of items", "items": { "$ref": "#/definitions/ItemEntity" } }Other Changes
parse_grape_entity_paramsinto smaller, focused methods for readabilityparse_nestedmethodBackward Compatibility
is_array: truecontinues to work as beforeis_array: falseis respected when explicitly settype: 'Array'is not double-wrappedNote
Tests for empty entity handling require grape-swagger >= 2.1.3 (ruby-grape/grape-swagger#963) and are skipped for older versions.
Fixes ruby-grape/grape-swagger#962