Skip to content

Commit 2615fad

Browse files
authored
Properly render example for array exposure done using another entity (#68)
* Properly render example for arrays of subresources * Move array typing to data_type_from This is so it matches waht happens in entity_model_type. The main difference is we use Symbols (both keys and values) here, whereas we use Strings there. I'm not sure the difference is intentional, but I didn't want to introduce any actual changes in this commit. * DRY up AttributeParser#call The reason I'm opening this PR is because logic that was present for "standard" exposures was accidentally left out from "using: Entity..." exposures". The point of this commit is to hopefully prevent such omissions from being made again.
1 parent 29b9619 commit 2615fad

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
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
* [#67](https://github.com/ruby-grape/grape-swagger-entity/pull/67): Various build updates - [@mscrivo](https://github.com/mscrivo).
10+
* [#68](https://github.com/ruby-grape/grape-swagger-entity/pull/68): Properly render `example` for array exposure done using another entity - [@magni-](https://github.com/magni-).
1011
* Your contribution here.
1112

1213
### 0.5.3 (2024/02/02)

lib/grape-swagger/entity/attribute_parser.rb

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,30 @@ def initialize(endpoint)
1010
end
1111

1212
def call(entity_options)
13-
documentation = entity_options[:documentation]
14-
entity_model = model_from(entity_options)
15-
16-
if entity_model
17-
name = GrapeSwagger::Entity::Helper.model_name(entity_model, endpoint)
18-
19-
entity_model_type = entity_model_type(name, entity_options)
20-
return entity_model_type unless documentation
21-
22-
add_extension_documentation(entity_model_type, documentation)
23-
add_array_documentation(entity_model_type, documentation) if documentation[:is_array]
13+
param = if (entity_model = model_from(entity_options))
14+
name = GrapeSwagger::Entity::Helper.model_name(entity_model, endpoint)
15+
entity_model_type(name, entity_options)
16+
else
17+
data_type_from(entity_options)
18+
end
2419

25-
entity_model_type
26-
else
27-
param = data_type_from(entity_options)
28-
return param unless documentation
20+
documentation = entity_options[:documentation]
21+
return param if documentation.nil?
2922

30-
if (values = documentation[:values]) && values.is_a?(Array)
31-
param[:enum] = values
32-
end
23+
if (values = documentation[:values]) && values.is_a?(Array)
24+
param[:enum] = values
25+
end
3326

34-
if documentation[:is_array]
35-
param = { type: :array, items: param }
36-
add_array_documentation(param, documentation)
37-
end
27+
add_array_documentation(param, documentation) if documentation[:is_array]
3828

39-
add_attribute_sample(param, documentation, :default)
40-
add_attribute_sample(param, documentation, :example)
29+
add_attribute_sample(param, documentation, :default)
30+
add_attribute_sample(param, documentation, :example)
4131

42-
add_attribute_documentation(param, documentation)
32+
add_attribute_documentation(param, documentation)
4333

44-
add_extension_documentation(param, documentation)
45-
add_discriminator_extension(param, documentation)
46-
param
47-
end
34+
add_extension_documentation(param, documentation)
35+
add_discriminator_extension(param, documentation)
36+
param
4837
end
4938

5039
private
@@ -79,7 +68,16 @@ def data_type_from(documentation)
7968

8069
data_type = GrapeSwagger::DocMethods::DataType.call(documented_type)
8170

82-
document_data_type(documentation[:documentation], data_type)
71+
documented_data_type = document_data_type(documentation[:documentation], data_type)
72+
73+
if documentation[:documentation] && documentation[:documentation][:is_array]
74+
{
75+
type: :array,
76+
items: documented_data_type
77+
}
78+
else
79+
documented_data_type
80+
end
8381
end
8482

8583
def document_data_type(documentation, data_type)

spec/grape-swagger/entity/attribute_parser_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@
1818
it { is_expected.to include('type' => 'array') }
1919
it { is_expected.to include('items' => { '$ref' => '#/definitions/Tag' }) }
2020

21+
context 'when it contains example' do
22+
let(:entity_options) do
23+
{
24+
using: ThisApi::Entities::Tag,
25+
documentation: {
26+
is_array: true,
27+
example: [
28+
{ name: 'green' },
29+
{ name: 'blue' }
30+
]
31+
}
32+
}
33+
end
34+
35+
it { is_expected.to include(example: %w[green blue].map { { name: _1 } }) }
36+
end
37+
2138
context 'when it contains min_items' do
2239
let(:entity_options) { { using: ThisApi::Entities::Tag, documentation: { is_array: true, min_items: 1 } } }
2340

0 commit comments

Comments
 (0)