diff --git a/.changeset/fresh-ads-approve.md b/.changeset/fresh-ads-approve.md new file mode 100644 index 000000000..921797bcd --- /dev/null +++ b/.changeset/fresh-ads-approve.md @@ -0,0 +1,5 @@ +--- +"@gitbook/react-openapi": patch +--- + +Fix Python code example for JSON payload diff --git a/packages/react-openapi/src/code-samples.test.ts b/packages/react-openapi/src/code-samples.test.ts index adcb3922a..363baf552 100644 --- a/packages/react-openapi/src/code-samples.test.ts +++ b/packages/react-openapi/src/code-samples.test.ts @@ -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()' ); }); diff --git a/packages/react-openapi/src/code-samples.ts b/packages/react-openapi/src/code-samples.ts index 7bd45e614..fe44da11d 100644 --- a/packages/react-openapi/src/code-samples.ts +++ b/packages/react-openapi/src/code-samples.ts @@ -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 }; diff --git a/packages/react-openapi/src/stringifyOpenAPI.ts b/packages/react-openapi/src/stringifyOpenAPI.ts index c0d070c2d..3fb2853de 100644 --- a/packages/react-openapi/src/stringifyOpenAPI.ts +++ b/packages/react-openapi/src/stringifyOpenAPI.ts @@ -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 ); }