ignore invalid nodes

ignore invalid nodes
This commit is contained in:
Luis Tejeda
2022-06-22 17:49:31 -05:00
parent 4dab3e028d
commit eed8a2c5d3
5 changed files with 224 additions and 66 deletions

1
bundled.json Normal file
View File

@@ -0,0 +1 @@
"{\"openapi\":\"3.0.0\",\"info\":{\"title\":\"Sample API\",\"description\":\"Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.\",\"version\":\"0.1.9\"},\"servers\":[{\"url\":\"http://api.example.com/v1\",\"description\":\"Optional server description, e.g. Main (production) server\"},{\"url\":\"http://staging-api.example.com\",\"description\":\"Optional server description, e.g. Internal staging server for testing\"}],\"paths\":{\"/users/{userId}\":{\"get\":{\"summary\":\"Get a user by ID\",\"parameters\":\"...\",\"responses\":{\"200\":{\"description\":\"A single user.\",\"content\":{\"application/json\":{\"schema\":{\"$ref\":\"#/components/schemas/User\"}}}},\"404\":{\"$ref\":\"#/components/responses/NotFound\"}}}},\"/users\":{\"get\":{\"summary\":\"Get all users\",\"responses\":{\"200\":{\"description\":\"A list of users.\",\"content\":{\"application/json\":{\"schema\":{\"type\":\"array\",\"items\":{\"$ref\":\"#/components/schemas/User\"}}}}}}}}},\"components\":{\"responses\":{\"$ref\":\"./responses.yaml\"},\"schemas\":{\"Monster\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"},\"clientName\":{\"type\":\"string\"}}},\"User\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"},\"userName\":{\"type\":\"string\"}}},\"Toy\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"},\"toyName\":{\"type\":\"string\"}}}}}}"

View File

