Skip to content

Commit 500c4f4

Browse files
committed
Fix Python JSON code example
1 parent 4b8a621 commit 500c4f4

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

packages/react-openapi/src/code-samples.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,15 @@ describe('python code sample generator', () => {
413413
},
414414
body: {
415415
key: 'value',
416+
truethy: true,
417+
falsey: false,
416418
},
417419
};
418420

419421
const output = generator?.generate(input);
420422

421423
expect(output).toBe(
422-
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/json"},\n data={"key":"value"}\n)\n\ndata = response.json()'
424+
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/json"},\n data=json.dumps({"key":"value","truethy":True,"falsey":False})\n)\n\ndata = response.json()'
423425
);
424426
});
425427

packages/react-openapi/src/code-samples.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
isFormData,
44
isFormUrlEncoded,
55
isGraphQL,
6+
isJSON,
67
isPDF,
78
isPlainObject,
89
isText,
@@ -173,11 +174,14 @@ ${headerString}${bodyString}`;
173174
code += indent(`headers=${stringifyOpenAPI(headers)},\n`, 4);
174175
}
175176

177+
const contentType = headers?.['Content-Type'];
176178
if (body) {
177179
if (body === 'files') {
178180
code += indent(`files=${body}\n`, 4);
181+
} else if (isJSON(contentType)) {
182+
code += indent(`data=json.dumps(${body})\n`, 4);
179183
} else {
180-
code += indent(`data=${stringifyOpenAPI(body)}\n`, 4);
184+
code += indent(`data=${body}\n`, 4);
181185
}
182186
}
183187

@@ -343,18 +347,27 @@ const BodyGenerators = {
343347
}
344348
code += '}\n\n';
345349
body = 'files';
346-
}
347-
348-
if (isPDF(contentType)) {
350+
} else if (isPDF(contentType)) {
349351
code += 'files = {\n';
350352
code += `${indent(`"file": "${body}",`, 4)}\n`;
351353
code += '}\n\n';
352354
body = 'files';
353-
}
354-
355-
if (isXML(contentType)) {
355+
} else if (isXML(contentType)) {
356356
// Convert JSON to XML if needed
357-
body = convertBodyToXML(body);
357+
body = JSON.stringify(convertBodyToXML(body));
358+
} else {
359+
body = stringifyOpenAPI(body, (_key, value) => {
360+
switch (value) {
361+
case true:
362+
return '$$__TRUE__$$';
363+
case false:
364+
return '$$__FALSE__$$';
365+
default:
366+
return value;
367+
}
368+
})
369+
.replaceAll('"$$__TRUE__$$"', 'True')
370+
.replaceAll('"$$__FALSE__$$"', 'False');
358371
}
359372

360373
return { body, code, headers };
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
/**
22
* Stringify an OpenAPI object. Same API as JSON.stringify.
33
*/
4-
export function stringifyOpenAPI(body: unknown, _?: null, indent?: number): string {
4+
export function stringifyOpenAPI(
5+
value: any,
6+
replacer?: ((this: any, key: string, value: any) => any) | null,
7+
space?: string | number
8+
): string {
59
return JSON.stringify(
6-
body,
10+
value,
711
(key, value) => {
812
// Ignore internal keys
913
if (key.startsWith('x-gitbook-')) {
1014
return undefined;
1115
}
1216

17+
if (replacer) {
18+
return replacer(key, value);
19+
}
20+
1321
return value;
1422
},
15-
indent
23+
space
1624
);
1725
}

0 commit comments

Comments
 (0)