Skip to content

Commit 631cecc

Browse files
committed
Better support for python json
1 parent 0bef455 commit 631cecc

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ describe('python code sample generator', () => {
366366
const output = generator?.generate(input);
367367

368368
expect(output).toBe(
369-
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/x-www-form-urlencoded"},\n json={"key":"value"}\n)\n\ndata = response.json()'
369+
'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()'
370370
);
371371
});
372372

@@ -402,7 +402,7 @@ describe('python code sample generator', () => {
402402
const output = generator?.generate(input);
403403

404404
expect(output).toBe(
405-
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/xml"},\n json="<key>value</key>"\n)\n\ndata = response.json()'
405+
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/xml"},\n data="<key>value</key>"\n)\n\ndata = response.json()'
406406
);
407407
});
408408

@@ -419,7 +419,7 @@ describe('python code sample generator', () => {
419419
const output = generator?.generate(input);
420420

421421
expect(output).toBe(
422-
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/graphql"},\n json="{ key }"\n)\n\ndata = response.json()'
422+
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/json"},\n json="{ key }"\n)\n\ndata = response.json()'
423423
);
424424
});
425425

@@ -436,7 +436,7 @@ describe('python code sample generator', () => {
436436
const output = generator?.generate(input);
437437

438438
expect(output).toBe(
439-
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"text/csv"},\n json="key,value"\n)\n\ndata = response.json()'
439+
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"text/csv"},\n data="key,value"\n)\n\ndata = response.json()'
440440
);
441441
});
442442

@@ -470,7 +470,7 @@ describe('python code sample generator', () => {
470470
const output = generator?.generate(input);
471471

472472
expect(output).toBe(
473-
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"text/plain"},\n json="value"\n)\n\ndata = response.json()'
473+
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"text/plain"},\n data="value"\n)\n\ndata = response.json()'
474474
);
475475
});
476476

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,16 @@ export const codeSampleGenerators: CodeSampleGenerator[] = [
126126
code += indent(`headers=${stringifyOpenAPI(headers)},\n`, 4);
127127
}
128128

129+
const contentType = headers?.['Content-Type'] || '';
130+
129131
if (body) {
130132
if (body === 'files') {
131133
code += indent(`files=${body}\n`, 4);
132-
} else {
134+
} else if (contentType === 'application/json') {
135+
// If the content type is JSON, we use json={}
133136
code += indent(`json=${stringifyOpenAPI(body)}\n`, 4);
137+
} else {
138+
code += indent(`data=${stringifyOpenAPI(body)}\n`, 4);
134139
}
135140
}
136141

@@ -326,7 +331,9 @@ const BodyGenerators = {
326331
getPythonBody: (body: any, headers?: Record<string, string>) => {
327332
if (!body || !headers) return;
328333
let code = '';
329-
const contentType: string = headers['Content-Type'] || '';
334+
// Copy headers to avoid mutating the original object
335+
const headersCopy = { ...headers };
336+
const contentType: string = headersCopy['Content-Type'] || '';
330337

331338
if (isFormData(contentType)) {
332339
code += 'files = {\n';
@@ -346,7 +353,12 @@ const BodyGenerators = {
346353
body = 'files';
347354
}
348355

349-
return { body, code, headers };
356+
if (isGraphQL(contentType)) {
357+
// Set Content-Type to application/json for GraphQL, recommended by GraphQL spec
358+
headersCopy['Content-Type'] = 'application/json';
359+
}
360+
361+
return { body, code, headers: headersCopy };
350362
},
351363
getHTTPBody: (body: any, headers?: Record<string, string>) => {
352364
if (!body || !headers) return undefined;

0 commit comments

Comments
 (0)