Fix Python JSON code example (#3159)

This commit is contained in:
Greg Bergé
2025-04-14 21:50:40 +02:00
committed by GitHub
parent 4b8a62122a
commit eeb977fc03
4 changed files with 40 additions and 12 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@gitbook/react-openapi": patch
---
Fix Python code example for JSON payload
@@ -413,13 +413,15 @@ describe('python code sample generator', () => {
}, },
body: { body: {
key: 'value', key: 'value',
truethy: true,
falsey: false,
}, },
}; };
const output = generator?.generate(input); const output = generator?.generate(input);
expect(output).toBe( expect(output).toBe(
'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()' '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()'
); );
}); });
+21 -8
View File
@@ -3,6 +3,7 @@ import {
isFormData, isFormData,
isFormUrlEncoded, isFormUrlEncoded,
isGraphQL, isGraphQL,
isJSON,
isPDF, isPDF,
isPlainObject, isPlainObject,
isText, isText,
@@ -173,11 +174,14 @@ ${headerString}${bodyString}`;
code += indent(`headers=${stringifyOpenAPI(headers)},\n`, 4); code += indent(`headers=${stringifyOpenAPI(headers)},\n`, 4);
} }
const contentType = headers?.['Content-Type'];
if (body) { if (body) {
if (body === 'files') { if (body === 'files') {
code += indent(`files=${body}\n`, 4); code += indent(`files=${body}\n`, 4);
} else if (isJSON(contentType)) {
code += indent(`data=json.dumps(${body})\n`, 4);
} else { } else {
code += indent(`data=${stringifyOpenAPI(body)}\n`, 4); code += indent(`data=${body}\n`, 4);
} }
} }
@@ -343,18 +347,27 @@ const BodyGenerators = {
} }
code += '}\n\n'; code += '}\n\n';
body = 'files'; body = 'files';
} } else if (isPDF(contentType)) {
if (isPDF(contentType)) {
code += 'files = {\n'; code += 'files = {\n';
code += `${indent(`"file": "${body}",`, 4)}\n`; code += `${indent(`"file": "${body}",`, 4)}\n`;
code += '}\n\n'; code += '}\n\n';
body = 'files'; body = 'files';
} } else if (isXML(contentType)) {
if (isXML(contentType)) {
// Convert JSON to XML if needed // Convert JSON to XML if needed
body = convertBodyToXML(body); body = JSON.stringify(convertBodyToXML(body));
} else {
body = stringifyOpenAPI(body, (_key, value) => {
switch (value) {
case true:
return '$$__TRUE__$$';
case false:
return '$$__FALSE__$$';
default:
return value;
}
})
.replaceAll('"$$__TRUE__$$"', 'True')
.replaceAll('"$$__FALSE__$$"', 'False');
} }
return { body, code, headers }; return { body, code, headers };
+11 -3
View File
@@ -1,17 +1,25 @@
/** /**
* Stringify an OpenAPI object. Same API as JSON.stringify. * Stringify an OpenAPI object. Same API as JSON.stringify.
*/ */
export function stringifyOpenAPI(body: unknown, _?: null, indent?: number): string { export function stringifyOpenAPI(
value: any,
replacer?: ((this: any, key: string, value: any) => any) | null,
space?: string | number
): string {
return JSON.stringify( return JSON.stringify(
body, value,
(key, value) => { (key, value) => {
// Ignore internal keys // Ignore internal keys
if (key.startsWith('x-gitbook-')) { if (key.startsWith('x-gitbook-')) {
return undefined; return undefined;
} }
if (replacer) {
return replacer(key, value);
}
return value; return value;
}, },
indent space
); );
} }