Indent JSON python code sample (#3260)

This commit is contained in:
Nolann B.
2025-05-23 09:13:20 +02:00
committed by GitHub
parent 19c20c0be3
commit a0c06a72cc
3 changed files with 23 additions and 14 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@gitbook/react-openapi': patch
---
Indent JSON python code sample
@@ -400,7 +400,7 @@ describe('python code sample generator', () => {
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/x-www-form-urlencoded"},\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/x-www-form-urlencoded"},\n data={\n "key": "value"\n }\n)\n\ndata = response.json()'
); );
}); });
@@ -422,7 +422,7 @@ describe('python code sample generator', () => {
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=json.dumps({"key":"value","truethy":True,"falsey":False,"nullish":None})\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({\n "key": "value",\n "truethy": True,\n "falsey": False,\n "nullish": None\n })\n)\n\ndata = response.json()'
); );
}); });
+6 -2
View File
@@ -356,7 +356,9 @@ const BodyGenerators = {
// Convert JSON to XML if needed // Convert JSON to XML if needed
body = JSON.stringify(convertBodyToXML(body)); body = JSON.stringify(convertBodyToXML(body));
} else { } else {
body = stringifyOpenAPI(body, (_key, value) => { body = stringifyOpenAPI(
body,
(_key, value) => {
switch (value) { switch (value) {
case true: case true:
return '$$__TRUE__$$'; return '$$__TRUE__$$';
@@ -367,7 +369,9 @@ const BodyGenerators = {
default: default:
return value; return value;
} }
}) },
2
)
.replaceAll('"$$__TRUE__$$"', 'True') .replaceAll('"$$__TRUE__$$"', 'True')
.replaceAll('"$$__FALSE__$$"', 'False') .replaceAll('"$$__FALSE__$$"', 'False')
.replaceAll('"$$__NULL__$$"', 'None'); .replaceAll('"$$__NULL__$$"', 'None');