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

Fixed the range :values options, now it does not fail with float range #234

Merged
merged 1 commit into from
Mar 18, 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
16 changes: 8 additions & 8 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2015-03-13 06:55:39 -0400 using RuboCop version 0.27.0.
# on 2015-03-18 19:06:42 +0300 using RuboCop version 0.27.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 10
# Offense count: 11
Metrics/AbcSize:
Max: 360
Max: 356

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 496
Max: 504

# Offense count: 6
Metrics/CyclomaticComplexity:
Max: 106
Max: 102

# Offense count: 300
# Offense count: 303
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 254

# Offense count: 20
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 377
Max: 376

# Offense count: 5
Metrics/PerceivedComplexity:
Max: 108
Max: 104

# 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 @@ -14,4 +14,5 @@ env:
- GRAPE_VERSION=0.9.0
- GRAPE_VERSION=0.10.0
- GRAPE_VERSION=0.10.1
- GRAPE_VERSION=0.11.0
- GRAPE_VERSION=HEAD
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#### Fixes

* [#232](https://github.com/tim-vandecasteele/grape-swagger/pull/232): Fixed missing raw array params - [@u2](https://github.com/u2).
* [#234](https://github.com/tim-vandecasteele/grape-swagger/pull/234): Fixed range :values with float - [@azhi](https://github.com/azhi).

### 0.10.1 (March 11, 2015)

Expand Down
15 changes: 12 additions & 3 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ def parse_array_params(params)
modified_params
end

def parse_enum_values(values)
if values.is_a?(Range) && [Integer, String].any? { |klass| values.first.is_a?(klass) }
values.to_a
elsif values.is_a?(Proc)
values.call
else
values
end
end

def create_documentation_class
Class.new(Grape::API) do
class << self
Expand Down Expand Up @@ -214,9 +224,8 @@ def parse_params(params, path, method)
default_value = value.is_a?(Hash) ? value[:default] : nil
example = value.is_a?(Hash) ? value[:example] : 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)
values = value.is_a?(Hash) ? value[:values] : nil
enum_values = parse_enum_values(values)

if value.is_a?(Hash) && value.key?(:documentation) && value[:documentation].key?(:param_type)
param_type = value[:documentation][:param_type]
Expand Down
130 changes: 130 additions & 0 deletions spec/param_values_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
require 'spec_helper'
require 'grape_version'

describe 'Convert values to enum' do
def app
Class.new(Grape::API) do
format :json

params do
requires :letter, type: String, values: %w(a b c)
end
post :plain_array do
end

params do
requires :letter, type: String, values: proc { %w(d e f) }
end
post :array_in_proc do
end

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

params do
requires :integer, type: Integer, values: -5..5
end
post :range_integer do
end

add_swagger_documentation
end
end

def first_parameter_info(request)
get "/swagger_doc/#{request}"
expect(last_response.status).to eq 200
body = JSON.parse last_response.body
body['apis'].first['operations'].first['parameters']
end

context 'Plain array values' do
subject(:plain_array) { first_parameter_info('plain_array') }

it 'has values as array in enum' do
expect(plain_array).to eq [
{ 'paramType' => 'form', 'name' => 'letter', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false, 'enum' => %w(a b c) }
]
end
end

context 'Array in proc values' do
subject(:array_in_proc) { first_parameter_info('array_in_proc') }

it 'has proc returned values as array in enum' do
expect(array_in_proc).to eq [
{ 'paramType' => 'form', 'name' => 'letter', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false, 'enum' => %w(d e f) }
]
end
end

context 'Range values' do
subject(:range_letter) { first_parameter_info('range_letter') }

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

subject(:range_integer) { first_parameter_info('range_integer') }

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

describe 'Convert values to enum for float range and not arrays inside a proc', if: GrapeVersion.satisfy?('>= 0.11.0') do
def app
Class.new(Grape::API) do
format :json

params do
requires :letter, type: String, values: proc { 'string' }
end
post :non_array_in_proc do
end

params do
requires :float, type: Float, values: -5.0..5.0
end
post :range_float do
end

add_swagger_documentation
end
end

def first_parameter_info(request)
get "/swagger_doc/#{request}"
expect(last_response.status).to eq 200
body = JSON.parse last_response.body
body['apis'].first['operations'].first['parameters']
end

context 'Non array in proc values' do
subject(:non_array_in_proc) { first_parameter_info('non_array_in_proc') }

it 'has proc returned value as string in enum' do
expect(non_array_in_proc).to eq [
{ 'paramType' => 'form', 'name' => 'letter', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false, 'enum' => 'string' }
]
end
end

context 'Range values' do
subject(:range_float) { first_parameter_info('range_float') }

it 'has float range values as string in enum' do
expect(range_float).to eq [
{ 'paramType' => 'form', 'name' => 'float', 'description' => nil, 'type' => 'float', 'required' => true, 'allowMultiple' => false, 'enum' => '-5.0..5.0' }
]
end
end
end
49 changes: 0 additions & 49 deletions spec/range_values_spec.rb

This file was deleted.

11 changes: 11 additions & 0 deletions spec/support/grape_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class GrapeVersion
class << self
def current_version
Grape::VERSION
end

def satisfy?(requirement)
Gem::Dependency.new('grape-test', requirement).match?('grape-test', current_version)
end
end
end