Skip to content

Commit

Permalink
Add support for minItems, maxItems and uniqueItems for entity arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
fotos committed Nov 23, 2018
1 parent 8a648dc commit 3d21cd5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/grape-swagger/entity/attribute_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ def call(entity_options)

if entity_model
name = endpoint.nil? ? entity_model.to_s.demodulize : endpoint.send(:expose_params_from_model, entity_model)
return entity_model_type(name, entity_options)

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

if documentation[:is_array]
entity_model_type.merge!(
documentation.slice(:minItems, :maxItems, :uniqueItems)
)
end

entity_model_type
else
param = data_type_from(entity_options)
return param unless documentation
Expand Down
18 changes: 18 additions & 0 deletions spec/grape-swagger/entity/attribute_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@

it { is_expected.to include('type' => 'array') }
it { is_expected.to include('items' => { '$ref' => '#/definitions/Tag' }) }

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

it { is_expected.to include(minItems: 1) }
end

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

it { is_expected.to include(maxItems: 1) }
end

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

it { is_expected.to include(uniqueItems: true) }
end
end

context 'when it is not exposed as an array' do
Expand Down

0 comments on commit 3d21cd5

Please sign in to comment.