@@ -46,6 +46,16 @@ function parseFileOrThrow(fileContent) {
return result; return result;
} }
/**
* 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 parseFile(fileContent) {
const result = parse.getOasObject(fileContent);
return result;
}
/** /**
* Calculates the path relative to parent * Calculates the path relative to parent
* @param {string} parentFileName - parent file name of the current node * @param {string} parentFileName - parent file name of the current node
@@ -270,13 +280,15 @@ function handleLocalCollisions(trace, initialMainKeys) {
* @param {object} version - The version of the spec we are bundling * @param {object} version - The version of the spec we are bundling
* @param {object} rootMainKeys - A dictionary with the component keys in local components object and its mainKeys * @param {object} rootMainKeys - A dictionary with the component keys in local components object and its mainKeys
* @param {string} commonPathFromData - The common path in the file's paths * @param {string} commonPathFromData - The common path in the file's paths
* @param {Array} allData - array of { path, content} objects
* @returns {object} - The references in current node and the new content from the node * @returns {object} - The references in current node and the new content from the node
*/ */
function getReferences (currentNode, isOutOfRoot, pathSolver, parentFilename, version, rootMainKeys, function getReferences (currentNode, isOutOfRoot, pathSolver, parentFilename, version, rootMainKeys,
commonPathFromData) { commonPathFromData, allData) {
let referencesInNode = [], let referencesInNode = [],
nodeReferenceDirectory = {}, nodeReferenceDirectory = {},
mainKeys = {}; mainKeys = {};
traverseUtility(currentNode).forEach(function (property) { traverseUtility(currentNode).forEach(function (property) {
if (property) { if (property) {
let hasReferenceTypeKey; let hasReferenceTypeKey;
@@ -307,11 +319,21 @@ function getReferences (currentNode, isOutOfRoot, pathSolver, parentFilename, ve
return item !== undefined; return item !== undefined;
}), this.key]; }), this.key];
let newValue, let newValue,
[, local] = tempRef.split(localPointer); [, local] = tempRef.split(localPointer),
nodeFromData,
refHasContent = false,
parseResult;
newValue = Object.assign({}, this.node); newValue = Object.assign({}, this.node);
newValue.$ref = referenceInDocument; nodeFromData = findNodeFromPath(tempRef, allData);
if (nodeFromData && nodeFromData.content) {
parseResult = parseFile(nodeFromData.content);
if (parseResult.result) {
newValue.$ref = referenceInDocument;
refHasContent = true;
nodeFromData.parsed = parseResult;
}
}
this.update({ $ref: tempRef }); this.update({ $ref: tempRef });
nodeReferenceDirectory[tempRef] = { nodeReferenceDirectory[tempRef] = {
@@ -321,7 +343,8 @@ function getReferences (currentNode, isOutOfRoot, pathSolver, parentFilename, ve
reference: referenceInDocument, reference: referenceInDocument,
traceToParent, traceToParent,
parentNodeKey: parentFilename, parentNodeKey: parentFilename,
mainKeyInTrace: nodeTrace[nodeTrace.length - 1] mainKeyInTrace: nodeTrace[nodeTrace.length - 1],
refHasContent
}; };
mainKeys[componentKey] = tempRef; mainKeys[componentKey] = tempRef;
@@ -349,13 +372,18 @@ function getReferences (currentNode, isOutOfRoot, pathSolver, parentFilename, ve
function getNodeContentAndReferences (currentNode, allData, specRoot, version, rootMainKeys, commonPathFromData) { function getNodeContentAndReferences (currentNode, allData, specRoot, version, rootMainKeys, commonPathFromData) {
let graphAdj = [], let graphAdj = [],
missingNodes = [], missingNodes = [],
nodeContent; nodeContent,
parseResult;
if (currentNode.parsed) { if (currentNode.parsed) {
nodeContent = currentNode.parsed.oasObject; nodeContent = currentNode.parsed.oasObject;
} }
else { else {
nodeContent = parseFileOrThrow(currentNode.content).oasObject; parseResult = parseFile(currentNode.content);
if (parseResult.result === false) {
return { graphAdj, missingNodes, undefined, nodeReferenceDirectory: {}, nodeName: currentNode.fileName };
}
nodeContent = parseResult.oasObject;
} }
const { referencesInNode, nodeReferenceDirectory } = getReferences( const { referencesInNode, nodeReferenceDirectory } = getReferences(
@@ -365,7 +393,8 @@ function getNodeContentAndReferences (currentNode, allData, specRoot, version, r
currentNode.fileName, currentNode.fileName,
version, version,
rootMainKeys, rootMainKeys,
commonPathFromData commonPathFromData,
allData
); );
referencesInNode.forEach((reference) => { referencesInNode.forEach((reference) => {
@@ -404,12 +433,14 @@ function generateComponentsObject (documentContext, rootContent, refTypeResolver
}); });
notInLine.forEach(([key, value]) => { notInLine.forEach(([key, value]) => {
let [, partial] = key.split(localPointer); let [, partial] = key.split(localPointer);
setValueInComponents( if (documentContext.globalReferences[key].refHasContent) {
value.keyInComponents, setValueInComponents(
components, value.keyInComponents,
getContentFromTrace(documentContext.nodeContents[key], partial), components,
version getContentFromTrace(documentContext.nodeContents[key], partial),
); version
);
}
}); });
[rootContent, components].forEach((contentData) => { [rootContent, components].forEach((contentData) => {
traverseUtility(contentData).forEach(function (property) { traverseUtility(contentData).forEach(function (property) {
@@ -429,12 +460,17 @@ function generateComponentsObject (documentContext, rootContent, refTypeResolver
return missingNode.path === nodeRef; return missingNode.path === nodeRef;
}); });
if (isMissingNode) { if (isMissingNode) {
refData.nodeContent = { // refData.nodeContent = {
[tempRef]: 'This related node was not found in provided data' // [tempRef]: 'This related node was not found in provided data'
}; // };
refData.inline = true; refData.nodeContent = refData.node;
// refData.inline = true;
this.update(refData.node);
refData.local = false; refData.local = false;
} }
if (!refData) {
return;
}
else { else {
refData.nodeContent = documentContext.nodeContents[nodeRef]; refData.nodeContent = documentContext.nodeContents[nodeRef];
refData.inline = refData.keyInComponents.length === 0; refData.inline = refData.keyInComponents.length === 0;
@@ -451,14 +487,18 @@ function generateComponentsObject (documentContext, rootContent, refTypeResolver
if (refData.inline) { if (refData.inline) {
refData.node = refData.nodeContent; refData.node = refData.nodeContent;
} }
this.update(refData.node); if (!isMissingNode) {
this.update(refData.node);
}
if (!refData.inline) { if (!refData.inline) {
setValueInComponents( if (documentContext.globalReferences[tempRef].refHasContent) {
refData.keyInComponents, setValueInComponents(
components, refData.keyInComponents,
refData.nodeContent, components,
version refData.nodeContent,
); version
);
}
} }
} }
} }

View File

@@ -4848,7 +4848,9 @@ module.exports = {
mapBundleOutput(format, parsedRootFiles) { mapBundleOutput(format, parsedRootFiles) {
return (contentAndComponents) => { return (contentAndComponents) => {
let bundledFile = contentAndComponents.fileContent; let bundledFile = contentAndComponents.fileContent;
bundledFile.components = contentAndComponents.components; if (!_.isEmpty(contentAndComponents.components)) {
bundledFile.components = contentAndComponents.components;
}
if (!format) { if (!format) {
let rootFormat = parsedRootFiles.find((inputRoot) => { let rootFormat = parsedRootFiles.find((inputRoot) => {
return inputRoot.fileName === contentAndComponents.fileName; return inputRoot.fileName === contentAndComponents.fileName;

View File

@@ -644,11 +644,9 @@ class SchemaPack {
path = pathBrowserify; path = pathBrowserify;
OasResolverOptions.browser = true; OasResolverOptions.browser = true;
} }
// if ('content' in input.data[0]) {
input.data.forEach((file) => { input.data.forEach((file) => {
files[path.resolve(file.fileName)] = file.content ? file.content : ''; files[path.resolve(file.fileName)] = file.content ? file.content : '';
}); });
// }
adaptedInput = schemaUtils.mapDetectRootFilesInputToGetRootFilesInput(input); adaptedInput = schemaUtils.mapDetectRootFilesInputToGetRootFilesInput(input);
adaptedInput.origin = input.origin; adaptedInput.origin = input.origin;
rootFiles = parse.getRootFiles(adaptedInput, concreteUtils.inputValidation, this.computedOptions, files, rootFiles = parse.getRootFiles(adaptedInput, concreteUtils.inputValidation, this.computedOptions, files,

View File

@@ -703,6 +703,8 @@ describe('bundle files method - 3.0', function () {
expect(res).to.not.be.empty; expect(res).to.not.be.empty;
expect(res.result).to.be.true; expect(res.result).to.be.true;
fs.writeFileSync('bundled.json', JSON.stringify(res.output.data[0].bundledContent));
expect(JSON.stringify(JSON.parse(res.output.data[0].bundledContent), null, 2)).to.be.equal(expected); expect(JSON.stringify(JSON.parse(res.output.data[0].bundledContent), null, 2)).to.be.equal(expected);
}); });
@@ -1938,43 +1940,6 @@ describe('bundle files method - 3.0', function () {
}); });
it('should ignore reference when is empty content', async function() {
let input =
{
type: 'multiFile',
specificationVersion: '3.0',
bundleFormat: 'JSON',
data: [
{
path: 'hello.yaml',
content: ''
},
{
path: 'openapi.yaml',
content: 'openapi: 3.0.0\n' +
'info:\n' +
' title: hello world\n' +
' version: 0.1.1\n' +
'paths:\n' +
' /hello:\n' +
' get:\n' +
' summary: get the hello\n' +
' responses:\n' +
' 200:\n' +
' description: sample des\n' +
' content:\n' +
' application/json:\n' +
' schema:\n' +
' $ref: \'./hello.yaml\''
}
]
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.specification.version).to.equal('3.0');
});
it('Should return bundled file as json - schema_collision_from_responses', async function () { it('Should return bundled file as json - schema_collision_from_responses', async function () {
let contentRootFile = fs.readFileSync(schemaCollision + '/root.yaml', 'utf8'), let contentRootFile = fs.readFileSync(schemaCollision + '/root.yaml', 'utf8'),
user = fs.readFileSync(schemaCollision + '/schemas_/_user.yaml', 'utf8'), user = fs.readFileSync(schemaCollision + '/schemas_/_user.yaml', 'utf8'),
@@ -2107,6 +2072,157 @@ describe('bundle files method - 3.0', function () {
expect(res.output.specification.version).to.equal('3.0'); expect(res.output.specification.version).to.equal('3.0');
expect(JSON.stringify(JSON.parse(res.output.data[0].bundledContent), null, 2)).to.be.equal(expected); expect(JSON.stringify(JSON.parse(res.output.data[0].bundledContent), null, 2)).to.be.equal(expected);
}); });
it('should ignore reference when is empty content and no root is sent', async function () {
let input =
{
type: 'multiFile',
specificationVersion: '3.0',
bundleFormat: 'YAML',
data: [
{
path: 'hello.yaml',
content: ''
},
{
path: 'openapi.yaml',
content: 'openapi: 3.0.0\n' +
'info:\n' +
' title: hello world\n' +
' version: 0.1.1\n' +
'paths:\n' +
' /hello:\n' +
' get:\n' +
' summary: get the hello\n' +
' responses:\n' +
' \'200\':\n' +
' description: sample des\n' +
' content:\n' +
' application/json:\n' +
' schema:\n' +
' $ref: ./hello.yaml\n'
}
]
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.specification.version).to.equal('3.0');
expect(res.output.data[0].bundledContent).to.be.equal(input.data[1].content);
});
it('should ignore reference when is empty content', async function () {
let input =
{
type: 'multiFile',
specificationVersion: '3.0',
bundleFormat: 'YAML',
rootFiles: [{ path: 'openapi.yaml' }],
data: [
{
path: 'hello.yaml',
content: ''
},
{
path: 'openapi.yaml',
content: 'openapi: 3.0.0\n' +
'info:\n' +
' title: hello world\n' +
' version: 0.1.1\n' +
'paths:\n' +
' /hello:\n' +
' get:\n' +
' summary: get the hello\n' +
' responses:\n' +
' \'200\':\n' +
' description: sample des\n' +
' content:\n' +
' application/json:\n' +
' schema:\n' +
' $ref: ./hello.yaml\n'
}
]
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.specification.version).to.equal('3.0');
expect(res.output.data[0].bundledContent).to.be.equal(input.data[1].content);
});
it('should ignore reference when is invalid content', async function () {
let input =
{
type: 'multiFile',
specificationVersion: '3.0',
bundleFormat: 'YAML',
rootFiles: [{ path: 'openapi.yaml' }],
data: [
{
path: 'hello.yaml',
content: 'asd'
},
{
path: 'openapi.yaml',
content: 'openapi: 3.0.0\n' +
'info:\n' +
' title: hello world\n' +
' version: 0.1.1\n' +
'paths:\n' +
' /hello:\n' +
' get:\n' +
' summary: get the hello\n' +
' responses:\n' +
' \'200\':\n' +
' description: sample des\n' +
' content:\n' +
' application/json:\n' +
' schema:\n' +
' $ref: ./hello.yaml\n'
}
]
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.specification.version).to.equal('3.0');
expect(res.output.data[0].bundledContent).to.be.equal(input.data[1].content);
});
it('should ignore reference when is invalid', async function () {
let input =
{
type: 'multiFile',
specificationVersion: '3.0',
bundleFormat: 'YAML',
rootFiles: [{ path: 'openapi.yaml' }],
data: [
{
path: 'openapi.yaml',
content: 'openapi: 3.0.0\n' +
'info:\n' +
' title: hello world\n' +
' version: 0.1.1\n' +
'paths:\n' +
' /hello:\n' +
' get:\n' +
' summary: get the hello\n' +
' responses:\n' +
' \'200\':\n' +
' description: sample des\n' +
' content:\n' +
' application/json:\n' +
' schema:\n' +
' $ref: ./hello.yaml\n'
}
]
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.specification.version).to.equal('3.0');
expect(res.output.data[0].bundledContent).to.be.equal(input.data[0].content);
});
}); });
@@ -2163,7 +2279,8 @@ describe('getReferences method when node does not have any reference', function(
'the/parent/filename', 'the/parent/filename',
'3.0', '3.0',
{}, {},
'' '',
[]
); );
expect(result.nodeReferenceDirectory).to.be.an('object'); expect(result.nodeReferenceDirectory).to.be.an('object');
expect(Object.keys(result.nodeReferenceDirectory).length).to.equal(1); expect(Object.keys(result.nodeReferenceDirectory).length).to.equal(1);