Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CUE-5523] Fix Root file detection for multi-file OAS import #835

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,93 @@ module.exports = {
throw new Error(e.message);
}
});

// there can be only one root file and the root file should not be referenced by any other file.
if (rootFilesArray.length > 1) {
let fileRefPathObj = {};

rootFilesArray.forEach((file) => {
fileRefPathObj[file] = 0;

let obj,
oasObject,
refFilePaths;
// Use files map if present to read files.
if (!_.isEmpty(files)) {
obj = files[path.resolve(file)];
}
else if (allowReadingFS) {
obj = fs.readFileSync(file, 'utf8');
}

obj = this.getOasObject(obj);

if (obj.result) {
oasObject = obj.oasObject;
}

refFilePaths = this.getRefFilePaths(oasObject, file);
refFilePaths.forEach((refFilePath) => {
if (refFilePath in fileRefPathObj) {
fileRefPathObj[refFilePath] += 1;
}
});
});

// Get the root files
rootFilesArray = Object.keys(fileRefPathObj).filter((file) => {
if (fileRefPathObj[file] === 0) {
return file;
}
});
}
return rootFilesArray;
},

/** Given an OAS object and a file path, returns the file paths of the referenced files
* @param {Object} oasObject OAS object
* @param {String} filePath file path
* @return {Array} refFilePaths
* */
getRefFilePaths: function (oasObject, filePath) {
let refFilePaths = [];
if (oasObject.hasOwnProperty('$ref')) {
refFilePaths.push(path.resolve(path.dirname(filePath), oasObject.$ref));
}
else {
let refFilePathsArray = [];
this.getRefFilePathsHelper(oasObject, filePath, refFilePathsArray);
refFilePaths = refFilePathsArray;
}
return refFilePaths;
},

/** Helper function for getRefFilePaths
* @param {Object} oasObject OAS object
* @param {String} filePath file path
* @param {Array} refFilePathsArray array to store file paths
* @return {Array} refFilePaths
* */
getRefFilePathsHelper: function (oasObject, filePath, refFilePathsArray) {
let refFilePaths = [];
if (oasObject.hasOwnProperty('$ref')) {
if (!oasObject.$ref.startsWith('#') && !oasObject.$ref.startsWith('http')) {
let resolvedFilePath = path.resolve(path.dirname(filePath), oasObject.$ref.split('#')[0]);
if (resolvedFilePath !== filePath) {
refFilePaths.push(resolvedFilePath);
}
}
}
else {
Object.keys(oasObject).forEach((key) => {
if (typeof oasObject[key] === 'object') {
this.getRefFilePathsHelper(oasObject[key], filePath, refFilePathsArray);
}
});
}
refFilePathsArray.push(...refFilePaths);
},

/**
* Resolve file references and generate single OAS Object
*
Expand Down
Loading