From e79200090b46176b071dc3c624f8cd33098e1360 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Sun, 6 Sep 2015 16:50:05 -0700 Subject: [PATCH 1/2] added fix, tests for #489 --- lib/resolver.js | 2 +- test/resolver.js | 48 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/lib/resolver.js b/lib/resolver.js index e825e3eb0..ff85a3b78 100644 --- a/lib/resolver.js +++ b/lib/resolver.js @@ -188,7 +188,7 @@ Resolver.prototype.resolve = function (spec, arg1, arg2, arg3) { // resolve anything that is local for(var ii = 0; ii < toResolve.length; ii++) { (function(item, self) { - if(item.root === null) { + if(item.root === null || item.root === root) { // local resolve self.resolveItem(spec, _root, resolutionTable, resolvedRefs, unresolvedRefs, item); processedCalls += 1; diff --git a/test/resolver.js b/test/resolver.js index 7936de719..12c6bf3ec 100644 --- a/test/resolver.js +++ b/test/resolver.js @@ -9,7 +9,6 @@ var Resolver = require('../lib/resolver'); var instance; describe('swagger resolver', function () { - before(function (done) { mock.petstore(done, function (petstore, server){ instance = server; @@ -20,7 +19,7 @@ describe('swagger resolver', function () { instance.close(); done(); }); - +/* it('is OK without remote references', function (done) { var api = new Resolver(); var spec = {}; @@ -30,7 +29,7 @@ describe('swagger resolver', function () { done(); }); }); - +*/ it('resolves a remote model property reference $ref', function (done) { var api = new Resolver(); var spec = { @@ -43,7 +42,7 @@ describe('swagger resolver', function () { } }; - api.resolve(spec, 'http://localhost:8000/v2/petstore.json', function (spec) { + api.resolve(spec, 'http://localhost:8080/v2/petstore.json', function (spec) { expect(spec.definitions.Category).toExist(); done(); }); @@ -131,7 +130,7 @@ describe('swagger resolver', function () { } }; - api.resolve(spec, 'http://localhost:8000/v2/petstore.json', function (spec) { + api.resolve(spec, 'http://localhost:8080/v2/petstore.json', function (spec) { expect(spec.definitions.Category).toExist(); done(); }); @@ -154,7 +153,7 @@ describe('swagger resolver', function () { } }; - api.resolve(spec, 'http://localhost:8000/v2/petstore.json', function (spec) { + api.resolve(spec, 'http://localhost:8080/v2/petstore.json', function (spec) { expect(spec.definitions.Pet).toExist(); done(); }); @@ -177,7 +176,7 @@ describe('swagger resolver', function () { } }; - api.resolve(spec, 'http://localhost:8000/v2/petstore.json', function (spec) { + api.resolve(spec, 'http://localhost:8080/v2/petstore.json', function (spec) { expect(spec.definitions.Pet).toExist(); expect(spec.paths['/pet'].post.responses['200'].schema.$ref).toBe('#/definitions/Pet'); @@ -328,7 +327,7 @@ describe('swagger resolver', function () { } }; - api.resolve(spec, 'http://localhost:8000/v2/petstore.json', function (spec) { + api.resolve(spec, 'http://localhost:8080/v2/petstore.json', function (spec) { var path = spec.paths['/myUsername']; test.object(path); test.object(path.get); @@ -390,7 +389,8 @@ describe('swagger resolver', function () { } } }; - api.resolve(spec, 'http://localhost:8000/v2/petstore.json', function (spec) { + // we use a different URL so the resolver doesn't treat the `spec` as the *entire* spec. + api.resolve(spec, 'http://localhost:8080/v2/petstore.json', function (spec) { var get = spec.paths['/myUsername'].get; var response = get.responses['400']; expect(response.description).toBe('failed'); @@ -696,4 +696,34 @@ describe('swagger resolver', function () { done(); }); }); + + it('does not make multiple calls for parameter refs #489', function(done) { + var api = new Resolver(); + var spec = { + paths: { + '/pet': { + post: { + parameters: [{ + $ref: '#/parameters/sharedSkip' + }] + } + } + }, + parameters: { + sharedSkip: { + name: 'skip', + in: 'query', + description: 'Results to skip', + required: false, + type: 'integer', + format: 'int32' + } + } + }; + + api.resolve(spec, 'http://localhost:8000/swagger.json', function (spec, unresolved) { + expect(Object.keys(unresolved).length).toBe(0); + done(); + }); + }); }); From 2575cd2a08ce400da6a8b02fb2727dd6283c90a2 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Sun, 6 Sep 2015 16:52:08 -0700 Subject: [PATCH 2/2] enabled test --- test/resolver.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/resolver.js b/test/resolver.js index 12c6bf3ec..945382fda 100644 --- a/test/resolver.js +++ b/test/resolver.js @@ -19,7 +19,7 @@ describe('swagger resolver', function () { instance.close(); done(); }); -/* + it('is OK without remote references', function (done) { var api = new Resolver(); var spec = {}; @@ -29,7 +29,7 @@ describe('swagger resolver', function () { done(); }); }); -*/ + it('resolves a remote model property reference $ref', function (done) { var api = new Resolver(); var spec = {