Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/two-coats-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gitbook/react-openapi": patch
---

Fix OpenAPI response showing as JSON instead of YAML when it should
16 changes: 12 additions & 4 deletions packages/react-openapi/src/OpenAPIExample.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import yaml from 'js-yaml';

import type { OpenAPIV3 } from '@gitbook/openapi-parser';
import type { OpenAPIContext, OpenAPIUniversalContext } from './context';
import { json2xml } from './json2xml';
Expand All @@ -13,7 +15,7 @@ export function OpenAPIExample(props: {
syntax: string;
}) {
const { example, context, syntax } = props;
const code = stringifyExample({ example, xml: syntax === 'xml' });
const code = stringifyExample({ example, syntax });

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

function stringifyExample(args: { example: OpenAPIV3.ExampleObject; xml: boolean }): string | null {
const { example, xml } = args;
function stringifyExample(args: { example: OpenAPIV3.ExampleObject; syntax: string }):
| string
| null {
const { example, syntax } = args;

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

if (xml) {
if (syntax === 'xml') {
return json2xml(example.value);
}

if (syntax === 'yaml') {
return yaml.dump(example.value).replace(/'/g, '').replace(/\\n/g, '\n');
}

return stringifyOpenAPI(example.value, null, 2);
}

Expand Down
4 changes: 4 additions & 0 deletions packages/react-openapi/src/util/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ function getSyntaxFromMediaType(mediaType: string): string {
return 'json';
}

if (mediaType.includes('yaml')) {
return 'yaml';
}

if (mediaType === 'application/xml') {
return 'xml';
}
Expand Down