Skip to content

Fix Array[Object] documentation type parsing#86

Merged
numbata merged 10 commits into
ruby-grape:masterfrom
numbata:bugfix/issue-962-empty-model-error
Jan 28, 2026
Merged

Fix Array[Object] documentation type parsing#86
numbata merged 10 commits into
ruby-grape:masterfrom
numbata:bugfix/issue-962-empty-model-error

Conversation

@numbata

@numbata numbata commented Aug 24, 2025

Copy link
Copy Markdown
Collaborator

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 $ref items.

Solution

Add array_type? method that detects array notations:

  • is_array: true - explicit flag (existing behavior)
  • type: 'Array[Object]' - new syntax for entity arrays

Usage Example

class Report < Grape::Entity
  expose :items,
         using: ItemEntity,
         documentation: {
           type: 'Array[Object]',
           desc: 'List of items'
         }
end

Generates correct schema:

{
  "type": "array",
  "description": "List of items",
  "items": { "$ref": "#/definitions/ItemEntity" }
}

Other Changes

  • Refactor parse_grape_entity_params into smaller, focused methods for readability
  • Add nil guards to prevent potential crashes
  • Fix indentation in parse_nested method

Backward Compatibility

  • is_array: true continues to work as before
  • is_array: false is respected when explicitly set
  • Bare type: 'Array' is not double-wrapped

Note

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

@numbata numbata self-assigned this Aug 24, 2025
@grape-bot

Copy link
Copy Markdown
1 Warning
⚠️ Unless you're refactoring existing code or improving documentation, please update CHANGELOG.md.

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
@numbata numbata force-pushed the bugfix/issue-962-empty-model-error branch from a343211 to 7566ddb Compare January 28, 2026 11:11
@github-actions

github-actions Bot commented Jan 28, 2026

Copy link
Copy Markdown

Danger Report

No issues found.

View run

@numbata numbata force-pushed the bugfix/issue-962-empty-model-error branch from e3580e9 to 2a91237 Compare January 28, 2026 12:26
@numbata numbata changed the title Fix: Properly parse custom array documentation Support Array[Object] syntax in entity documentation Jan 28, 2026
@numbata numbata changed the title Support Array[Object] syntax in entity documentation Fix Array[Object] documentation type parsing Jan 28, 2026
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.
@numbata numbata force-pushed the bugfix/issue-962-empty-model-error branch from 2a91237 to 3971c76 Compare January 28, 2026 12:31
@numbata numbata marked this pull request as ready for review January 28, 2026 12:32
@numbata numbata requested a review from Copilot January 28, 2026 12:32

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.

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? in AttributeParser to interpret is_array, type: 'array', and type: 'Array[object]' as arrays, and refines model/type resolution logic.
  • Refactors Parser#parse_grape_entity_params into 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.

Comment thread lib/grape-swagger/entity/attribute_parser.rb Outdated
- 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

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.

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.
@numbata numbata merged commit 409d701 into ruby-grape:master Jan 28, 2026
11 checks passed
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.

documentation isn't respected when generated definitions include nested entities

3 participants