diff --git a/packages/react-openapi/src/OpenAPIResponse.tsx b/packages/react-openapi/src/OpenAPIResponse.tsx index a51a7f82e..a2f0d3701 100644 --- a/packages/react-openapi/src/OpenAPIResponse.tsx +++ b/packages/react-openapi/src/OpenAPIResponse.tsx @@ -1,8 +1,7 @@ -import * as React from 'react'; import classNames from 'classnames'; import { OpenAPIV3 } from '@scalar/openapi-types'; -import { OpenAPIRootSchema, OpenAPISchemaProperties } from './OpenAPISchema'; -import { noReference } from './utils'; +import { OpenAPISchemaProperties } from './OpenAPISchema'; +import { checkIsReference, noReference } from './utils'; import { OpenAPIClientContext } from './types'; import { OpenAPIDisclosure } from './OpenAPIDisclosure'; @@ -38,13 +37,12 @@ export function OpenAPIResponse(props: { /> ) : null} -
); } + +function handleUnresolvedReference( + input: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined, +): OpenAPIV3.SchemaObject { + const isReference = checkIsReference(input); + + if (isReference || input === undefined) { + // If we find a reference that wasn't resolved or needed to be resolved externally, do not try to render it. + // Instead we render `any` + return {}; + } + + return input; +} diff --git a/packages/react-openapi/src/OpenAPIResponseExample.tsx b/packages/react-openapi/src/OpenAPIResponseExample.tsx index ce9a79b47..8100ed4d9 100644 --- a/packages/react-openapi/src/OpenAPIResponseExample.tsx +++ b/packages/react-openapi/src/OpenAPIResponseExample.tsx @@ -1,8 +1,7 @@ -import * as React from 'react'; import { OpenAPIOperationData } from './fetchOpenAPIOperation'; import { generateSchemaExample } from './generateSchemaExample'; import { OpenAPIContextProps } from './types'; -import { noReference } from './utils'; +import { checkIsReference, noReference } from './utils'; import { stringifyOpenAPI } from './stringifyOpenAPI'; import { OpenAPIV3 } from '@scalar/openapi-types'; import { OpenAPITabs, OpenAPITabsList, OpenAPITabsPanels } from './OpenAPITabs'; @@ -62,7 +61,7 @@ export function OpenAPIResponseExample(props: { }; } - const example: OpenAPIV3.ExampleObject | null = noReference( + const example = handleUnresolvedReference( (() => { const { examples, example } = mediaTypeObject; if (examples) { @@ -129,3 +128,16 @@ function OpenAPIEmptyResponseExample() { ); } + +function handleUnresolvedReference( + input: OpenAPIV3.ExampleObject | null, +): OpenAPIV3.ExampleObject | null { + const isReference = checkIsReference(input?.value); + + if (isReference) { + // If we find a reference that wasn't resolved or needed to be resolved externally, render out the URL + return { value: input.value.$ref }; + } + + return input; +}