Skip to content

Commit 6e54a06

Browse files
authored
Support response examples in API blocks (#2799)
1 parent 1f8e416 commit 6e54a06

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

.changeset/chilly-readers-tap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/react-openapi': patch
3+
---
4+
5+
Support response examples

packages/react-openapi/src/OpenAPIResponseExample.tsx

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { generateSchemaExample } from './generateSchemaExample';
55
import { OpenAPIContextProps } from './types';
66
import { createStateKey, noReference } from './utils';
77
import { stringifyOpenAPI } from './stringifyOpenAPI';
8+
import { OpenAPIV3 } from '@scalar/openapi-types';
89

910
/**
1011
* Display an example of the response content.
@@ -39,39 +40,60 @@ export function OpenAPIResponseExample(props: {
3940
});
4041

4142
const examples = responses
42-
.map((response) => {
43-
const responseObject = noReference(response[1]);
43+
.map(([key, value]) => {
44+
const responseObject = noReference(value);
45+
const mediaTypeObject = (() => {
46+
if (!responseObject.content) {
47+
return null;
48+
}
49+
return (
50+
responseObject.content['application/json'] ??
51+
responseObject.content[Object.keys(responseObject.content)[0]]
52+
);
53+
})();
4454

45-
// TODO: unnecessary with https://github.com/GitbookIO/gitbook/pull/2780
46-
if (!responseObject) {
55+
if (!mediaTypeObject) {
4756
return null;
4857
}
4958

50-
const schema = noReference(
51-
(
52-
responseObject.content?.['application/json'] ??
53-
responseObject.content?.[Object.keys(responseObject.content)[0]]
54-
)?.schema,
55-
);
59+
const example: OpenAPIV3.ExampleObject | null = noReference(
60+
(() => {
61+
const { examples, example } = mediaTypeObject;
62+
if (examples) {
63+
const firstKey = Object.keys(examples)[0];
64+
// @TODO handle multiple examples
65+
const firstExample = noReference(examples[firstKey]);
66+
if (firstExample) {
67+
return firstExample;
68+
}
69+
}
5670

57-
if (!schema) {
58-
return null;
59-
}
71+
if (example) {
72+
return { value: example };
73+
}
74+
75+
const schema = noReference(mediaTypeObject.schema);
76+
if (!schema) {
77+
return null;
78+
}
79+
80+
return { value: generateSchemaExample(schema) };
81+
})(),
82+
);
6083

61-
const example = generateSchemaExample(schema);
62-
if (example === undefined) {
84+
if (!example?.value) {
6385
return null;
6486
}
6587

6688
return {
67-
key: `${response[0]}`,
68-
label: `${response[0]}`,
89+
key: key,
90+
label: key,
6991
body: (
7092
<context.CodeBlock
7193
code={
72-
typeof example === 'string'
73-
? example
74-
: stringifyOpenAPI(example, null, 2)
94+
typeof example.value === 'string'
95+
? example.value
96+
: stringifyOpenAPI(example.value, null, 2)
7597
}
7698
syntax="json"
7799
/>

0 commit comments

Comments
 (0)