Problem: Issue relates to OpenAPI specs which have self referencing components, resulting in infinite loops being made within resolveRefs.

Solution: Added basic use of Set to store already-traversed items within the OpenAPI spec.
Linked to personal use having import problems as well as this issue raised: https://github.com/usebruno/bruno/issues/939#issue-1986513743

Tested against the API Spec mentioned in the issue both as JSON and YAML.
This commit is contained in:
Art051 2023-11-14 18:28:35 +00:00
parent f617504cd6
commit f39d985877

View File

@ -187,18 +187,24 @@ const transformOpenapiRequestItem = (request) => {
return brunoRequestItem; return brunoRequestItem;
}; };
const resolveRefs = (spec, components = spec.components) => { const resolveRefs = (spec, components = spec.components, visitedItems = new Set()) => {
if (!spec || typeof spec !== 'object') { if (!spec || typeof spec !== 'object') {
return spec; return spec;
} }
if (Array.isArray(spec)) { if (Array.isArray(spec)) {
return spec.map((item) => resolveRefs(item, components)); return spec.map((item) => resolveRefs(item, components, visitedItems));
} }
if ('$ref' in spec) { if ('$ref' in spec) {
const refPath = spec.$ref; const refPath = spec.$ref;
if (visitedItems.has(refPath)) {
return spec;
} else {
visitedItems.add(refPath);
}
if (refPath.startsWith('#/components/')) { if (refPath.startsWith('#/components/')) {
// Local reference within components // Local reference within components
const refKeys = refPath.replace('#/components/', '').split('/'); const refKeys = refPath.replace('#/components/', '').split('/');
@ -213,7 +219,7 @@ const resolveRefs = (spec, components = spec.components) => {
} }
} }
return resolveRefs(ref, components); return resolveRefs(ref, components, visitedItems);
} else { } else {
// Handle external references (not implemented here) // Handle external references (not implemented here)
// You would need to fetch the external reference and resolve it. // You would need to fetch the external reference and resolve it.
@ -223,7 +229,7 @@ const resolveRefs = (spec, components = spec.components) => {
// Recursively resolve references in nested objects // Recursively resolve references in nested objects
for (const prop in spec) { for (const prop in spec) {
spec[prop] = resolveRefs(spec[prop], components); spec[prop] = resolveRefs(spec[prop], components, visitedItems);
} }
return spec; return spec;