Skip to content

Commit

Permalink
Deep merge params for definition and add specs (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdmurphy authored and LeFnord committed Jul 24, 2018
1 parent c25fe68 commit baa8614
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

* [#688](https://github.com/ruby-grape/grape-swagger/pull/688): Use deep merge for nested parameter definitions - [@jdmurphy](https://github.com/jdmurphy).
* Your contribution here.

### 0.30.1 (July 19, 2018)
Expand Down
14 changes: 8 additions & 6 deletions lib/grape-swagger/doc_methods/move_params.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'active_support/core_ext/hash/deep_merge'

module GrapeSwagger
module DocMethods
class MoveParams
Expand Down Expand Up @@ -114,11 +116,11 @@ def build_nested_properties(params, properties = {})

def recursive_call(properties, property, nested_params)
if should_expose_as_array?(nested_params)
properties[property] = array_type
move_params_to_new(properties[property][:items], nested_params)
properties[property.to_sym] = array_type
move_params_to_new(properties[property.to_sym][:items], nested_params)
else
properties[property] = object_type
move_params_to_new(properties[property], nested_params)
properties[property.to_sym] = object_type
move_params_to_new(properties[property.to_sym], nested_params)
end
end

Expand All @@ -135,10 +137,10 @@ def delete_from(params, to_delete)

def add_properties_to_definition(definition, properties, required)
if definition.key?(:items)
definition[:items][:properties].merge!(properties)
definition[:items][:properties].deep_merge!(properties)
add_to_required(definition[:items], required)
else
definition[:properties].merge!(properties)
definition[:properties].deep_merge!(properties)
add_to_required(definition, required)
end
end
Expand Down
175 changes: 175 additions & 0 deletions spec/lib/move_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -513,5 +513,180 @@
end
end
end

describe 'recursive_call' do
before :each do
subject.send(:recursive_call, properties, 'test', nested_params)
end

let(:properties) { {} }

context 'when nested params is an array' do
let(:nested_params) do
[
{
in: 'body',
name: 'aliases',
description: 'The aliases of test.',
type: 'array',
items: { type: 'string' },
required: true
}
]
end

let(:expected_properties) do
{
type: 'array',
items: {
type: 'object',
properties: {
aliases: {
type: 'string',
description: 'The aliases of test.'
}
},
required: [:aliases]
}
}
end

it 'adds property as symbol with array type and items' do
expect(properties[:test]).to eq expected_properties
end
end

context 'when nested params is not an array' do
let(:nested_params) do
[
{
in: 'body',
name: 'id',
description: 'The unique ID of test.',
type: 'string',
required: true
}
]
end

let(:expected_properties) do
{
type: 'object',
required: [:id],
properties: {
id: {
type: 'string',
description: 'The unique ID of test.'
}
}
}
end

it 'adds property as symbol with object type' do
expect(properties[:test]).to eq expected_properties
end
end
end

describe 'add_properties_to_definition' do
before :each do
subject.send(:add_properties_to_definition, definition, properties, [])
end

context 'when definition has items key' do
let(:definition) do
{
type: 'array',
items: {
type: 'object',
properties: {
description: 'Test description'
}
}
}
end

let(:properties) do
{
strings: {
type: 'string',
description: 'string elements'
}
}
end

let(:expected_definition) do
{
type: 'array',
items: {
type: 'object',
properties: {
description: 'Test description',
strings: {
type: 'string',
description: 'string elements'
}
}
}
}
end

it 'deep merges properties into definition item properties' do
expect(definition).to eq expected_definition
end
end

context 'when definition does not have items key' do
let(:definition) do
{
type: 'object',
properties: {
parent: {
type: 'object',
description: 'Parent to child'
}
}
}
end

let(:properties) do
{
parent: {
type: 'object',
properties: {
id: {
type: 'string',
description: 'Parent ID'
}
},
required: [:id]
}
}
end

let(:expected_definition) do
{
type: 'object',
properties: {
parent: {
type: 'object',
description: 'Parent to child',
properties: {
id: {
type: 'string',
description: 'Parent ID'
}
},
required: [:id]
}
}
}
end

it 'deep merges properties into definition properties' do
expect(definition).to eq expected_definition
end
end
end
end
end

0 comments on commit baa8614

Please sign in to comment.