From aa54c3d5e46cec5f4ab386f127f37f5eccb1240e Mon Sep 17 00:00:00 2001 From: Andrei Subbota Date: Thu, 8 May 2025 01:16:05 +0200 Subject: [PATCH 1/5] Fix: Handle Boolean type as a primitive in attribute parser The `AttributeParser` was not correctly identifying `Grape::API::Boolean` as a primitive type, leading to incorrect Swagger schema generation for boolean attributes. This commit updates the `ADDITIONAL_PRIMITIVE_TYPES` constant to include "boolean". This resolves issues where boolean types were potentially being treated as complex objects or having incorrect type definitions in the generated Swagger documentation. --- lib/grape-swagger/entity/attribute_parser.rb | 2 +- .../entity/attribute_parser_spec.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/grape-swagger/entity/attribute_parser.rb b/lib/grape-swagger/entity/attribute_parser.rb index b847786..39cc597 100644 --- a/lib/grape-swagger/entity/attribute_parser.rb +++ b/lib/grape-swagger/entity/attribute_parser.rb @@ -6,7 +6,7 @@ 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 + ADDITIONAL_PRIMITIVE_TYPES = %w[boolean string array].freeze def initialize(endpoint) @endpoint = endpoint diff --git a/spec/grape-swagger/entity/attribute_parser_spec.rb b/spec/grape-swagger/entity/attribute_parser_spec.rb index c5686fe..089ab08 100644 --- a/spec/grape-swagger/entity/attribute_parser_spec.rb +++ b/spec/grape-swagger/entity/attribute_parser_spec.rb @@ -262,6 +262,22 @@ 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) { { documentation: { type: Grape::API::Boolean, example: example_value, default: example_value } } } + + 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 } } } From 978ddea860d10e079940652327bcc84617f88a1d Mon Sep 17 00:00:00 2001 From: Andrei Subbota Date: Thu, 8 May 2025 01:24:30 +0200 Subject: [PATCH 2/5] Let rubocop be happy --- spec/grape-swagger/entity/attribute_parser_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/grape-swagger/entity/attribute_parser_spec.rb b/spec/grape-swagger/entity/attribute_parser_spec.rb index 089ab08..3a3241d 100644 --- a/spec/grape-swagger/entity/attribute_parser_spec.rb +++ b/spec/grape-swagger/entity/attribute_parser_spec.rb @@ -263,7 +263,9 @@ def self.to_s end context 'when it is exposed as a Boolean class' do - let(:entity_options) { { documentation: { type: Grape::API::Boolean, example: example_value, default: example_value } } } + 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 } From 686d9a4226eda73ca686f5d79ea8cf74a42b35f1 Mon Sep 17 00:00:00 2001 From: Andrei Subbota Date: Thu, 8 May 2025 01:32:50 +0200 Subject: [PATCH 3/5] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ceabfe..1436668 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ #### Fixes * Your contribution here. +* [#82](https://github.com/ruby-grape/grape-swagger-entity/pull/82): Fix: recognize grape::api::boolean as a primitive type - [@numbata](https://github.com/numbata). ### 0.6.1 (2025/05/06) From 2b3173065ab230a8d487f72bd5cb57ebda469fde Mon Sep 17 00:00:00 2001 From: Andrey Subbota Date: Thu, 8 May 2025 15:23:16 +0200 Subject: [PATCH 4/5] Rely on request primitive check from GrapeSwagger --- lib/grape-swagger/entity/attribute_parser.rb | 6 +----- .../entity/attribute_parser_spec.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/grape-swagger/entity/attribute_parser.rb b/lib/grape-swagger/entity/attribute_parser.rb index 39cc597..9da9e3c 100644 --- a/lib/grape-swagger/entity/attribute_parser.rb +++ b/lib/grape-swagger/entity/attribute_parser.rb @@ -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[boolean string array].freeze - def initialize(endpoint) @endpoint = endpoint end @@ -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) diff --git a/spec/grape-swagger/entity/attribute_parser_spec.rb b/spec/grape-swagger/entity/attribute_parser_spec.rb index 3a3241d..71ada22 100644 --- a/spec/grape-swagger/entity/attribute_parser_spec.rb +++ b/spec/grape-swagger/entity/attribute_parser_spec.rb @@ -295,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 From fa7f11a5550a50a59c932489d56e2d08c8a9db4c Mon Sep 17 00:00:00 2001 From: Andrey Subbota Date: Thu, 8 May 2025 15:24:59 +0200 Subject: [PATCH 5/5] Update CHANGELOG.md file --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1436668..04d4613 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ #### Fixes * Your contribution here. -* [#82](https://github.com/ruby-grape/grape-swagger-entity/pull/82): Fix: recognize grape::api::boolean as a primitive type - [@numbata](https://github.com/numbata). +* [#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)