diff --git a/.rubocop.yml b/.rubocop.yml index 872e72b9..06d3b69b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -8,3 +8,6 @@ inherit_from: .rubocop_todo.yml Metrics/LineLength: Exclude: - spec/**/* + +Style/IndentHash: + EnforcedStyle: consistent diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d01b5f7..2b6f8940 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/grape-swagger/doc_methods/move_params.rb b/lib/grape-swagger/doc_methods/move_params.rb index 70529300..a8bdd10e 100644 --- a/lib/grape-swagger/doc_methods/move_params.rb +++ b/lib/grape-swagger/doc_methods/move_params.rb @@ -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 diff --git a/lib/grape-swagger/doc_methods/parse_params.rb b/lib/grape-swagger/doc_methods/parse_params.rb index 80119d6c..0edad04f 100644 --- a/lib/grape-swagger/doc_methods/parse_params.rb +++ b/lib/grape-swagger/doc_methods/parse_params.rb @@ -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] diff --git a/spec/issues/427_entity_as_string_spec.rb b/spec/issues/427_entity_as_string_spec.rb index c8344c22..3ab2d75b 100644 --- a/spec/issues/427_entity_as_string_spec.rb +++ b/spec/issues/427_entity_as_string_spec.rb @@ -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 diff --git a/spec/issues/430_entity_definitions_spec.rb b/spec/issues/430_entity_definitions_spec.rb index 1d5ddc8a..7ed2cc78 100644 --- a/spec/issues/430_entity_definitions_spec.rb +++ b/spec/issues/430_entity_definitions_spec.rb @@ -1,6 +1,4 @@ require 'spec_helper' -require 'grape-entity' -require 'grape-swagger-entity' describe 'definition names' do before :all do diff --git a/spec/issues/539_array_post_body_spec.rb b/spec/issues/539_array_post_body_spec.rb index 25f9d456..9f022255 100644 --- a/spec/issues/539_array_post_body_spec.rb +++ b/spec/issues/539_array_post_body_spec.rb @@ -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 diff --git a/spec/issues/542_array_of_type_in_post_body_spec.rb b/spec/issues/542_array_of_type_in_post_body_spec.rb index c002d7a8..3b9a2f29 100644 --- a/spec/issues/542_array_of_type_in_post_body_spec.rb +++ b/spec/issues/542_array_of_type_in_post_body_spec.rb @@ -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 diff --git a/spec/issues/553_align_array_put_post_params_spec.rb b/spec/issues/553_align_array_put_post_params_spec.rb new file mode 100644 index 00000000..4ae6a2ac --- /dev/null +++ b/spec/issues/553_align_array_put_post_params_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b24c0e9b..be1acabb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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'