Skip to content

Commit 4836546

Browse files
authored
Merge pull request #71 from Jell/fix-enum-values-of-array-attributes
Fix enum values of array attributes
2 parents 11b2dcd + a5ee701 commit 4836546

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

99
* Your contribution here.
10+
* [#71](https://github.com/ruby-grape/grape-swagger-entity/pull/71): Fix regression for enum values in array attributes - [@Jell](https://github.com/Jell).
1011

1112
### 0.5.4 (2024/04/19)
1213

lib/grape-swagger/entity/attribute_parser.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ def call(entity_options)
2020
documentation = entity_options[:documentation]
2121
return param if documentation.nil?
2222

23-
if (values = documentation[:values]) && values.is_a?(Array)
24-
param[:enum] = values
25-
end
26-
2723
add_array_documentation(param, documentation) if documentation[:is_array]
2824

2925
add_attribute_sample(param, documentation, :default)
@@ -62,15 +58,15 @@ def ambiguous_model_type?(type)
6258
!type == Array
6359
end
6460

65-
def data_type_from(documentation)
66-
documented_type = documentation[:type]
67-
documented_type ||= documentation[:documentation] && documentation[:documentation][:type]
61+
def data_type_from(entity_options)
62+
documentation = entity_options[:documentation] || {}
63+
documented_type = entity_options[:type] || documentation[:type]
6864

6965
data_type = GrapeSwagger::DocMethods::DataType.call(documented_type)
7066

71-
documented_data_type = document_data_type(documentation[:documentation], data_type)
67+
documented_data_type = document_data_type(documentation, data_type)
7268

73-
if documentation[:documentation] && documentation[:documentation][:is_array]
69+
if documentation[:is_array]
7470
{
7571
type: :array,
7672
items: documented_data_type
@@ -87,7 +83,12 @@ def document_data_type(documentation, data_type)
8783
else
8884
{ type: data_type }
8985
end
90-
type[:format] = documentation[:format] if documentation&.key?(:format)
86+
87+
type[:format] = documentation[:format] if documentation.key?(:format)
88+
89+
if (values = documentation[:values]) && values.is_a?(Array)
90+
type[:enum] = values
91+
end
9192

9293
type
9394
end

spec/grape-swagger/entity/attribute_parser_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@
227227

228228
it { is_expected.to include(uniqueItems: true) }
229229
end
230+
231+
context 'when it contains values array' do
232+
let(:entity_options) do
233+
{ documentation: { type: 'string', desc: 'Colors', is_array: true, values: %w[red blue] } }
234+
end
235+
236+
it { is_expected.to eq(type: :array, items: { type: 'string', enum: %w[red blue] }) }
237+
end
230238
end
231239

232240
context 'when it is not exposed as an array' do

0 commit comments

Comments
 (0)