Skip to content

Commit 2f5bb86

Browse files
committed
Fix and test for swagger-ui #3328: swagger-api/swagger-ui#3328. Added manual logic to merge arrays after calling deepExtend within mergeDeep function
1 parent 1512306 commit 2f5bb86

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/specmap/lib/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,19 @@ function applyPatch(obj, patch, opts) {
4444
else if (patch.op === 'mergeDeep') {
4545
const valPatch = _get(patch.path)
4646
jsonPatch.apply(obj, [valPatch])
47+
const origValPatchValue = Object.assign({}, valPatch.value)
4748
deepExtend(valPatch.value, patch.value)
49+
50+
// deepExtend doesn't merge arrays, so we will do it manually
51+
for ( let prop in patch.value ) {
52+
if ( patch.value.hasOwnProperty(prop) ) {
53+
const propVal = patch.value[prop]
54+
if ( Array.isArray(propVal) ) {
55+
let existing = origValPatchValue[prop] || []
56+
valPatch.value[prop] = existing.concat( propVal )
57+
}
58+
}
59+
}
4860
}
4961
else {
5062
jsonPatch.apply(obj, [patch])

test/specmap/all-of.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,51 @@ describe('allOf', function () {
173173
})
174174
})
175175
})
176+
177+
it('merges arrays inside of an `allOf`', function() {
178+
return mapSpec({
179+
plugins: [plugins.refs, plugins.allOf],
180+
showDebug: true,
181+
spec: {
182+
definitions: {
183+
one: {
184+
allOf: [
185+
{
186+
$ref: '#/definitions/two'
187+
},
188+
{
189+
type: 'object',
190+
required: ['a', 'b']
191+
}
192+
]
193+
},
194+
two: {
195+
allOf: [
196+
{
197+
type: 'object',
198+
required: ['c', 'd']
199+
}
200+
]
201+
}
202+
}
203+
}
204+
}).then((res) => {
205+
expect(res.errors).toEqual([])
206+
expect(res.spec).toEqual({
207+
definitions: {
208+
one: {
209+
type: 'object',
210+
required: ['c', 'd', 'a', 'b']
211+
},
212+
two: {
213+
type: 'object',
214+
required: ['c', 'd']
215+
}
216+
},
217+
})
218+
})
219+
})
220+
176221
// TODO: this needs to get fixed
177222
it.skip('should handle case, with an `allOf` referencing an `allOf` ', function () {
178223
return mapSpec({

0 commit comments

Comments
 (0)