Skip to content

Commit 3973b1c

Browse files
scazanemmerich
andauthored
Handle unresolved references in OpenAPI response' (#2811)
Co-authored-by: Steven H <[email protected]>
1 parent 160fca1 commit 3973b1c

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

packages/react-openapi/src/OpenAPIResponse.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import * as React from 'react';
21
import classNames from 'classnames';
32
import { OpenAPIV3 } from '@scalar/openapi-types';
4-
import { OpenAPIRootSchema, OpenAPISchemaProperties } from './OpenAPISchema';
5-
import { noReference } from './utils';
3+
import { OpenAPISchemaProperties } from './OpenAPISchema';
4+
import { checkIsReference, noReference } from './utils';
65
import { OpenAPIClientContext } from './types';
76
import { OpenAPIDisclosure } from './OpenAPIDisclosure';
87

@@ -38,13 +37,12 @@ export function OpenAPIResponse(props: {
3837
/>
3938
</OpenAPIDisclosure>
4039
) : null}
41-
4240
<div className={classNames('openapi-responsebody')}>
4341
<OpenAPISchemaProperties
4442
id={`response-${context.blockKey}`}
4543
properties={[
4644
{
47-
schema: noReference(mediaType.schema) ?? {},
45+
schema: handleUnresolvedReference(mediaType.schema) ?? {},
4846
},
4947
]}
5048
context={context}
@@ -53,3 +51,17 @@ export function OpenAPIResponse(props: {
5351
</div>
5452
);
5553
}
54+
55+
function handleUnresolvedReference(
56+
input: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined,
57+
): OpenAPIV3.SchemaObject {
58+
const isReference = checkIsReference(input);
59+
60+
if (isReference || input === undefined) {
61+
// If we find a reference that wasn't resolved or needed to be resolved externally, do not try to render it.
62+
// Instead we render `any`
63+
return {};
64+
}
65+
66+
return input;
67+
}

packages/react-openapi/src/OpenAPIResponseExample.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import * as React from 'react';
21
import { OpenAPIOperationData } from './fetchOpenAPIOperation';
32
import { generateSchemaExample } from './generateSchemaExample';
43
import { OpenAPIContextProps } from './types';
5-
import { noReference } from './utils';
4+
import { checkIsReference, noReference } from './utils';
65
import { stringifyOpenAPI } from './stringifyOpenAPI';
76
import { OpenAPIV3 } from '@scalar/openapi-types';
87
import { OpenAPITabs, OpenAPITabsList, OpenAPITabsPanels } from './OpenAPITabs';
@@ -62,7 +61,7 @@ export function OpenAPIResponseExample(props: {
6261
};
6362
}
6463

65-
const example: OpenAPIV3.ExampleObject | null = noReference(
64+
const example = handleUnresolvedReference(
6665
(() => {
6766
const { examples, example } = mediaTypeObject;
6867
if (examples) {
@@ -129,3 +128,16 @@ function OpenAPIEmptyResponseExample() {
129128
</pre>
130129
);
131130
}
131+
132+
function handleUnresolvedReference(
133+
input: OpenAPIV3.ExampleObject | null,
134+
): OpenAPIV3.ExampleObject | null {
135+
const isReference = checkIsReference(input?.value);
136+
137+
if (isReference) {
138+
// If we find a reference that wasn't resolved or needed to be resolved externally, render out the URL
139+
return { value: input.value.$ref };
140+
}
141+
142+
return input;
143+
}

0 commit comments

Comments
 (0)