From ee3f77572de30a18dc9dd53ad04ef4c022520943 Mon Sep 17 00:00:00 2001 From: Fotos Georgiadis Date: Fri, 23 Nov 2018 12:33:07 +0100 Subject: [PATCH] Add support for minItems, maxItems and uniqueItems for primitive arrays --- lib/grape-swagger/entity/attribute_parser.rb | 6 +++++- .../entity/attribute_parser_spec.rb | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/grape-swagger/entity/attribute_parser.rb b/lib/grape-swagger/entity/attribute_parser.rb index 5885e8a..21dd5b7 100644 --- a/lib/grape-swagger/entity/attribute_parser.rb +++ b/lib/grape-swagger/entity/attribute_parser.rb @@ -35,7 +35,11 @@ def call(entity_options) param[:enum] = values if values.is_a?(Array) end - param = { type: :array, items: param } if documentation[:is_array] + if documentation[:is_array] + param = { type: :array, items: param } + param.merge!(documentation.slice(:minItems, :maxItems, :uniqueItems)) + end + param end end diff --git a/spec/grape-swagger/entity/attribute_parser_spec.rb b/spec/grape-swagger/entity/attribute_parser_spec.rb index 1068117..1a3eb30 100644 --- a/spec/grape-swagger/entity/attribute_parser_spec.rb +++ b/spec/grape-swagger/entity/attribute_parser_spec.rb @@ -49,6 +49,24 @@ it { is_expected.to include(type: :array) } it { is_expected.to include(items: { type: 'string' }) } + + context 'when it contains minItems' do + let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', is_array: true, minItems: 1 } } } + + it { is_expected.to include(minItems: 1) } + end + + context 'when it contains minItems' do + let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', is_array: true, maxItems: 1 } } } + + it { is_expected.to include(maxItems: 1) } + end + + context 'when it contains minItems' do + let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', is_array: true, uniqueItems: true } } } + + it { is_expected.to include(uniqueItems: true) } + end end context 'when it is not exposed as an array' do