Skip to content

Commit

Permalink
Fix array of entities with nested entities (#683)
Browse files Browse the repository at this point in the history
* Update prepare_nested_types to allow for references and update specs

* Add CHANGELOG entry

* Fix lint
  • Loading branch information
jdmurphy authored and LeFnord committed Jul 19, 2018
1 parent c00c5b4 commit 1c1b7ea
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 9 additions & 2 deletions lib/grape-swagger/doc_methods/move_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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].key?('$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

Expand Down
188 changes: 188 additions & 0 deletions spec/lib/move_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1c1b7ea

Please sign in to comment.