Skip to content

Commit

Permalink
Handle unresolved references in OpenAPI response' (#2811)
Browse files Browse the repository at this point in the history
Co-authored-by: Steven H <steven@gitbook.io>
  • Loading branch information
scazan and emmerich authored Feb 6, 2025
1 parent 160fca1 commit 3973b1c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
22 changes: 17 additions & 5 deletions packages/react-openapi/src/OpenAPIResponse.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -38,13 +37,12 @@ export function OpenAPIResponse(props: {
/>
</OpenAPIDisclosure>
) : null}

<div className={classNames('openapi-responsebody')}>
<OpenAPISchemaProperties
id={`response-${context.blockKey}`}
properties={[
{
schema: noReference(mediaType.schema) ?? {},
schema: handleUnresolvedReference(mediaType.schema) ?? {},
},
]}
context={context}
Expand All @@ -53,3 +51,17 @@ export function OpenAPIResponse(props: {
</div>
);
}

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;
}
18 changes: 15 additions & 3 deletions packages/react-openapi/src/OpenAPIResponseExample.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -62,7 +61,7 @@ export function OpenAPIResponseExample(props: {
};
}

const example: OpenAPIV3.ExampleObject | null = noReference(
const example = handleUnresolvedReference(
(() => {
const { examples, example } = mediaTypeObject;
if (examples) {
Expand Down Expand Up @@ -129,3 +128,16 @@ function OpenAPIEmptyResponseExample() {
</pre>
);
}

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;
}

0 comments on commit 3973b1c

Please sign in to comment.