Skip to content
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

Properly render example for array exposure done using another entity #68

Merged
merged 4 commits into from
Apr 14, 2024
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

* [#67](https://github.com/ruby-grape/grape-swagger-entity/pull/67): Various build updates - [@mscrivo](https://github.com/mscrivo).
* [#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-).
* Your contribution here.

### 0.5.3 (2024/02/02)
Expand Down
58 changes: 28 additions & 30 deletions lib/grape-swagger/entity/attribute_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,30 @@ def initialize(endpoint)
end

def call(entity_options)
documentation = entity_options[:documentation]
entity_model = model_from(entity_options)

if entity_model
name = GrapeSwagger::Entity::Helper.model_name(entity_model, endpoint)

entity_model_type = entity_model_type(name, entity_options)
return entity_model_type unless documentation

add_extension_documentation(entity_model_type, documentation)
add_array_documentation(entity_model_type, documentation) if documentation[:is_array]
param = if (entity_model = model_from(entity_options))
name = GrapeSwagger::Entity::Helper.model_name(entity_model, endpoint)
entity_model_type(name, entity_options)
else
data_type_from(entity_options)
end

entity_model_type
else
param = data_type_from(entity_options)
return param unless documentation
documentation = entity_options[:documentation]
return param if documentation.nil?

if (values = documentation[:values]) && values.is_a?(Array)
param[:enum] = values
end
if (values = documentation[:values]) && values.is_a?(Array)
param[:enum] = values
end

if documentation[:is_array]
param = { type: :array, items: param }
add_array_documentation(param, documentation)
end
add_array_documentation(param, documentation) if documentation[:is_array]

add_attribute_sample(param, documentation, :default)
add_attribute_sample(param, documentation, :example)
add_attribute_sample(param, documentation, :default)
add_attribute_sample(param, documentation, :example)

add_attribute_documentation(param, documentation)
add_attribute_documentation(param, documentation)

add_extension_documentation(param, documentation)
add_discriminator_extension(param, documentation)
param
end
add_extension_documentation(param, documentation)
add_discriminator_extension(param, documentation)
param
end

private
Expand Down Expand Up @@ -79,7 +68,16 @@ def data_type_from(documentation)

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

document_data_type(documentation[:documentation], data_type)
documented_data_type = document_data_type(documentation[:documentation], data_type)

if documentation[:documentation] && documentation[:documentation][:is_array]
{
type: :array,
items: documented_data_type
}
else
documented_data_type
end
end

def document_data_type(documentation, data_type)
Expand Down
17 changes: 17 additions & 0 deletions spec/grape-swagger/entity/attribute_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@
it { is_expected.to include('type' => 'array') }
it { is_expected.to include('items' => { '$ref' => '#/definitions/Tag' }) }

context 'when it contains example' do
let(:entity_options) do
{
using: ThisApi::Entities::Tag,
documentation: {
is_array: true,
example: [
{ name: 'green' },
{ name: 'blue' }
]
}
}
end

it { is_expected.to include(example: %w[green blue].map { { name: _1 } }) }
end

context 'when it contains min_items' do
let(:entity_options) { { using: ThisApi::Entities::Tag, documentation: { is_array: true, min_items: 1 } } }

Expand Down