From d4e811c37cdb7b98bf84024d4d4cb5f021ef3694 Mon Sep 17 00:00:00 2001 From: Amar Zavery Date: Thu, 6 Apr 2017 13:19:46 -0700 Subject: [PATCH] working version --- lib/specResolver.js | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/lib/specResolver.js b/lib/specResolver.js index 37177ecc..d1f8a46d 100644 --- a/lib/specResolver.js +++ b/lib/specResolver.js @@ -16,9 +16,9 @@ var util = require('util'), * Resolves the swagger spec by unifying x-ms-paths, resolving relative file references if any, * resolving the allof is present in any model definition and then setting additionalProperties * to false if it is not previously set to true or an object in that definition. - */ + */ class SpecResolver { - + /* * @constructor * Initializes a new instance of the SpecResolver class. @@ -96,7 +96,7 @@ class SpecResolver { * If provided the value should be 'all'. This indicates that 'local' references should also be resolved apart from the default ones. * * @return {object} doc fully resolved json document - */ + */ resolveRelativePaths(doc, docPath, filterType) { let self = this; let docDir; @@ -168,7 +168,7 @@ class SpecResolver { let reference = node['$ref']; let parsedReference = utils.parseReferenceInSwagger(reference); let docDir = path.dirname(docPath); - + if (parsedReference.filePath) { //assuming that everything in the spec is relative to it, let us join the spec directory //and the file path in reference. @@ -194,7 +194,34 @@ class SpecResolver { referencedObj = utils.getObject(result, slicedLocalReferenceValue); utils.setObject(self.specInJson, slicedLocalReferenceValue, referencedObj); self.visitedEntities[slicedLocalReferenceValue] = referencedObj; - return Promise.resolve(self.resolveRelativePaths(referencedObj, docPath, 'all')); + return self.resolveRelativePaths(referencedObj, docPath, 'all').then(() => { + if (result && result.definitions) { + let unresolvedDefinitions = []; + for (let defName in result.definitions) { + if (result.definitions[defName].allOf) { + let matchFound = result.definitions[defName].allOf.some((item) => { + return (item['$ref'] === `#${slicedLocalReferenceValue}` && !self.visitedEntities[`/definitions/${defName}`]); + }); + if (matchFound) { + unresolvedDefinitions.push(defName); + } + } + } + if (unresolvedDefinitions.length) { + unresolvedDefinitions.forEach((definitionName) => { + let slicedDefinitionRef = `/definitions/${definitionName}`; + let definitionObj = result.definitions[definitionName]; + utils.setObject(self.specInJson, slicedDefinitionRef, definitionObj); + self.visitedEntities[slicedDefinitionRef] = definitionObj; + return self.resolveRelativePaths(definitionObj, docPath, 'all'); + }); + } else { + return Promise.resolve(); + } + } else { + return Promise.resolve(); + } + }); } else { return Promise.resolve(doc); } @@ -295,7 +322,7 @@ class SpecResolver { /* * Deletes all the references to allOf from all the model definitions in the swagger spec. - */ + */ deleteReferencesToAllOf() { let self = this; let spec = self.specInJson;