Skip to content

Commit 639330f

Browse files
authored
Merge branch 'master' into master
2 parents c0e22f5 + 98d836e commit 639330f

File tree

2 files changed

+140
-3
lines changed

2 files changed

+140
-3
lines changed

src/specmap/lib/all-of.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ export default {
1717
const parent = fullPath.slice(0, -1)
1818
let alreadyAddError = false
1919

20-
return [specmap.replace(parent, {})].concat(val.map((toMerge, index) => {
20+
// Find the original definition from the `patch.value` object
21+
// Remove the `allOf` property so it doesn't get added to the result of the `allOf` plugin
22+
let originalDefinitionObj = patch.value
23+
parent.forEach((part) => {
24+
originalDefinitionObj = originalDefinitionObj[part]
25+
})
26+
originalDefinitionObj = Object.assign({}, originalDefinitionObj)
27+
delete originalDefinitionObj.allOf
28+
29+
const allOfPatches = [specmap.replace(parent, {})].concat(val.map((toMerge, index) => {
2130
if (!specmap.isObject(toMerge)) {
2231
if (alreadyAddError) {
2332
return null
@@ -31,5 +40,16 @@ export default {
3140

3241
return specmap.mergeDeep(parent, toMerge)
3342
}))
43+
44+
// Merge back the values from the original definition
45+
allOfPatches.push(specmap.mergeDeep(parent, originalDefinitionObj))
46+
47+
// If there was not an original $$ref value, make sure to remove
48+
// any $$ref value that may exist from the result of `allOf` merges
49+
if (!originalDefinitionObj.$$ref) {
50+
allOfPatches.push(specmap.remove([].concat(parent, '$$ref')))
51+
}
52+
53+
return allOfPatches
3454
}
3555
}

test/specmap/all-of.js

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,124 @@ describe('allOf', function () {
5656
spec: {
5757
one: {baz: 4},
5858
two: 2,
59+
bar: {baz: 4}
60+
}
61+
})
62+
})
63+
})
64+
65+
it('should not overwrite properties that are already present', function () {
66+
return mapSpec({
67+
spec: {
68+
original: 'yes',
69+
allOf: [
70+
{
71+
original: 'no',
72+
notOriginal: 'yes'
73+
}
74+
]
75+
},
76+
plugins: [plugins.refs, plugins.allOf]
77+
}).then((res) => {
78+
expect(res).toEqual({
79+
errors: [],
80+
spec: {
81+
original: 'yes',
82+
notOriginal: 'yes'
83+
}
84+
})
85+
})
86+
})
87+
88+
it('should set $$ref values', function () {
89+
return mapSpec({
90+
allowMetaPatches: true,
91+
spec: {
92+
Pet: {
93+
type: 'object',
94+
properties: {
95+
name: {
96+
type: 'string'
97+
}
98+
}
99+
},
100+
Cat: {
101+
allOf: [
102+
{$ref: '#/Pet'},
103+
{
104+
type: 'object',
105+
properties: {
106+
meow: {
107+
type: 'string'
108+
}
109+
}
110+
}
111+
]
112+
},
113+
Animal: {
114+
type: 'object',
115+
properties: {
116+
pet: {
117+
$ref: '#/Pet'
118+
},
119+
cat: {
120+
$ref: '#/Cat'
121+
}
122+
}
123+
}
124+
},
125+
plugins: [plugins.refs, plugins.allOf]
126+
}).then((res) => {
127+
expect(res).toEqual({
128+
errors: [],
129+
spec: {
130+
Pet: {
131+
$$ref: '#/Pet',
132+
type: 'object',
133+
properties: {
134+
name: {
135+
type: 'string'
136+
}
137+
}
138+
},
139+
Cat: {
140+
$$ref: '#/Cat',
141+
properties: {
142+
meow: {
143+
type: 'string'
144+
},
145+
name: {
146+
type: 'string'
147+
}
148+
},
149+
type: 'object'
150+
},
151+
Animal: {
152+
type: 'object',
153+
properties: {
154+
pet: {
155+
$$ref: '#/Pet',
156+
properties: {
157+
name: {
158+
type: 'string'
159+
}
160+
},
161+
type: 'object'
162+
},
163+
cat: {
164+
$$ref: '#/Cat',
165+
properties: {
166+
meow: {
167+
type: 'string'
168+
},
169+
name: {
170+
type: 'string'
171+
}
172+
},
173+
type: 'object'
174+
}
175+
}
176+
}
59177
}
60178
})
61179
})
@@ -218,8 +336,7 @@ describe('allOf', function () {
218336
})
219337
})
220338

221-
// TODO: this needs to get fixed
222-
it.skip('should handle case, with an `allOf` referencing an `allOf` ', function () {
339+
it('should handle case, with an `allOf` referencing an `allOf` ', function () {
223340
return mapSpec({
224341
plugins: [plugins.refs, plugins.allOf],
225342
showDebug: true,

0 commit comments

Comments
 (0)