From 0b63a5bfbc0e23a0a9068cb418bf412c80d59138 Mon Sep 17 00:00:00 2001 From: Josh Murphy Date: Tue, 17 Jul 2018 09:35:30 -0500 Subject: [PATCH 1/3] Update prepare_nested_types to allow for references and update specs --- lib/grape-swagger/doc_methods/move_params.rb | 11 +- spec/lib/move_params_spec.rb | 188 +++++++++++++++++++ 2 files changed, 197 insertions(+), 2 deletions(-) diff --git a/lib/grape-swagger/doc_methods/move_params.rb b/lib/grape-swagger/doc_methods/move_params.rb index 2a8c36c5..e888507d 100644 --- a/lib/grape-swagger/doc_methods/move_params.rb +++ b/lib/grape-swagger/doc_methods/move_params.rb @@ -177,9 +177,16 @@ def object_type def prepare_nested_types(params) params.each do |param| next unless param[:items] - param[:type] = param[:items][:type] == 'array' ? 'string' : param[:items][:type] + + param[:type] = if param[:items][:type] == 'array' + 'string' + elsif param[:items].keys.include?('$ref') + param[:type] = 'object' + else + param[:items][:type] + end param[:format] = param[:items][:format] if param[:items][:format] - param.delete(:items) + param.delete(:items) if param[:type] != 'object' end end diff --git a/spec/lib/move_params_spec.rb b/spec/lib/move_params_spec.rb index 92bf06cc..a7b28455 100644 --- a/spec/lib/move_params_spec.rb +++ b/spec/lib/move_params_spec.rb @@ -325,5 +325,193 @@ it { expect(params).to eql expected_params } end end + + describe 'prepare_nested_types' do + before :each do + subject.send(:prepare_nested_types, params) + end + + let(:params) do + [ + { + in: 'body', + name: 'address[street_lines]', + description: 'street lines', + type: 'array', + items: { + type: 'string' + }, + required: true + } + ] + end + + context 'when params contains nothing with :items key' do + let(:params) do + [ + { + in: 'body', + name: 'phone_number', + description: 'phone number', + type: 'string', + required: true + } + ] + end + + let(:expected_params) do + [ + { + in: 'body', + name: 'phone_number', + description: 'phone number', + type: 'string', + required: true + } + ] + end + + it 'does nothing' do + expect(params).to eq expected_params + end + end + + context 'when params contains :items key with array type' do + let(:params) do + [ + { + in: 'body', + name: 'address_street_lines', + description: 'street lines', + type: 'array', + items: { + type: 'array' + }, + required: true + } + ] + end + + let(:expected_params) do + [ + { + in: 'body', + name: 'address_street_lines', + description: 'street lines', + type: 'string', + required: true + } + ] + end + + it 'sets type to string and removes :items' do + expect(params).to eq expected_params + end + end + + context 'when params contains :items key with $ref' do + let(:params) do + [ + { + in: 'body', + name: 'address_street_lines', + description: 'street lines', + type: 'array', + items: { + '$ref' => '#/definitions/StreetLine' + }, + required: true + } + ] + end + + let(:expected_params) do + [ + { + in: 'body', + name: 'address_street_lines', + description: 'street lines', + type: 'object', + items: { + '$ref' => '#/definitions/StreetLine' + }, + required: true + } + ] + end + + it 'sets type to object and does not remove :items' do + expect(params).to eq expected_params + end + end + + context 'when params contains :items without $ref or array type' do + let(:params) do + [ + { + in: 'body', + name: 'address_street_lines', + description: 'street lines', + type: 'array', + items: { + type: 'string' + }, + required: true + } + ] + end + + let(:expected_params) do + [ + { + in: 'body', + name: 'address_street_lines', + description: 'street lines', + type: 'string', + required: true + } + ] + end + + it 'sets type to :items :type and removes :items' do + expect(params).to eq expected_params + end + end + + context 'when params contains :items key with :format' do + let(:params) do + [ + { + in: 'body', + name: 'street_number', + description: 'street number', + type: 'array', + items: { + type: 'integer', + format: 'int32' + }, + required: true + } + ] + end + + let(:expected_params) do + [ + { + in: 'body', + name: 'street_number', + description: 'street number', + type: 'integer', + format: 'int32', + required: true + } + ] + end + + it 'sets format and removes :items' do + expect(params).to eq expected_params + end + end + end end end From d502274a49abb72b555d8dd0cd6203915b6c6984 Mon Sep 17 00:00:00 2001 From: Josh Murphy Date: Tue, 17 Jul 2018 09:51:17 -0500 Subject: [PATCH 2/3] Add CHANGELOG entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e95d148f..7ffde049 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ #### Fixes -* Your contribution here. +* [#683](https://github.com/ruby-grape/grape-swagger/pull/683): Fix handling of arrays of complex entities in params so that valid OpenAPI spec is generated - [@jdmurphy](https://github.com/jdmurphy). ### 0.29.0 (May 22, 2018) From 2689a8dbd214157933b07ada26453a27dcb3f945 Mon Sep 17 00:00:00 2001 From: Josh Murphy Date: Tue, 17 Jul 2018 10:15:05 -0500 Subject: [PATCH 3/3] Fix lint --- lib/grape-swagger/doc_methods/move_params.rb | 2 +- spec/lib/move_params_spec.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/grape-swagger/doc_methods/move_params.rb b/lib/grape-swagger/doc_methods/move_params.rb index e888507d..a73005b8 100644 --- a/lib/grape-swagger/doc_methods/move_params.rb +++ b/lib/grape-swagger/doc_methods/move_params.rb @@ -180,7 +180,7 @@ def prepare_nested_types(params) param[:type] = if param[:items][:type] == 'array' 'string' - elsif param[:items].keys.include?('$ref') + elsif param[:items].key?('$ref') param[:type] = 'object' else param[:items][:type] diff --git a/spec/lib/move_params_spec.rb b/spec/lib/move_params_spec.rb index a7b28455..2d020b50 100644 --- a/spec/lib/move_params_spec.rb +++ b/spec/lib/move_params_spec.rb @@ -340,7 +340,7 @@ type: 'array', items: { type: 'string' - }, + }, required: true } ] @@ -386,7 +386,7 @@ type: 'array', items: { type: 'array' - }, + }, required: true } ] @@ -419,7 +419,7 @@ type: 'array', items: { '$ref' => '#/definitions/StreetLine' - }, + }, required: true } ] @@ -455,7 +455,7 @@ type: 'array', items: { type: 'string' - }, + }, required: true } ] @@ -489,7 +489,7 @@ items: { type: 'integer', format: 'int32' - }, + }, required: true } ]