Skip to content

Commit ba3ec94

Browse files
committed
fix(mergers): unmerged $ref pointing at allOf
1 parent 132a299 commit ba3ec94

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/__tests__/tree.spec.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,71 @@ describe('SchemaTree', () => {
278278
"
279279
`);
280280
});
281+
282+
it('given allOf having a $ref pointing at another allOf, should merge it', () => {
283+
const schema: JSONSchema4 = {
284+
type: 'object',
285+
allOf: [
286+
{
287+
$ref: '#/definitions/Item',
288+
},
289+
{
290+
properties: {
291+
summary: {
292+
type: 'string',
293+
},
294+
},
295+
},
296+
],
297+
definitions: {
298+
Item: {
299+
allOf: [
300+
{
301+
properties: {
302+
id: {
303+
type: 'string',
304+
},
305+
},
306+
},
307+
{
308+
properties: {
309+
description: {
310+
type: 'string',
311+
},
312+
},
313+
},
314+
],
315+
},
316+
},
317+
};
318+
319+
const tree = new SchemaTree(schema);
320+
tree.populate();
321+
322+
expect(printTree(schema)).toMatchInlineSnapshot(`
323+
"└─ #
324+
├─ types
325+
│ └─ 0: object
326+
├─ primaryType: object
327+
└─ children
328+
├─ 0
329+
│ └─ #/properties/summary
330+
│ ├─ types
331+
│ │ └─ 0: string
332+
│ └─ primaryType: string
333+
├─ 1
334+
│ └─ #/properties/id
335+
│ ├─ types
336+
│ │ └─ 0: string
337+
│ └─ primaryType: string
338+
└─ 2
339+
└─ #/properties/description
340+
├─ types
341+
│ └─ 0: string
342+
└─ primaryType: string
343+
"
344+
`);
345+
});
281346
});
282347
});
283348

src/mergers/mergeAllOf.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,10 @@ export function mergeAllOf(fragment: SchemaFragment, path: string[], walkingOpti
5959
store.set(walkingOptions.resolveRef, new WeakMap());
6060
}
6161

62-
return _mergeAllOf(fragment, path, walkingOptions.resolveRef);
62+
const merged = _mergeAllOf(fragment, path, walkingOptions.resolveRef);
63+
if ('allOf' in merged) {
64+
return _mergeAllOf(merged, path, walkingOptions.resolveRef);
65+
}
66+
67+
return merged;
6368
}

0 commit comments

Comments
 (0)