Skip to content

Commit

Permalink
fix(specmap): suppress merging examples (#2437)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheremnov authored Feb 2, 2022
1 parent 2bd283a commit 2a88e2a
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/specmap/lib/all-of.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export default {
return undefined;
});

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

Expand Down
98 changes: 98 additions & 0 deletions test/specmap/all-of.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,102 @@ describe('allOf', () => {
},
});
}));

// https://github.com/swagger-api/swagger-ui/issues/4175
test('should suppress merging examples when composing a schema', async () => {
const res = await mapSpec({
plugins: [plugins.refs, plugins.allOf],
spec: {
definitions: {
Pet: {
type: 'object',
properties: {
name: {
type: 'string',
},
},
example: {
name: 'my pet',
},
},
Cat: {
allOf: [
{ $ref: '#/definitions/Pet' },
{
type: 'object',
properties: {
meow: {
type: 'string',
},
},
example: {
name: 'my cat',
meow: 'meow',
},
},
],
},
PetCat: {
allOf: [{ $ref: '#/definitions/Pet' }, { $ref: '#/definitions/Cat' }],
properties: {
id: {
type: 'string',
},
},
example: {
id: '1',
},
},
},
},
});
expect(res.errors).toEqual([]);
expect(res.spec).toEqual({
definitions: {
Pet: {
type: 'object',
properties: {
name: {
type: 'string',
},
},
example: {
name: 'my pet',
},
},
Cat: {
type: 'object',
properties: {
name: {
type: 'string',
},
meow: {
type: 'string',
},
},
example: {
name: 'my cat',
meow: 'meow',
},
},
PetCat: {
type: 'object',
properties: {
id: {
type: 'string',
},
name: {
type: 'string',
},
meow: {
type: 'string',
},
},
example: {
id: '1',
},
},
},
});
});
});

0 comments on commit 2a88e2a

Please sign in to comment.