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 minimum and maximum #69

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Metrics/BlockLength:
# Configuration parameters: CountComments, Max, CountAsOne.
Metrics/ClassLength:
Exclude:
- 'lib/grape-swagger/entity/attribute_parser.rb'
Copy link
Contributor Author

@storey storey Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not ideal but when I split up this file into two files I found that there were a bunch of existing MethodLength violations within it that we were ignoring, so I figured I would just keep the status quo of the file not being fully rubocop compliant?

Let me know if we should just refactor that whole file to be compliant (and pull the param parser out) in a separate PR

- 'lib/grape-swagger/entity/parser.rb'

# Offense count: 2
Expand Down
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

* [#69](https://github.com/ruby-grape/grape-swagger-entity/pull/67): Add support for minimum and maximum - [@storey](https://github.com/storey).
* Your contribution here.

#### Fixes
Expand Down
9 changes: 9 additions & 0 deletions lib/grape-swagger/entity/attribute_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ def add_attribute_sample(attribute, hash, key)
end

def add_attribute_documentation(param, documentation)
param[:minimum] = documentation[:minimum] if documentation.key?(:minimum)
param[:maximum] = documentation[:maximum] if documentation.key?(:maximum)

values = documentation[:values]
if values&.is_a?(Range)
param[:minimum] = values.begin if values.begin.is_a?(Numeric)
param[:maximum] = values.end if values.end.is_a?(Numeric)
end

param[:minLength] = documentation[:min_length] if documentation.key?(:min_length)
param[:maxLength] = documentation[:max_length] if documentation.key?(:max_length)
end
Expand Down
102 changes: 102 additions & 0 deletions spec/grape-swagger/entity/attribute_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,115 @@
it { is_expected.to include(maxLength: 1) }
end

context 'when it contains values array' do
let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', values: ['red', 'blue'] } } }

it { is_expected.to_not include('minimum') }
it { is_expected.to_not include('maximum') }
end

context 'when it contains values range' do
let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', values: 'a'...'c' } } }

it { is_expected.to_not include('minimum') }
it { is_expected.to_not include('maximum') }
end

context 'when it contains extensions' do
let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', x: { some: 'stuff' } } } }

it { is_expected.to include('x-some' => 'stuff') }
end
end

context 'when it is exposed as a number' do
let(:entity_options) { { documentation: { type: 'number', desc: 'Solution pH' } } }

it { is_expected.to include(type: 'number') }

context 'when it contains minimum' do
let(:entity_options) { { documentation: { type: 'number', desc: 'Solution pH', minimum: 2.5 } } }

it { is_expected.to include(minimum: 2.5) }
end

context 'when it contains maximum' do
let(:entity_options) { { documentation: { type: 'number', desc: 'Solution pH', maximum: 9.1 } } }

it { is_expected.to include(maximum: 9.1) }
end

context 'when it contains values array' do
let(:entity_options) { { documentation: { type: 'number', desc: 'Solution pH', values: [6.0, 7.0, 8.0] } } }

it { is_expected.to_not include('minimum') }
it { is_expected.to_not include('maximum') }
end

context 'when it contains values range' do
let(:entity_options) { { documentation: { type: 'number', desc: 'Solution pH', values: 0.0..14.0 } } }

it { is_expected.to include(minimum: 0.0, maximum: 14.0) }
end

context 'when it contains values range with no minimum' do
let(:entity_options) { { documentation: { type: 'number', desc: 'Solution pH', values: ..14.0 } } }

it { is_expected.to_not include('minimum') }
it { is_expected.to include(maximum: 14.0) }
end

context 'when it contains values range with no maximum' do
let(:entity_options) { { documentation: { type: 'number', desc: 'Solution pH', values: 0.0.. } } }

it { is_expected.to_not include('maximum') }
it { is_expected.to include(minimum: 0.0) }
end

context 'when it contains extensions' do
let(:entity_options) { { documentation: { type: 'number', desc: 'Solution pH', x: { some: 'stuff' } } } }

it { is_expected.to include('x-some' => 'stuff') }
end
end

context 'when it is exposed as an integer' do
let(:entity_options) { { documentation: { type: 'integer', desc: 'Count' } } }

it { is_expected.to include(type: 'integer') }

context 'when it contains minimum' do
let(:entity_options) { { documentation: { type: 'integer', desc: 'Count', minimum: 2 } } }

it { is_expected.to include(minimum: 2) }
end

context 'when it contains maximum' do
let(:entity_options) { { documentation: { type: 'integer', desc: 'Count', maximum: 100 } } }

it { is_expected.to include(maximum: 100) }
end

context 'when it contains values array' do
let(:entity_options) { { documentation: { type: 'integer', desc: 'Count', values: 1..10 } } }

it { is_expected.to_not include('minimum') }
it { is_expected.to_not include('maximum') }
end

context 'when it contains values range' do
let(:entity_options) { { documentation: { type: 'integer', desc: 'Count', values: 1..10 } } }

it { is_expected.to include(minimum: 1, maximum: 10) }
end

context 'when it contains extensions' do
let(:entity_options) { { documentation: { type: 'integer', desc: 'Count', x: { some: 'stuff' } } } }

it { is_expected.to include('x-some' => 'stuff') }
end
end

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

Expand Down
Loading