Skip to content

Commit

Permalink
align array params for post, put request (#553)
Browse files Browse the repository at this point in the history
- adds changelog entry
  • Loading branch information
peter scholz authored Dec 16, 2016
1 parent 8c90a9c commit 17db1b8
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ inherit_from: .rubocop_todo.yml
Metrics/LineLength:
Exclude:
- spec/**/*

Style/IndentHash:
EnforcedStyle: consistent
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* [#546](https://github.com/ruby-grape/grape-swagger/pull/546): Move development dependencies to Gemfile - [@olleolleolle](https://github.com/olleolleolle).
* [#547](https://github.com/ruby-grape/grape-swagger/pull/547): Use entity_name event if type come from a string - [@frodrigo](https://github.com/frodrigo).
* [#548](https://github.com/ruby-grape/grape-swagger/pull/548): Remove dots from operation id - [@frodrigo](https://github.com/frodrigo).
* [#553](https://github.com/ruby-grape/grape-swagger/pull/553): Align array params for post, put request - addition to [#540](https://github.com/ruby-grape/grape-swagger/pull/540) - [@LeFnord](https://github.com/LeFnord).

* Your contribution here.

### 0.25.2 (November 30, 2016)
Expand Down
5 changes: 3 additions & 2 deletions lib/grape-swagger/doc_methods/move_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ def to_definition(params, route, definitions)
@definitions = definitions
unify!(params)

return correct_array_param(params) if should_correct_array?(params)

params_to_move = movable_params(params)

return (params + correct_array_param(params_to_move)) if should_correct_array?(params_to_move)

params << parent_definition_of_params(params_to_move, route)

params
Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def document_array_param(value_type, definitions)
if definitions[value_type[:data_type]]
array_items['$ref'] = "#/definitions/#{@parsed_param[:type]}"
else
array_items[:type] = type || @parsed_param[:type]
array_items[:type] = type || @parsed_param[:type] == 'array' ? 'string' : @parsed_param[:type]
end
array_items[:format] = @parsed_param.delete(:format) if @parsed_param[:format]

Expand Down
2 changes: 0 additions & 2 deletions spec/issues/427_entity_as_string_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'spec_helper'
require 'grape-entity'
require 'grape-swagger-entity'

describe '#427 nested entity given as string' do
let(:app) do
Expand Down
2 changes: 0 additions & 2 deletions spec/issues/430_entity_definitions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'spec_helper'
require 'grape-entity'
require 'grape-swagger-entity'

describe 'definition names' do
before :all do
Expand Down
2 changes: 0 additions & 2 deletions spec/issues/539_array_post_body_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'spec_helper'
require 'grape-entity'
require 'grape-swagger-entity'

describe '#539 post params given as array' do
let(:app) do
Expand Down
2 changes: 0 additions & 2 deletions spec/issues/542_array_of_type_in_post_body_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'spec_helper'
require 'grape-entity'
require 'grape-swagger-entity'

describe '#542 array of type in post params' do
let(:app) do
Expand Down
142 changes: 142 additions & 0 deletions spec/issues/553_align_array_put_post_params_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
require 'spec_helper'

describe '#553 array of type in post/put params' do
let(:app) do
Class.new(Grape::API) do
namespace :in_form_data do
desc 'create foo'
params do
requires :guid, type: Array[String]
end
post do
# your code goes here
end

desc 'put specific foo'
params do
requires :id
requires :guid, type: Array[String]
end
put ':id' do
# your code goes here
end
end

namespace :in_body do
desc 'create foo'
params do
requires :guid, type: Array[String], documentation: { param_type: 'body' }
end
post do
# your code goes here
end

desc 'put specific foo'
params do
requires :id
requires :guid, type: Array[String], documentation: { param_type: 'body' }
end
put ':id' do
# your code goes here
end
end

add_swagger_documentation format: :json
end
end

subject do
get '/swagger_doc'
JSON.parse(last_response.body)
end

describe 'type for Array specified' do
describe 'in formData' do
describe 'post request' do
let(:params) { subject['paths']['/in_form_data']['post']['parameters'] }

specify do
expect(params).to eql([{
'in' => 'formData',
'name' => 'guid',
'type' => 'array',
'items' => { 'type' => 'string' },
'required' => true
}])
end
end

describe 'put request' do
let(:params) { subject['paths']['/in_form_data/{id}']['put']['parameters'] }

specify do
expect(params).to eql(
[
{
'in' => 'path',
'name' => 'id',
'type' => 'string',
'required' => true
},
{
'in' => 'formData',
'name' => 'guid',
'type' => 'array',
'items' => { 'type' => 'string' },
'required' => true
}
]
)
end
end
end

describe 'in body' do
describe 'post request' do
let(:params) { subject['paths']['/in_body']['post']['parameters'] }

specify do
expect(params).to eql(
[
{
'in' => 'body',
'name' => 'guid',
'required' => true,
'schema' => {
'type' => 'array',
'items' => { 'type' => 'string' }
}
}
]
)
end
end

describe 'put request' do
let(:params) { subject['paths']['/in_body/{id}']['put']['parameters'] }

specify do
expect(params).to eql(
[
{
'in' => 'path',
'name' => 'id',
'type' => 'string',
'required' => true
},
{
'in' => 'body',
'name' => 'guid',
'required' => true,
'schema' => {
'type' => 'array',
'items' => { 'type' => 'string' }
}
}
]
)
end
end
end
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
require "grape-swagger/#{MODEL_PARSER}" if MODEL_PARSER != 'mock'
require File.join(Dir.getwd, "spec/support/model_parsers/#{MODEL_PARSER}_parser.rb")

require 'grape-entity'
require 'grape-swagger-entity'

Bundler.setup :default, :test

require 'rack'
Expand Down

0 comments on commit 17db1b8

Please sign in to comment.