Skip to content

Commit

Permalink
fix: resolve function recusion
Browse files Browse the repository at this point in the history
  • Loading branch information
pooja-bruno authored and helloanoop committed Feb 3, 2025
1 parent 08139a8 commit 650cb47
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions packages/bruno-app/src/utils/importers/openapi-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,52 +229,54 @@ const transformOpenapiRequestItem = (request) => {
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') {
return spec;
}

if (cache.has(spec)) {
return cache.get(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) {
const refPath = spec.$ref;

if (visitedItems.has(refPath)) {
return spec;
} else {
visitedItems.add(refPath);
if (cache.has(refPath)) {
return cache.get(refPath);
}

if (refPath.startsWith('#/components/')) {
// Local reference within components
const refKeys = refPath.replace('#/components/', '').split('/');
let ref = components;

for (const key of refKeys) {
if (ref && ref[key]) {
ref = ref[key];
} else {
// Handle invalid references gracefully?
return spec;
}
}

return resolveRefs(ref, components, visitedItems);
} else {
// Handle external references (not implemented here)
// You would need to fetch the external reference and resolve it.
// Example: Fetch and resolve an external reference from a URL.
cache.set(refPath, {});
const resolved = resolveRefs(ref, components, cache);
cache.set(refPath, resolved);
return resolved;
}
return spec;
}

// Recursively resolve references in nested objects
for (const prop in spec) {
spec[prop] = resolveRefs(spec[prop], components, new Set(visitedItems));
const resolved = {};
cache.set(spec, resolved);

for (const [key, value] of Object.entries(spec)) {
resolved[key] = resolveRefs(value, components, cache);
}

return spec;
return resolved;
};

const groupRequestsByTags = (requests) => {
Expand Down

0 comments on commit 650cb47

Please sign in to comment.