Skip to content

Commit 609698b

Browse files
author
Josh Murphy
committed
Deep merge params for definition and add specs
1 parent c25fe68 commit 609698b

File tree

3 files changed

+184
-6
lines changed

3 files changed

+184
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Fixes
88

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

1112
### 0.30.1 (July 19, 2018)

lib/grape-swagger/doc_methods/move_params.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'active_support/core_ext/hash/deep_merge'
4+
35
module GrapeSwagger
46
module DocMethods
57
class MoveParams
@@ -114,11 +116,11 @@ def build_nested_properties(params, properties = {})
114116

115117
def recursive_call(properties, property, nested_params)
116118
if should_expose_as_array?(nested_params)
117-
properties[property] = array_type
118-
move_params_to_new(properties[property][:items], nested_params)
119+
properties[property.to_sym] = array_type
120+
move_params_to_new(properties[property.to_sym][:items], nested_params)
119121
else
120-
properties[property] = object_type
121-
move_params_to_new(properties[property], nested_params)
122+
properties[property.to_sym] = object_type
123+
move_params_to_new(properties[property.to_sym], nested_params)
122124
end
123125
end
124126

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

136138
def add_properties_to_definition(definition, properties, required)
137139
if definition.key?(:items)
138-
definition[:items][:properties].merge!(properties)
140+
definition[:items][:properties].deep_merge!(properties)
139141
add_to_required(definition[:items], required)
140142
else
141-
definition[:properties].merge!(properties)
143+
definition[:properties].deep_merge!(properties)
142144
add_to_required(definition, required)
143145
end
144146
end

spec/lib/move_params_spec.rb

+175
Original file line numberDiff line numberDiff line change
@@ -513,5 +513,180 @@
513513
end
514514
end
515515
end
516+
517+
describe 'recursive_call' do
518+
before :each do
519+
subject.send(:recursive_call, properties, 'test', nested_params)
520+
end
521+
522+
let(:properties) { {} }
523+
524+
context 'when nested params is an array' do
525+
let(:nested_params) do
526+
[
527+
{
528+
in: 'body',
529+
name: 'aliases',
530+
description: 'The aliases of test.',
531+
type: 'array',
532+
items: { type: 'string' },
533+
required: true
534+
}
535+
]
536+
end
537+
538+
let(:expected_properties) do
539+
{
540+
type: 'array',
541+
items: {
542+
type: 'object',
543+
properties: {
544+
aliases: {
545+
type: 'string',
546+
description: 'The aliases of test.'
547+
}
548+
},
549+
required: [:aliases]
550+
}
551+
}
552+
end
553+
554+
it 'adds property as symbol with array type and items' do
555+
expect(properties[:test]).to eq expected_properties
556+
end
557+
end
558+
559+
context 'when nested params is not an array' do
560+
let(:nested_params) do
561+
[
562+
{
563+
in: 'body',
564+
name: 'id',
565+
description: 'The unique ID of test.',
566+
type: 'string',
567+
required: true
568+
}
569+
]
570+
end
571+
572+
let(:expected_properties) do
573+
{
574+
type: 'object',
575+
required: [:id],
576+
properties: {
577+
id: {
578+
type: 'string',
579+
description: 'The unique ID of test.'
580+
}
581+
}
582+
}
583+
end
584+
585+
it 'adds property as symbol with object type' do
586+
expect(properties[:test]).to eq expected_properties
587+
end
588+
end
589+
end
590+
591+
describe 'add_properties_to_definition' do
592+
before :each do
593+
subject.send(:add_properties_to_definition, definition, properties, [])
594+
end
595+
596+
context 'when definition has items key' do
597+
let(:definition) do
598+
{
599+
type: 'array',
600+
items: {
601+
type: 'object',
602+
properties: {
603+
description: 'Test description'
604+
}
605+
}
606+
}
607+
end
608+
609+
let(:properties) do
610+
{
611+
strings: {
612+
type: 'string',
613+
description: 'string elements'
614+
}
615+
}
616+
end
617+
618+
let(:expected_definition) do
619+
{
620+
type: 'array',
621+
items: {
622+
type: 'object',
623+
properties: {
624+
description: 'Test description',
625+
strings: {
626+
type: 'string',
627+
description: 'string elements'
628+
}
629+
}
630+
}
631+
}
632+
end
633+
634+
it 'deep merges properties into definition item properties' do
635+
expect(definition).to eq expected_definition
636+
end
637+
end
638+
639+
context 'when definition does not have items key' do
640+
let(:definition) do
641+
{
642+
type: 'object',
643+
properties: {
644+
parent: {
645+
type: 'object',
646+
description: 'Parent to child'
647+
}
648+
}
649+
}
650+
end
651+
652+
let(:properties) do
653+
{
654+
parent: {
655+
type: 'object',
656+
properties: {
657+
id: {
658+
type: 'string',
659+
description: 'Parent ID'
660+
}
661+
},
662+
required: [:id]
663+
}
664+
}
665+
end
666+
667+
let(:expected_definition) do
668+
{
669+
type: 'object',
670+
properties: {
671+
parent: {
672+
type: 'object',
673+
description: 'Parent to child',
674+
properties: {
675+
id: {
676+
type: 'string',
677+
description: 'Parent ID'
678+
}
679+
},
680+
required: [:id]
681+
}
682+
}
683+
}
684+
end
685+
686+
it 'deep merges properties into definition properties' do
687+
expect(definition).to eq expected_definition
688+
end
689+
end
690+
end
516691
end
517692
end

0 commit comments

Comments
 (0)