From f39d9858776c1ae1cef90f07c4500fcd47e7374b Mon Sep 17 00:00:00 2001 From: Art051 <91856576+Art051@users.noreply.github.com> Date: Tue, 14 Nov 2023 18:28:35 +0000 Subject: [PATCH] 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. --- .../src/utils/importers/openapi-collection.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/bruno-app/src/utils/importers/openapi-collection.js b/packages/bruno-app/src/utils/importers/openapi-collection.js index d317fdfe4..febe7d61d 100644 --- a/packages/bruno-app/src/utils/importers/openapi-collection.js +++ b/packages/bruno-app/src/utils/importers/openapi-collection.js @@ -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;