Skip to content

Fix Python JSON code example #3159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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/fresh-ads-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gitbook/react-openapi": patch
---

Fix Python code example for JSON payload
4 changes: 3 additions & 1 deletion packages/react-openapi/src/code-samples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()'
);
});

Expand Down
29 changes: 21 additions & 8 deletions packages/react-openapi/src/code-samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
isFormData,
isFormUrlEncoded,
isGraphQL,
isJSON,
isPDF,
isPlainObject,
isText,
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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 };
Expand Down
14 changes: 11 additions & 3 deletions packages/react-openapi/src/stringifyOpenAPI.ts
Original file line number Diff line number Diff line change
@@ -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
);
}