Skip to content

Fixed the range :values option, now exposed as enum parameters #210

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

Merged
merged 1 commit into from
Feb 3, 2015
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
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

# Offense count: 8
Metrics/AbcSize:
Max: 324
Max: 327

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 399
Max: 400

# Offense count: 5
Metrics/CyclomaticComplexity:
Max: 93
Max: 95

# Offense count: 220
# Configuration parameters: AllowURI, URISchemes.
Expand All @@ -26,11 +26,11 @@ Metrics/LineLength:
# Offense count: 13
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 360
Max: 361

# Offense count: 4
Metrics/PerceivedComplexity:
Max: 94
Max: 96

# Offense count: 8
Style/ClassVars:
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ env:
- GRAPE_VERSION=0.8.0
- GRAPE_VERSION=0.9.0
- GRAPE_VERSION=0.10.0
- GRAPE_VERSION=0.10.1
- GRAPE_VERSION=HEAD
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### Next Release
* [#210](https://github.com/tim-vandecasteele/grape-swagger/pull/210): Fixed the range `:values` option, now exposed as `enum` parameters - [@u2](https://github.com/u2).
* [#208](https://github.com/tim-vandecasteele/grape-swagger/pull/208): Fixed `Float` parameters, exposed as Swagger `float` types - [@u2](https://github.com/u2).
* [#196](https://github.com/tim-vandecasteele/grape-swagger/pull/196): If `:type` is omitted, see if it's available in `:using` - [@jhollinger](https://github.com/jhollinger).
* [#200](https://github.com/tim-vandecasteele/grape-swagger/pull/200): Treat `type: Symbol` as string form parameter - [@ypresto](https://github.com/ypresto).
Expand Down
1 change: 1 addition & 0 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def parse_params(params, path, method)
default_value = value.is_a?(Hash) ? value[:default] : nil
is_array = value.is_a?(Hash) ? (value[:is_array] || false) : false
enum_values = value.is_a?(Hash) ? value[:values] : nil
enum_values = enum_values.to_a if enum_values && enum_values.is_a?(Range)
enum_values = enum_values.call if enum_values && enum_values.is_a?(Proc)

if value.is_a?(Hash) && value.key?(:param_type)
Expand Down
50 changes: 50 additions & 0 deletions spec/range_values_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'spec_helper'

describe 'Range Params' do
def app
Class.new(Grape::API) do
format :json

params do
requires :letter, type: Virtus::Attribute::String, values: 'a'..'z'
end
post :letter do
end

params do
requires :number, type: Virtus::Attribute::Integer, values: -5..5
end
post :integer do
end

add_swagger_documentation
end
end

subject(:letter) do
get '/swagger_doc/letter'
expect(last_response.status).to eq 200
body = JSON.parse last_response.body
body['apis'].first['operations'].first['parameters']
end

it 'has letter range values' do
expect(letter).to eq [
{ 'paramType' => 'form', 'name' => 'letter', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false, 'enum' => ('a'..'z').to_a }
]
end

subject(:number) do
get '/swagger_doc/integer'
expect(last_response.status).to eq 200
body = JSON.parse last_response.body
body['apis'].first['operations'].first['parameters']
end

it 'has number range values' do
expect(number).to eq [
{ 'paramType' => 'form', 'name' => 'number', 'description' => nil, 'type' => 'integer', 'required' => true, 'allowMultiple' => false, 'format' => 'int32', 'enum' => (-5..5).to_a }
]
end

end