Skip to content

Commit d7948e3

Browse files
authored
Fix OpenAPI response showing as JSON instead of YAML (#3669)
1 parent 9f42211 commit d7948e3

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

.changeset/two-coats-trade.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+
Fix OpenAPI response showing as JSON instead of YAML when it should

packages/react-openapi/src/OpenAPIExample.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import yaml from 'js-yaml';
2+
13
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
24
import type { OpenAPIContext, OpenAPIUniversalContext } from './context';
35
import { json2xml } from './json2xml';
@@ -13,7 +15,7 @@ export function OpenAPIExample(props: {
1315
syntax: string;
1416
}) {
1517
const { example, context, syntax } = props;
16-
const code = stringifyExample({ example, xml: syntax === 'xml' });
18+
const code = stringifyExample({ example, syntax });
1719

1820
if (code === null) {
1921
return <OpenAPIEmptyExample context={context} />;
@@ -22,8 +24,10 @@ export function OpenAPIExample(props: {
2224
return context.renderCodeBlock({ code, syntax });
2325
}
2426

25-
function stringifyExample(args: { example: OpenAPIV3.ExampleObject; xml: boolean }): string | null {
26-
const { example, xml } = args;
27+
function stringifyExample(args: { example: OpenAPIV3.ExampleObject; syntax: string }):
28+
| string
29+
| null {
30+
const { example, syntax } = args;
2731

2832
if (!example.value) {
2933
return null;
@@ -33,10 +37,14 @@ function stringifyExample(args: { example: OpenAPIV3.ExampleObject; xml: boolean
3337
return example.value;
3438
}
3539

36-
if (xml) {
40+
if (syntax === 'xml') {
3741
return json2xml(example.value);
3842
}
3943

44+
if (syntax === 'yaml') {
45+
return yaml.dump(example.value).replace(/'/g, '').replace(/\\n/g, '\n');
46+
}
47+
4048
return stringifyOpenAPI(example.value, null, 2);
4149
}
4250

packages/react-openapi/src/util/example.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ function getSyntaxFromMediaType(mediaType: string): string {
121121
return 'json';
122122
}
123123

124+
if (mediaType.includes('yaml')) {
125+
return 'yaml';
126+
}
127+
124128
if (mediaType === 'application/xml') {
125129
return 'xml';
126130
}

0 commit comments

Comments
 (0)