diff --git a/lib/parser.js b/lib/parser.js index f756fcae9..17e857dc7 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -51,11 +51,11 @@ 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}`; try { ({ initialFormat, parsedJSON } = toJS(asyncapiYAMLorJSON)); @@ -133,10 +133,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);