Skip to content

Commit

Permalink
feat: support circular references (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Dec 6, 2021
1 parent 177b66d commit d25a01d
Show file tree
Hide file tree
Showing 8 changed files with 2,017 additions and 4,664 deletions.
39 changes: 2 additions & 37 deletions filters/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const { default: AsyncApiComponent, hljs } = require('@asyncapi/react-component');
const { AsyncAPIDocument } = require('@asyncapi/parser');

const filter = module.exports;

Expand All @@ -20,42 +21,6 @@ function prepareConfiguration(params = {}) {
return config;
}

function replaceObject(val) {
return Object.entries(val).reduce((o, [propertyName, property]) => {
if (propertyName.startsWith('x-parser-')) {
o[propertyName] = property;
}
return o;
}, {});
}

/**
* Remove this function when it will be implemented https://github.com/asyncapi/parser-js/issues/266
*/
function replaceCircular(val, cache) {
cache = cache || new WeakSet();

if (val && typeof(val) == 'object') {
if (cache.has(val)) {
if (!Array.isArray(val)) {
return replaceObject(val);
}
return {};
}

cache.add(val);

const obj = (Array.isArray(val) ? [] : {});
for(var idx in val) {
obj[idx] = replaceCircular(val[idx], cache);
}

cache.delete(val);
return obj;
}
return val;
}

let initLanguages = false;
/**
* Load all language configurations from highlight.js
Expand Down Expand Up @@ -98,7 +63,7 @@ filter.includeFile = includeFile;
* and annotates that specification is parsed.
*/
function stringifySpec(asyncapi) {
return JSON.stringify(replaceCircular(asyncapi.json()));
return AsyncAPIDocument.stringify(asyncapi);
}
filter.stringifySpec = stringifySpec;

Expand Down
Loading

0 comments on commit d25a01d

Please sign in to comment.