Skip to content

Commit 2a88e2a

Browse files
authored
fix(specmap): suppress merging examples (#2437)
Refs swagger-api/swagger-ui#4175
1 parent 2bd283a commit 2a88e2a

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

src/specmap/lib/all-of.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ export default {
7575
return undefined;
7676
});
7777

78+
// If there was an example in the original definition,
79+
// keep it instead of merging with examples from other schema
80+
if (originalDefinitionObj.example) {
81+
// Delete other schema examples
82+
patches.push(specmap.remove([].concat(parent, 'example')));
83+
}
7884
// Merge back the values from the original definition
7985
patches.push(specmap.mergeDeep(parent, originalDefinitionObj));
8086

test/specmap/all-of.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,102 @@ describe('allOf', () => {
541541
},
542542
});
543543
}));
544+
545+
// https://github.com/swagger-api/swagger-ui/issues/4175
546+
test('should suppress merging examples when composing a schema', async () => {
547+
const res = await mapSpec({
548+
plugins: [plugins.refs, plugins.allOf],
549+
spec: {
550+
definitions: {
551+
Pet: {
552+
type: 'object',
553+
properties: {
554+
name: {
555+
type: 'string',
556+
},
557+
},
558+
example: {
559+
name: 'my pet',
560+
},
561+
},
562+
Cat: {
563+
allOf: [
564+
{ $ref: '#/definitions/Pet' },
565+
{
566+
type: 'object',
567+
properties: {
568+
meow: {
569+
type: 'string',
570+
},
571+
},
572+
example: {
573+
name: 'my cat',
574+
meow: 'meow',
575+
},
576+
},
577+
],
578+
},
579+
PetCat: {
580+
allOf: [{ $ref: '#/definitions/Pet' }, { $ref: '#/definitions/Cat' }],
581+
properties: {
582+
id: {
583+
type: 'string',
584+
},
585+
},
586+
example: {
587+
id: '1',
588+
},
589+
},
590+
},
591+
},
592+
});
593+
expect(res.errors).toEqual([]);
594+
expect(res.spec).toEqual({
595+
definitions: {
596+
Pet: {
597+
type: 'object',
598+
properties: {
599+
name: {
600+
type: 'string',
601+
},
602+
},
603+
example: {
604+
name: 'my pet',
605+
},
606+
},
607+
Cat: {
608+
type: 'object',
609+
properties: {
610+
name: {
611+
type: 'string',
612+
},
613+
meow: {
614+
type: 'string',
615+
},
616+
},
617+
example: {
618+
name: 'my cat',
619+
meow: 'meow',
620+
},
621+
},
622+
PetCat: {
623+
type: 'object',
624+
properties: {
625+
id: {
626+
type: 'string',
627+
},
628+
name: {
629+
type: 'string',
630+
},
631+
meow: {
632+
type: 'string',
633+
},
634+
},
635+
example: {
636+
id: '1',
637+
},
638+
},
639+
},
640+
});
641+
});
544642
});

0 commit comments

Comments
 (0)