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

Fix array of entities with nested entities #683

Merged
merged 3 commits into from
Jul 19, 2018
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
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