diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index eb85a10a203..fc779a66653 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -71,12 +71,12 @@ class {{classname}}(object): body_params = {{#bodyParam}}params.get('{{paramName}}'){{/bodyParam}}{{^bodyParam}}None{{/bodyParam}} # HTTP header `Accept` - accepts = [{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept([{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}]) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, diff --git a/modules/swagger-codegen/src/main/resources/python/swagger.mustache b/modules/swagger-codegen/src/main/resources/python/swagger.mustache index 7b284376a58..569f0cd9e8d 100644 --- a/modules/swagger-codegen/src/main/resources/python/swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/python/swagger.mustache @@ -259,7 +259,7 @@ class ApiClient(object): Return `Accept` based on an array of accepts provided """ if not accepts: - return 'application/json' + return if 'application/json'.lower() in accepts: return 'application/json' else: diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py index db12623e75d..a599aeaabc1 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py @@ -66,12 +66,12 @@ class PetApi(object): body_params = params.get('body') # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = ['application/json', 'application/xml'] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type(['application/json', 'application/xml']) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -107,12 +107,12 @@ class PetApi(object): body_params = params.get('body') # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = ['application/json', 'application/xml'] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type(['application/json', 'application/xml']) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -148,12 +148,12 @@ class PetApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -191,12 +191,12 @@ class PetApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -238,12 +238,12 @@ class PetApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -287,12 +287,12 @@ class PetApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = ['application/x-www-form-urlencoded'] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type(['application/x-www-form-urlencoded']) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -333,12 +333,12 @@ class PetApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -380,12 +380,12 @@ class PetApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = ['multipart/form-data'] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type(['multipart/form-data']) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py index 19ece346938..7f9b852f7ca 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py @@ -65,12 +65,12 @@ class StoreApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -108,12 +108,12 @@ class StoreApi(object): body_params = params.get('body') # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -155,12 +155,12 @@ class StoreApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -202,12 +202,12 @@ class StoreApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py index a705567306a..2471970c436 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py @@ -66,12 +66,12 @@ class UserApi(object): body_params = params.get('body') # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -107,12 +107,12 @@ class UserApi(object): body_params = params.get('body') # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -148,12 +148,12 @@ class UserApi(object): body_params = params.get('body') # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -190,12 +190,12 @@ class UserApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -232,12 +232,12 @@ class UserApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -277,12 +277,12 @@ class UserApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -325,12 +325,12 @@ class UserApi(object): body_params = params.get('body') # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -370,12 +370,12 @@ class UserApi(object): body_params = None # HTTP header `Accept` - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ApiClient.select_header_accept(accepts) + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] # HTTP header `Content-Type` - content_types = [] - header_params['Content-Type'] = ApiClient.select_header_content_type(content_types) + header_params['Content-Type'] = ApiClient.select_header_content_type([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py index 7b284376a58..569f0cd9e8d 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py @@ -259,7 +259,7 @@ class ApiClient(object): Return `Accept` based on an array of accepts provided """ if not accepts: - return 'application/json' + return if 'application/json'.lower() in accepts: return 'application/json' else: diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py new file mode 100644 index 00000000000..437e94d09c6 --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py @@ -0,0 +1,58 @@ +# coding: utf-8 + +""" +Run the tests. +$ pip install nose (optional) +$ cd SwaggerPetstore-python +$ nosetests -v +""" + +import os +import time +import unittest + +import SwaggerPetstore + +HOST = 'http://petstore.swagger.io/v2' + + +class ApiClientTests(unittest.TestCase): + + def setUp(self): + self.api_client = SwaggerPetstore.ApiClient(HOST) + + def test_select_header_accept(self): + accepts = ['application/json', 'application/xml'] + accept = SwaggerPetstore.ApiClient.select_header_accept(accepts) + self.assertEqual(accept, 'application/json') + + accepts = ['application/xml', 'application/json'] + accept = SwaggerPetstore.ApiClient.select_header_accept(accepts) + self.assertEqual(accept, 'application/json') + + accepts = ['text/plain', 'application/xml'] + accept = SwaggerPetstore.ApiClient.select_header_accept(accepts) + self.assertEqual(accept, 'text/plain, application/xml') + + accepts = [] + accept = SwaggerPetstore.ApiClient.select_header_accept(accepts) + self.assertEqual(accept, None) + + def test_select_header_content_type(self): + content_types = ['application/json', 'application/xml'] + content_type = SwaggerPetstore.ApiClient.select_header_content_type(content_types) + self.assertEqual(content_type, 'application/json') + + content_types = ['application/xml', 'application/json'] + content_type = SwaggerPetstore.ApiClient.select_header_content_type(content_types) + self.assertEqual(content_type, 'application/json') + + content_types = ['text/plain', 'application/xml'] + content_type = SwaggerPetstore.ApiClient.select_header_content_type(content_types) + self.assertEqual(content_type, 'text/plain') + + content_types = [] + content_type = SwaggerPetstore.ApiClient.select_header_content_type(content_types) + self.assertEqual(content_type, 'application/json') + +