[BUG][PYTHON] Content-Type: application/json on GET(also HEAD,DELETE) requests
Created by: johnboyes
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
The same problem as #476, #1061 is also occurring in Python.
The Python generator makes the default Content-Type
application/json
for all HTTP methods (including GET
, HEAD
and DELETE
), whereas there should never be a Content-Type
set for GET
, HEAD
and DELETE
.
This causes a problem with other tools/servers that insist that GET
, HEAD
and DELETE
requests do not have a Content-Type
(as per the OpenAPI 3 specification).
An example of the problem is when using Prism as a validation proxy.
Prism rejects any GET
request that has a Content-Type
.
Here is an example of the problem manifesting itself.
openapi-generator version
master
969cea8c (21 June, 2021) and latest release at time of filing this issue v5.1.1
Not a regression, this is a long-standing issue.
OpenAPI declaration file content or url
Any GET, HEAD or DELETE request generated from any OpenAPI spec via the Python generator.
Generation Details
Standard Python config
Steps to reproduce
- Start with any OpenAPI3 spec e.g. the Petstore example at https://editor.swagger.io/
- Generate Python client code for the spec using the standard Python generator
- Look at the generated
rest.py
e.g. in the standard sample in this repo and see that theContent-Type
defaults toapplication/json
for all HTTP methods (includingGET
,HEAD
andDELETE
), rather than there being noContent-Type
forGET
,HEAD
andDELETE
.
Related issues/PRs
Suggest a fix
The fix looks straightforward and I am happy to submit a PR.
Proposed fix is to amend the rest.mustache so that the Content-Type
is left unset for GET
, HEAD
and DELETE
requests, e.g.
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS']:
if 'Content-Type' not in headers:
headers['Content-Type'] = 'application/json'
instead of the existing
if 'Content-Type' not in headers:
headers['Content-Type'] = 'application/json'