mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-12 05:48:57 +00:00
Fix Python JSON code example (#3159)
This commit is contained in:
@@ -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: {
|
||||
key: 'value',
|
||||
truethy: true,
|
||||
falsey: false,
|
||||
},
|
||||
};
|
||||
|
||||
const output = generator?.generate(input);
|
||||
|
||||
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()'
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
isFormData,
|
||||
isFormUrlEncoded,
|
||||
isGraphQL,
|
||||
isJSON,
|
||||
isPDF,
|
||||
isPlainObject,
|
||||
isText,
|
||||
@@ -173,11 +174,14 @@ ${headerString}${bodyString}`;
|
||||
code += indent(`headers=${stringifyOpenAPI(headers)},\n`, 4);
|
||||
}
|
||||
|
||||
const contentType = headers?.['Content-Type'];
|
||||
if (body) {
|
||||
if (body === 'files') {
|
||||
code += indent(`files=${body}\n`, 4);
|
||||
} else if (isJSON(contentType)) {
|
||||
code += indent(`data=json.dumps(${body})\n`, 4);
|
||||
} else {
|
||||
code += indent(`data=${stringifyOpenAPI(body)}\n`, 4);
|
||||
code += indent(`data=${body}\n`, 4);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,18 +347,27 @@ const BodyGenerators = {
|
||||
}
|
||||
code += '}\n\n';
|
||||
body = 'files';
|
||||
}
|
||||
|
||||
if (isPDF(contentType)) {
|
||||
} else if (isPDF(contentType)) {
|
||||
code += 'files = {\n';
|
||||
code += `${indent(`"file": "${body}",`, 4)}\n`;
|
||||
code += '}\n\n';
|
||||
body = 'files';
|
||||
}
|
||||
|
||||
if (isXML(contentType)) {
|
||||
} else if (isXML(contentType)) {
|
||||
// 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 };
|
||||
|
||||
@@ -1,17 +1,25 @@
|
||||
/**
|
||||
* 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(
|
||||
body,
|
||||
value,
|
||||
(key, value) => {
|
||||
// Ignore internal keys
|
||||
if (key.startsWith('x-gitbook-')) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (replacer) {
|
||||
return replacer(key, value);
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
indent
|
||||
space
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user