Skip to content
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

[Python] remove default content-type in the request #10782

Merged
merged 11 commits into from
Feb 24, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,12 @@ class {{classname}}(object):
{{/hasProduces}}
{{#hasConsumes}}
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
[{{#consumes}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/consumes}}],
'{{httpMethod}}', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

{{/hasConsumes}}
# Authentication setting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ class ApiClient(object):
:return: Content-Type (e.g. application/json).
"""
if not content_types:
return 'application/json'
Copy link
Contributor

@spacether spacether Nov 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am concerned that this could be a breaking change for servers/client that depended upon this functionality.
How about adding a generator argument useJsonAsDefaultContentType and default it to True.
With that we could put your PR in a patch release.
If you want to default it to False, then this will need to be released as a minor release because it would be a breaking change with a fallback.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am concerned that this could be a breaking change for servers/client that depended upon this functionality.
How about adding a generator argument useJsonAsDefaultContentType and default it to True.

We've done something similar in other generators (e.g. Ruby) to remove such default behaviour (which was added before I worked on this project). No complains so far.

return None

content_types = [x.lower() for x in content_types]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ class ApiClient(object):
:return: Content-Type (e.g. application/json).
"""
if not content_types:
return 'application/json'
return None

content_types = [x.lower() for x in content_types]

Expand Down Expand Up @@ -863,10 +863,11 @@ class Endpoint(object):
content_type_headers_list = self.headers_map['content_type']
if content_type_headers_list:
if params['body'] != "":
header_list = self.api_client.select_header_content_type(
content_types_list = self.api_client.select_header_content_type(
content_type_headers_list, self.settings['http_method'],
params['body'])
params['header']['Content-Type'] = header_list
if content_types_list:
params['header']['Content-Type'] = content_types_list

return self.api_client.call_api(
self.settings['endpoint_path'], self.settings['http_method'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,12 @@ def call_123_test_special_tags_with_http_info(self, body, **kwargs): # noqa: E5
['application/json']) # noqa: E501

# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json'],
'PATCH', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,12 @@ def create_xml_item_with_http_info(self, xml_item, **kwargs): # noqa: E501
if 'xml_item' in local_var_params:
body_params = local_var_params['xml_item']
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/xml', 'application/xml; charset=utf-8', 'application/xml; charset=utf-16', 'text/xml', 'text/xml; charset=utf-8', 'text/xml; charset=utf-16'],
'POST', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down Expand Up @@ -821,10 +823,12 @@ def test_body_with_file_schema_with_http_info(self, body, **kwargs): # noqa: E5
if 'body' in local_var_params:
body_params = local_var_params['body']
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json'],
'PUT', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down Expand Up @@ -968,10 +972,12 @@ def test_body_with_query_params_with_http_info(self, query, body, **kwargs): #
if 'body' in local_var_params:
body_params = local_var_params['body']
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json'],
'PUT', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down Expand Up @@ -1110,10 +1116,12 @@ def test_client_model_with_http_info(self, body, **kwargs): # noqa: E501
['application/json']) # noqa: E501

# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json'],
'PATCH', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down Expand Up @@ -1381,10 +1389,12 @@ def test_endpoint_parameters_with_http_info(self, number, double, pattern_withou

body_params = None
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/x-www-form-urlencoded'],
'POST', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = ['http_basic_test'] # noqa: E501
Expand Down Expand Up @@ -1567,10 +1577,12 @@ def test_enum_parameters_with_http_info(self, **kwargs): # noqa: E501

body_params = None
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/x-www-form-urlencoded'],
'GET', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down Expand Up @@ -1878,10 +1890,12 @@ def test_inline_additional_properties_with_http_info(self, param, **kwargs): #
if 'param' in local_var_params:
body_params = local_var_params['param']
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json'],
'POST', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down Expand Up @@ -2025,10 +2039,12 @@ def test_json_form_data_with_http_info(self, param, param2, **kwargs): # noqa:

body_params = None
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/x-www-form-urlencoded'],
'GET', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,12 @@ def test_classname_with_http_info(self, body, **kwargs): # noqa: E501
['application/json']) # noqa: E501

# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json'],
'PATCH', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = ['api_key_query'] # noqa: E501
Expand Down
20 changes: 15 additions & 5 deletions samples/client/petstore/python-asyncio/petstore_api/api/pet_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ def add_pet_with_http_info(self, body, **kwargs): # noqa: E501
if 'body' in local_var_params:
body_params = local_var_params['body']
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json', 'application/xml'],
'POST', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = ['petstore_auth'] # noqa: E501
Expand Down Expand Up @@ -838,10 +840,12 @@ def update_pet_with_http_info(self, body, **kwargs): # noqa: E501
if 'body' in local_var_params:
body_params = local_var_params['body']
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json', 'application/xml'],
'PUT', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = ['petstore_auth'] # noqa: E501
Expand Down Expand Up @@ -988,10 +992,12 @@ def update_pet_with_form_with_http_info(self, pet_id, **kwargs): # noqa: E501

body_params = None
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/x-www-form-urlencoded'],
'POST', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = ['petstore_auth'] # noqa: E501
Expand Down Expand Up @@ -1142,10 +1148,12 @@ def upload_file_with_http_info(self, pet_id, **kwargs): # noqa: E501
['application/json']) # noqa: E501

# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['multipart/form-data'],
'POST', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = ['petstore_auth'] # noqa: E501
Expand Down Expand Up @@ -1302,10 +1310,12 @@ def upload_file_with_required_file_with_http_info(self, pet_id, required_file, *
['application/json']) # noqa: E501

# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['multipart/form-data'],
'POST', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = ['petstore_auth'] # noqa: E501
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ def select_header_content_type(self, content_types, method=None, body=None):
:return: Content-Type (e.g. application/json).
"""
if not content_types:
return 'application/json'
return None

content_types = [x.lower() for x in content_types]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,12 @@ def call_123_test_special_tags_with_http_info(self, body, **kwargs): # noqa: E5
['application/json']) # noqa: E501

# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json'],
'PATCH', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down
32 changes: 24 additions & 8 deletions samples/client/petstore/python-legacy/petstore_api/api/fake_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,12 @@ def create_xml_item_with_http_info(self, xml_item, **kwargs): # noqa: E501
if 'xml_item' in local_var_params:
body_params = local_var_params['xml_item']
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/xml', 'application/xml; charset=utf-8', 'application/xml; charset=utf-16', 'text/xml', 'text/xml; charset=utf-8', 'text/xml; charset=utf-16'],
'POST', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down Expand Up @@ -821,10 +823,12 @@ def test_body_with_file_schema_with_http_info(self, body, **kwargs): # noqa: E5
if 'body' in local_var_params:
body_params = local_var_params['body']
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json'],
'PUT', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down Expand Up @@ -968,10 +972,12 @@ def test_body_with_query_params_with_http_info(self, query, body, **kwargs): #
if 'body' in local_var_params:
body_params = local_var_params['body']
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json'],
'PUT', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down Expand Up @@ -1110,10 +1116,12 @@ def test_client_model_with_http_info(self, body, **kwargs): # noqa: E501
['application/json']) # noqa: E501

# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json'],
'PATCH', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down Expand Up @@ -1381,10 +1389,12 @@ def test_endpoint_parameters_with_http_info(self, number, double, pattern_withou

body_params = None
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/x-www-form-urlencoded'],
'POST', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = ['http_basic_test'] # noqa: E501
Expand Down Expand Up @@ -1567,10 +1577,12 @@ def test_enum_parameters_with_http_info(self, **kwargs): # noqa: E501

body_params = None
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/x-www-form-urlencoded'],
'GET', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down Expand Up @@ -1878,10 +1890,12 @@ def test_inline_additional_properties_with_http_info(self, param, **kwargs): #
if 'param' in local_var_params:
body_params = local_var_params['param']
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json'],
'POST', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down Expand Up @@ -2025,10 +2039,12 @@ def test_json_form_data_with_http_info(self, param, param2, **kwargs): # noqa:

body_params = None
# HTTP header `Content-Type`
header_params['Content-Type'] = local_var_params.get('_content_type',
content_types_list = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/x-www-form-urlencoded'],
'GET', body_params)) # noqa: E501
if content_types_list:
header_params['Content-Type'] = content_types_list

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down
Loading