From 6bb60ca90d4acdee6661465f4c6d59fc92f336a3 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Fri, 8 Dec 2017 23:53:08 -0800 Subject: [PATCH 1/4] Revert "Merge pull request #1201 from shockey/bug/editor-1585-refs-not-resolving" This reverts commit 8c21ccf5f0470a4bc8159361e381aef87ee47ce4, reversing changes made to 91558744b7ff477f7231adedd060f73b03a67411. --- src/specmap/index.js | 28 +++-- test/resolver.js | 249 ------------------------------------------- 2 files changed, 12 insertions(+), 265 deletions(-) diff --git a/src/specmap/index.js b/src/specmap/index.js index cb7a64631..5b4c4ffa2 100644 --- a/src/specmap/index.js +++ b/src/specmap/index.js @@ -83,7 +83,7 @@ class SpecMap { // We might consider making this (traversing & application) configurable later. function createKeyBasedPlugin(pluginObj) { return function* (patches, specmap) { - const resultCache = {} + const traversedRefs = {} for (const patch of patches.filter(lib.isAdditiveMutation)) { yield* traverse(patch.value, patch.path, patch) @@ -109,24 +109,20 @@ class SpecMap { // If the object has a meta '$$ref' - and store this $$ref // in a lookaside to prevent future traversals of this $ref's tree. const objRef = obj.$$ref - - if (isObj) { - // console.log('recursive case', {obj, path, patch}) - yield* traverse(val, updatedPath, patch) + const traversed = specmap.allowMetaPatches && traversedRefs[obj.$$ref] + + if (!traversed) { + if (isObj) { + // Only store the ref if it exists + if (specmap.allowMetaPatches && objRef) { + traversedRefs[objRef] = true + } + yield* traverse(val, updatedPath, patch) + } } if (!isRootProperties && key === pluginObj.key) { - // console.log('base case', {obj, path, patch}) - const stringifiedPath = path.join('/') - - if (resultCache[stringifiedPath]) { - yield resultCache[stringifiedPath] - } - else { - const res = pluginObj.plugin(val, key, updatedPath, specmap, patch) - resultCache[stringifiedPath] = res - yield res - } + yield pluginObj.plugin(val, key, updatedPath, specmap, patch) } } } diff --git a/test/resolver.js b/test/resolver.js index e2ee5fbf0..b9eeb0645 100644 --- a/test/resolver.js +++ b/test/resolver.js @@ -237,253 +237,4 @@ describe('resolver', () => { }) } }) - - const DOCUMENT_ORIGINAL = { - swagger: '2.0', - paths: { - '/pet': { - post: { - tags: [ - 'pet' - ], - summary: 'Add a new pet to the store', - operationId: 'addPet', - parameters: [ - { - in: 'body', - name: 'body', - description: 'Pet object that needs to be added to the store', - required: true, - schema: { - $ref: '#/definitions/Pet' - } - } - ], - responses: { - 405: { - description: 'Invalid input' - } - } - } - } - }, - definitions: { - Category: { - type: 'object', - properties: { - id: { - type: 'integer', - format: 'int64' - }, - name: { - type: 'string' - } - } - }, - Pet: { - type: 'object', - required: [ - 'category' - ], - properties: { - category: { - $ref: '#/definitions/Category' - } - } - } - } - } - - describe.only('Swagger usage', function () { - it.skip('should be able to resolve a Swagger document with $refs', () => { - // When - return Swagger.resolve({spec: DOCUMENT_ORIGINAL, allowMetaPatches: false}) - .then(handleResponse) - - // Then - function handleResponse(obj) { - expect(obj.errors).toEqual([]) - expect(obj.spec).toEqual({ - swagger: '2.0', - paths: { - '/pet': { - post: { - tags: [ - 'pet' - ], - summary: 'Add a new pet to the store', - operationId: 'addPet', - __originalOperationId: 'addPet', - parameters: [ - { - in: 'body', - name: 'body', - description: 'Pet object that needs to be added to the store', - required: true, - schema: { - type: 'object', - required: [ - 'category' - ], - properties: { - category: { - type: 'object', - properties: { - id: { - type: 'integer', - format: 'int64' - }, - name: { - type: 'string' - } - } - } - } - } - } - ], - responses: { - 405: { - description: 'Invalid input' - } - } - } - } - }, - definitions: { - Category: { - type: 'object', - properties: { - id: { - type: 'integer', - format: 'int64' - }, - name: { - type: 'string' - } - } - }, - Pet: { - type: 'object', - required: [ - 'category' - ], - properties: { - category: { - type: 'object', - properties: { - id: { - type: 'integer', - format: 'int64' - }, - name: { - type: 'string' - } - } - } - } - } - } - }) - } - }) - - it('should be able to resolve a Swagger document with $refs when allowMetaPatches is enabled', () => { - // When - return Swagger.resolve({spec: DOCUMENT_ORIGINAL, allowMetaPatches: true}) - .then(handleResponse) - - // Then - function handleResponse(obj) { - expect(obj.errors).toEqual([]) - expect(obj.spec).toEqual({ - swagger: '2.0', - paths: { - '/pet': { - post: { - tags: [ - 'pet' - ], - summary: 'Add a new pet to the store', - operationId: 'addPet', - __originalOperationId: 'addPet', - parameters: [ - { - in: 'body', - name: 'body', - description: 'Pet object that needs to be added to the store', - required: true, - schema: { - $$ref: '#/definitions/Pet', - type: 'object', - required: [ - 'category' - ], - properties: { - category: { - $$ref: '#/definitions/Category', - type: 'object', - properties: { - id: { - type: 'integer', - format: 'int64' - }, - name: { - type: 'string' - } - } - } - } - } - } - ], - responses: { - 405: { - description: 'Invalid input' - } - } - } - } - }, - definitions: { - Category: { - $$ref: '#/definitions/Category', // FIXME: benign, but this should not be present - type: 'object', - properties: { - id: { - type: 'integer', - format: 'int64' - }, - name: { - type: 'string' - } - } - }, - Pet: { - $$ref: '#/definitions/Pet', - type: 'object', - required: [ - 'category' - ], - properties: { - category: { - $$ref: '#/definitions/Category', // FIXME: benign, but this should not be present - type: 'object', - properties: { - id: { - type: 'integer', - format: 'int64' - }, - name: { - type: 'string' - } - } - } - } - } - } - }) - } - }) - }) }) From 3d035377c9d2efab293589a1f1c871f9779616d4 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Fri, 8 Dec 2017 23:53:54 -0800 Subject: [PATCH 2/4] Revert "Merge pull request #1190 from dballance/master" This reverts commit 1bdb45a334ce3939441524af5af68c377b8f88d7, reversing changes made to 66cff030af0bbb77e72fc0c45c285eeb9a6970a7. --- src/specmap/index.js | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/specmap/index.js b/src/specmap/index.js index 5b4c4ffa2..0632938ec 100644 --- a/src/specmap/index.js +++ b/src/specmap/index.js @@ -83,8 +83,6 @@ class SpecMap { // We might consider making this (traversing & application) configurable later. function createKeyBasedPlugin(pluginObj) { return function* (patches, specmap) { - const traversedRefs = {} - for (const patch of patches.filter(lib.isAdditiveMutation)) { yield* traverse(patch.value, patch.path, patch) } @@ -104,21 +102,9 @@ class SpecMap { for (const key of Object.keys(obj)) { const val = obj[key] const updatedPath = path.concat(key) - const isObj = lib.isObject(val) - - // If the object has a meta '$$ref' - and store this $$ref - // in a lookaside to prevent future traversals of this $ref's tree. - const objRef = obj.$$ref - const traversed = specmap.allowMetaPatches && traversedRefs[obj.$$ref] - - if (!traversed) { - if (isObj) { - // Only store the ref if it exists - if (specmap.allowMetaPatches && objRef) { - traversedRefs[objRef] = true - } - yield* traverse(val, updatedPath, patch) - } + + if (lib.isObject(val)) { + yield* traverse(val, updatedPath, patch) } if (!isRootProperties && key === pluginObj.key) { From 3903747e2c3bebe8066bc54666164c02e8196c3b Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Fri, 8 Dec 2017 23:55:30 -0800 Subject: [PATCH 3/4] Keep updated resolver tests from reverted PR #1201 --- test/resolver.js | 249 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 249 insertions(+) diff --git a/test/resolver.js b/test/resolver.js index b9eeb0645..c86dab181 100644 --- a/test/resolver.js +++ b/test/resolver.js @@ -237,4 +237,253 @@ describe('resolver', () => { }) } }) + + const DOCUMENT_ORIGINAL = { + swagger: '2.0', + paths: { + '/pet': { + post: { + tags: [ + 'pet' + ], + summary: 'Add a new pet to the store', + operationId: 'addPet', + parameters: [ + { + in: 'body', + name: 'body', + description: 'Pet object that needs to be added to the store', + required: true, + schema: { + $ref: '#/definitions/Pet' + } + } + ], + responses: { + 405: { + description: 'Invalid input' + } + } + } + } + }, + definitions: { + Category: { + type: 'object', + properties: { + id: { + type: 'integer', + format: 'int64' + }, + name: { + type: 'string' + } + } + }, + Pet: { + type: 'object', + required: [ + 'category' + ], + properties: { + category: { + $ref: '#/definitions/Category' + } + } + } + } + } + + describe('Swagger usage', function () { + it.skip('should be able to resolve a Swagger document with $refs', () => { + // When + return Swagger.resolve({spec: DOCUMENT_ORIGINAL, allowMetaPatches: false}) + .then(handleResponse) + + // Then + function handleResponse(obj) { + expect(obj.errors).toEqual([]) + expect(obj.spec).toEqual({ + swagger: '2.0', + paths: { + '/pet': { + post: { + tags: [ + 'pet' + ], + summary: 'Add a new pet to the store', + operationId: 'addPet', + __originalOperationId: 'addPet', + parameters: [ + { + in: 'body', + name: 'body', + description: 'Pet object that needs to be added to the store', + required: true, + schema: { + type: 'object', + required: [ + 'category' + ], + properties: { + category: { + type: 'object', + properties: { + id: { + type: 'integer', + format: 'int64' + }, + name: { + type: 'string' + } + } + } + } + } + } + ], + responses: { + 405: { + description: 'Invalid input' + } + } + } + } + }, + definitions: { + Category: { + type: 'object', + properties: { + id: { + type: 'integer', + format: 'int64' + }, + name: { + type: 'string' + } + } + }, + Pet: { + type: 'object', + required: [ + 'category' + ], + properties: { + category: { + type: 'object', + properties: { + id: { + type: 'integer', + format: 'int64' + }, + name: { + type: 'string' + } + } + } + } + } + } + }) + } + }) + + it('should be able to resolve a Swagger document with $refs when allowMetaPatches is enabled', () => { + // When + return Swagger.resolve({spec: DOCUMENT_ORIGINAL, allowMetaPatches: true}) + .then(handleResponse) + + // Then + function handleResponse(obj) { + expect(obj.errors).toEqual([]) + expect(obj.spec).toEqual({ + swagger: '2.0', + paths: { + '/pet': { + post: { + tags: [ + 'pet' + ], + summary: 'Add a new pet to the store', + operationId: 'addPet', + __originalOperationId: 'addPet', + parameters: [ + { + in: 'body', + name: 'body', + description: 'Pet object that needs to be added to the store', + required: true, + schema: { + $$ref: '#/definitions/Pet', + type: 'object', + required: [ + 'category' + ], + properties: { + category: { + $$ref: '#/definitions/Category', + type: 'object', + properties: { + id: { + type: 'integer', + format: 'int64' + }, + name: { + type: 'string' + } + } + } + } + } + } + ], + responses: { + 405: { + description: 'Invalid input' + } + } + } + } + }, + definitions: { + Category: { + $$ref: '#/definitions/Category', // FIXME: benign, but this should not be present + type: 'object', + properties: { + id: { + type: 'integer', + format: 'int64' + }, + name: { + type: 'string' + } + } + }, + Pet: { + $$ref: '#/definitions/Pet', + type: 'object', + required: [ + 'category' + ], + properties: { + category: { + $$ref: '#/definitions/Category', // FIXME: benign, but this should not be present + type: 'object', + properties: { + id: { + type: 'integer', + format: 'int64' + }, + name: { + type: 'string' + } + } + } + } + } + } + }) + } + }) + }) }) From ebbb27b3890a88623d7d59645af27f2c84779032 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Sat, 9 Dec 2017 00:01:10 -0800 Subject: [PATCH 4/4] v3.4.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bd4c87f10..d1b3388e3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swagger-client", - "version": "3.4.0", + "version": "3.4.1", "description": "SwaggerJS - a collection of interfaces for OAI specs", "main": "dist/index.js", "repository": "git@github.com:swagger-api/swagger-js.git",