From 6963e8d35990f2b81bd2b352d188a5d9099f52c7 Mon Sep 17 00:00:00 2001 From: Viacheslav Turovskyi Date: Tue, 19 Jul 2022 06:37:57 +0300 Subject: [PATCH] feat: parseFromUrl does not resolve relative references (#504) --- lib/parser.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index f756fcae9..e8d9049ad 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -51,12 +51,13 @@ module.exports = { * @param {ParserOptions=} options Configuration options object {@link #asyncapiparserparseroptions--object|ParserOptions} * @returns {Promise} The parsed AsyncAPI document. */ -async function parse(asyncapiYAMLorJSON, options = {}) { +async function parse(asyncapiYAMLorJSON, options = {}, urlRegExped) { let parsedJSON; let initialFormat; - options.path = options.path || `${process.cwd()}${path.sep}`; - + options.path = options.path || urlRegExped || `${process.cwd()}${path.sep}`; + options.resolve = { external: true, file: true, http: true }; + try { ({ initialFormat, parsedJSON } = toJS(asyncapiYAMLorJSON)); @@ -133,10 +134,22 @@ function parseFromUrl(url, fetchOptions, options) { //To not break the API by changing argument position and to silet the linter it is just better to move adding if (!fetchOptions) fetchOptions = {}; + // RegExp, which chooses name of the file with spec in the URL string, is created. + const urlRegExp = /[^/]+$/g; + // When executed in multiple async calls, 'checkErrorWrapper' passes URL + // string as an object by reference. In this case 'String.replace()' method + // mutates original URL string causing test with multiple async calls to fail. + // Thus completely independent object containing URL string is created for + // future manipulation. + const urlCopy = new String(url); + // In the copy of the URL string, name of the file with spec is removed, + // leaving only absolute URL, which is later passed through thenable chain. + const urlRegExped = urlCopy.replace(urlRegExp.exec(urlCopy), ''); + return new Promise((resolve, reject) => { fetch(url, fetchOptions) .then(res => res.text()) - .then(doc => parse(doc, options)) + .then(doc => parse(doc, options, urlRegExped)) .then(result => resolve(result)) .catch(e => { if (e instanceof ParserError) return reject(e);