Merge pull request #965 from Art051/bugfix/openapi-import-selfref-infinite-loop

Fix inifinite loop in openapi-spec import with self-references
This commit is contained in:
Anoop M D 2023-11-18 09:24:02 +05:30 committed by GitHub
commit 3bd7bc75a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -187,18 +187,24 @@ const transformOpenapiRequestItem = (request) => {
return brunoRequestItem;
};
const resolveRefs = (spec, components = spec.components) => {
const resolveRefs = (spec, components = spec.components, visitedItems = new Set()) => {
if (!spec || typeof spec !== 'object') {
return spec;
}
if (Array.isArray(spec)) {
return spec.map((item) => resolveRefs(item, components));
return spec.map((item) => resolveRefs(item, components, visitedItems));
}
if ('$ref' in spec) {
const refPath = spec.$ref;
if (visitedItems.has(refPath)) {
return spec;
} else {
visitedItems.add(refPath);
}
if (refPath.startsWith('#/components/')) {
// Local reference within components
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 {
// Handle external references (not implemented here)
// 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
for (const prop in spec) {
spec[prop] = resolveRefs(spec[prop], components);
spec[prop] = resolveRefs(spec[prop], components, visitedItems);
}
return spec;