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

Add support for minItems, maxItems and uniqueItems #37

Merged
merged 5 commits into from
Nov 26, 2018
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 @@ -2,6 +2,7 @@

#### Features

* [#37](https://github.com/ruby-grape/grape-swagger-entity/pull/37): Add support for minItems, maxItems and uniqueItems - [@fotos](https://github.com/fotos).
* Your contribution here.

#### Fixes
Expand Down
20 changes: 18 additions & 2 deletions lib/grape-swagger/entity/attribute_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ 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

add_array_documentation(entity_model_type, documentation) if documentation[:is_array]

entity_model_type
else
param = data_type_from(entity_options)
return param unless documentation
Expand All @@ -25,7 +31,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 }
add_array_documentation(param, documentation)
end

param
end
end
Expand Down Expand Up @@ -98,6 +108,12 @@ def add_attribute_example(attribute, example)

attribute[:example] = example.is_a?(Proc) ? example.call : example
end

def add_array_documentation(param, documentation)
param[:minItems] = documentation[:min_items] if documentation.key?(:min_items)
param[:maxItems] = documentation[:max_items] if documentation.key?(:max_items)
param[:uniqueItems] = documentation[:unique_items] if documentation.key?(:unique_items)
end
end
end
end
80 changes: 80 additions & 0 deletions spec/grape-swagger/entity/attribute_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'spec_helper'
require_relative '../../../spec/support/shared_contexts/this_api'

describe GrapeSwagger::Entity::AttributeParser do
include_context 'this api'

describe '#call' do
let(:endpoint) {}

subject { described_class.new(endpoint).call(entity_options) }

context 'when the entity is a model' do
context 'when it is exposed as an array' do
let(:entity_options) { { using: ThisApi::Entities::Tag, documentation: { is_array: true } } }

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

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

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

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

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

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

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

context 'when it is not exposed as an array' do
let(:entity_options) { { using: ThisApi::Entities::Kind, documentation: { type: 'ThisApi::Kind', desc: 'The kind of this something.' } } }

it { is_expected.to_not include('type') }
it { is_expected.to include('$ref' => '#/definitions/Kind') }
end
end

context 'when the entity is not a model' do
context 'when it is exposed as an array' do
let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', is_array: true } } }

it { is_expected.to include(type: :array) }
it { is_expected.to include(items: { type: 'string' }) }

context 'when it contains min_items' do
let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', is_array: true, min_items: 1 } } }

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

context 'when it contains max_items' do
let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', is_array: true, max_items: 1 } } }

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

context 'when it contains unique_items' do
let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', is_array: true, unique_items: true } } }

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

context 'when it is not exposed as an array' do
let(:entity_options) { { documentation: { type: 'string', desc: 'Content of something.' } } }

it { is_expected.to include(type: 'string') }
it { is_expected.to_not include('$ref') }
end
end
end
end