Skip to content

Fix: Recognize Grape::API::Boolean as a primitive type #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Fixes

* Your contribution here.
* [#82](https://github.com/ruby-grape/grape-swagger-entity/pull/82): Fix: simplify primitive types recognition - [@numbata](https://github.com/numbata).

### 0.6.1 (2025/05/06)

Expand Down
6 changes: 1 addition & 5 deletions lib/grape-swagger/entity/attribute_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ module Entity
class AttributeParser
attr_reader :endpoint

# The list of that doesn't handled by `GrapeSwagger::DocMethods::DataType.primitive?` method
ADDITIONAL_PRIMITIVE_TYPES = %w[string array].freeze

def initialize(endpoint)
@endpoint = endpoint
end
Expand Down Expand Up @@ -61,8 +58,7 @@ def ambiguous_model_type?(type)

def primitive_type?(type)
data_type = GrapeSwagger::DocMethods::DataType.call(type)
GrapeSwagger::DocMethods::DataType.primitive?(data_type) ||
ADDITIONAL_PRIMITIVE_TYPES.include?(data_type)
GrapeSwagger::DocMethods::DataType.request_primitive?(data_type)
end

def data_type_from(entity_options)
Expand Down
34 changes: 34 additions & 0 deletions spec/grape-swagger/entity/attribute_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,24 @@ def self.to_s
it { is_expected.not_to include('$ref') }
end

context 'when it is exposed as a Boolean class' do
let(:entity_options) do
{ documentation: { type: Grape::API::Boolean, example: example_value, default: example_value } }
end

context 'when the example value is true' do
let(:example_value) { true }

it { is_expected.to include(type: 'boolean', example: example_value, default: example_value) }
end

context 'when the example value is false' do
let(:example_value) { false }

it { is_expected.to include(type: 'boolean', example: example_value, default: example_value) }
end
end

context 'when it is exposed as a boolean' do
let(:entity_options) { { documentation: { type: 'boolean', example: example_value, default: example_value } } }

Expand All @@ -277,6 +295,22 @@ def self.to_s
it { is_expected.to include(type: 'boolean', example: example_value, default: example_value) }
end
end

context 'when it is exposed as a hash' do
let(:entity_options) { { documentation: { type: Hash, example: example_value, default: example_value } } }

context 'when the example value is true' do
let(:example_value) { true }

it { is_expected.to include(type: 'object', example: example_value, default: example_value) }
end

context 'when the example value is false' do
let(:example_value) { false }

it { is_expected.to include(type: 'object', example: example_value, default: example_value) }
end
end
end
end
end