fix: resolve function recusion

This commit is contained in:
Pooja Belaramani
2025-01-28 13:55:51 +05:30
parent fee631d496
commit d362216a68

View File

@ -229,26 +229,27 @@ const transformOpenapiRequestItem = (request) => {
return brunoRequestItem; return brunoRequestItem;
}; };
const resolveRefs = (spec, components = spec?.components, visitedItems = new Set()) => { const resolveRefs = (spec, components = spec?.components, cache = new Map()) => {
if (!spec || typeof spec !== 'object') { if (!spec || typeof spec !== 'object') {
return spec; return spec;
} }
if (cache.has(spec)) {
return cache.get(spec);
}
if (Array.isArray(spec)) { if (Array.isArray(spec)) {
return spec.map((item) => resolveRefs(item, components, visitedItems)); return spec.map(item => resolveRefs(item, components, cache));
} }
if ('$ref' in spec) { if ('$ref' in spec) {
const refPath = spec.$ref; const refPath = spec.$ref;
if (visitedItems.has(refPath)) { if (cache.has(refPath)) {
return spec; return cache.get(refPath);
} else {
visitedItems.add(refPath);
} }
if (refPath.startsWith('#/components/')) { if (refPath.startsWith('#/components/')) {
// Local reference within components
const refKeys = refPath.replace('#/components/', '').split('/'); const refKeys = refPath.replace('#/components/', '').split('/');
let ref = components; let ref = components;
@ -256,25 +257,26 @@ const resolveRefs = (spec, components = spec?.components, visitedItems = new Set
if (ref && ref[key]) { if (ref && ref[key]) {
ref = ref[key]; ref = ref[key];
} else { } else {
// Handle invalid references gracefully?
return spec; return spec;
} }
} }
return resolveRefs(ref, components, visitedItems); cache.set(refPath, {});
} else { const resolved = resolveRefs(ref, components, cache);
// Handle external references (not implemented here) cache.set(refPath, resolved);
// You would need to fetch the external reference and resolve it. return resolved;
// Example: Fetch and resolve an external reference from a URL.
} }
return spec;
} }
// Recursively resolve references in nested objects const resolved = {};
for (const prop in spec) { cache.set(spec, resolved);
spec[prop] = resolveRefs(spec[prop], components, new Set(visitedItems));
for (const [key, value] of Object.entries(spec)) {
resolved[key] = resolveRefs(value, components, cache);
} }
return spec; return resolved;
}; };
const groupRequestsByTags = (requests) => { const groupRequestsByTags = (requests) => {