Skip to content

Commit a0c06a7

Browse files
authored
Indent JSON python code sample (#3260)
1 parent 19c20c0 commit a0c06a7

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

.changeset/rotten-seals-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/react-openapi': patch
3+
---
4+
5+
Indent JSON python code sample

packages/react-openapi/src/code-samples.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ describe('python code sample generator', () => {
400400
const output = generator?.generate(input);
401401

402402
expect(output).toBe(
403-
'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()'
403+
'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()'
404404
);
405405
});
406406

@@ -422,7 +422,7 @@ describe('python code sample generator', () => {
422422
const output = generator?.generate(input);
423423

424424
expect(output).toBe(
425-
'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()'
425+
'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()'
426426
);
427427
});
428428

packages/react-openapi/src/code-samples.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -356,18 +356,22 @@ const BodyGenerators = {
356356
// Convert JSON to XML if needed
357357
body = JSON.stringify(convertBodyToXML(body));
358358
} else {
359-
body = stringifyOpenAPI(body, (_key, value) => {
360-
switch (value) {
361-
case true:
362-
return '$$__TRUE__$$';
363-
case false:
364-
return '$$__FALSE__$$';
365-
case null:
366-
return '$$__NULL__$$';
367-
default:
368-
return value;
369-
}
370-
})
359+
body = stringifyOpenAPI(
360+
body,
361+
(_key, value) => {
362+
switch (value) {
363+
case true:
364+
return '$$__TRUE__$$';
365+
case false:
366+
return '$$__FALSE__$$';
367+
case null:
368+
return '$$__NULL__$$';
369+
default:
370+
return value;
371+
}
372+
},
373+
2
374+
)
371375
.replaceAll('"$$__TRUE__$$"', 'True')
372376
.replaceAll('"$$__FALSE__$$"', 'False')
373377
.replaceAll('"$$__NULL__$$"', 'None');

0 commit comments

Comments
 (0)