Adding safe parsing to the node's content parsing

This commit is contained in:
Erik Mendoza
2022-06-02 14:29:03 -05:00
parent 462c4dfd14
commit c82d50ed8d
2 changed files with 19 additions and 14 deletions

View File

@@ -12,7 +12,8 @@ const { COMPONENTS_KEYS_30 } = require('./30XUtils/componentsParentMatcher'),
} = require('./jsonPointer'),
traverseUtility = require('traverse'),
parse = require('./parse.js'),
{ COMPONENTS_KEYS_20 } = require('./swaggerUtils/componentParentMatcher');
{ COMPONENTS_KEYS_20 } = require('./swaggerUtils/componentParentMatcher'),
{ ParseError } = require('./common/ParseError');
let path = require('path'),
pathBrowserify = require('path-browserify'),
@@ -32,6 +33,18 @@ function comparePaths(path1, path2) {
return path1 === path2;
}
/**
* Parses a node content or throw ParseError if there's any error
* @param {string} fileContent The content from the current node
* @returns {object} The parsed content
*/
function parseFileOrThrow(fileContent) {
const result = parse.getOasObject(fileContent);
if (result.result === false) {
throw new ParseError(result.reason);
}
return result;
}
/**
* Calculates the path relative to parent
@@ -279,7 +292,7 @@ function getNodeContentAndReferences (currentNode, allData, specRoot, version) {
nodeContent = currentNode.parsed.oasObject;
}
else {
nodeContent = parse.getOasObject(currentNode.content).oasObject;
nodeContent = parseFileOrThrow(currentNode.content).oasObject;
}
const { referencesInNode, nodeReferenceDirectory } = getReferences(
@@ -433,5 +446,6 @@ module.exports = {
components
};
},
getReferences
getReferences,
parseFileOrThrow
};

View File

@@ -92,7 +92,7 @@ const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/sc
{ getRelatedFiles } = require('./relatedFiles'),
{ compareVersion } = require('./common/versionUtils.js'),
parse = require('./parse'),
{ getBundleContentAndComponents } = require('./bundle.js');
{ getBundleContentAndComponents, parseFileOrThrow } = require('./bundle.js');
/* eslint-enable */
// See https://github.com/json-schema-faker/json-schema-faker/tree/master/docs#available-options
@@ -4867,7 +4867,7 @@ module.exports = {
mapProcessRelatedFiles(rootFiles, inputData, origin, version, format, toBundle = false) {
let bundleFormat = format,
parsedRootFiles = rootFiles.map((rootFile) => {
let parsedContent = this.parseFileOrThrow(rootFile.content);
let parsedContent = parseFileOrThrow(rootFile.content);
return { fileName: rootFile.fileName, content: rootFile.content, parsed: parsedContent };
}).filter((rootWithParsedContent) => {
let fileVersion = version === SWAGGER_VERSION ?
@@ -4931,14 +4931,5 @@ module.exports = {
else {
return res;
}
},
parseFileOrThrow(fileContent) {
const result = parse.getOasObject(fileContent);
if (result.result === false) {
throw new ParseError(result.reason);
}
return result;
}
};