From 2a88e2a6204cdf7a675f27c8d3f0aa4254adbe95 Mon Sep 17 00:00:00 2001 From: cheremnov <32135863+cheremnov@users.noreply.github.com> Date: Wed, 2 Feb 2022 18:03:23 +0300 Subject: [PATCH] fix(specmap): suppress merging examples (#2437) Refs https://github.com/swagger-api/swagger-ui/issues/4175 --- src/specmap/lib/all-of.js | 6 +++ test/specmap/all-of.js | 98 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/src/specmap/lib/all-of.js b/src/specmap/lib/all-of.js index fde945a57..4748ba462 100644 --- a/src/specmap/lib/all-of.js +++ b/src/specmap/lib/all-of.js @@ -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)); diff --git a/test/specmap/all-of.js b/test/specmap/all-of.js index 9e663ad99..08a71a790 100644 --- a/test/specmap/all-of.js +++ b/test/specmap/all-of.js @@ -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', + }, + }, + }, + }); + }); });