Skip to content

fix: resolve function recusion #3895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading