diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java index 9553f4ad133..a14fa71f425 100755 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java @@ -46,10 +46,11 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig typeMapping.put("long", "int"); typeMapping.put("double", "float"); typeMapping.put("array", "list"); - typeMapping.put("map", "map"); + typeMapping.put("map", "dict"); typeMapping.put("boolean", "bool"); typeMapping.put("string", "str"); typeMapping.put("date", "datetime"); + typeMapping.put("object", "object"); // from https://docs.python.org/release/2.5.4/ref/keywords.html reservedWords = new HashSet( @@ -111,7 +112,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig MapProperty mp = (MapProperty) p; Property inner = mp.getAdditionalProperties(); - return getSwaggerType(p) + "(String, " + getTypeDeclaration(inner) + ")"; + return getSwaggerType(p) + "(str, " + getTypeDeclaration(inner) + ")"; } return super.getTypeDeclaration(p); } diff --git a/modules/swagger-codegen/src/main/resources/python/api_client.mustache b/modules/swagger-codegen/src/main/resources/python/api_client.mustache index b8cc4cc2a84..e493074ba6d 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -168,13 +168,20 @@ class ApiClient(object): sub_class = match.group(1) return [self.deserialize(sub_obj, sub_class) for sub_obj in obj] - if obj_class in ['int', 'float', 'dict', 'list', 'str', 'bool', 'datetime']: + if 'dict(' in obj_class: + match = re.match('dict\((.*), (.*)\)', obj_class) + sub_class = match.group(2) + return {k: self.deserialize(v, sub_class) for k, v in iteritems(obj)} + + if obj_class in ['int', 'float', 'dict', 'list', 'str', 'bool', 'datetime', "object"]: obj_class = eval(obj_class) else: # not a native type, must be model class obj_class = eval('models.' + obj_class) if obj_class in [int, float, dict, list, str, bool]: return obj_class(obj) + elif obj_class == object: + return object() elif obj_class == datetime: return self.__parse_string_to_datetime(obj) diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/__init__.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/__init__.py index 30e93608274..3e7b51b8467 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/__init__.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/__init__.py @@ -9,8 +9,8 @@ from .models.order import Order # import apis into sdk package from .apis.user_api import UserApi -from .apis.store_api import StoreApi from .apis.pet_api import PetApi +from .apis.store_api import StoreApi # import ApiClient from .api_client import ApiClient diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py index b8cc4cc2a84..e493074ba6d 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py @@ -168,13 +168,20 @@ class ApiClient(object): sub_class = match.group(1) return [self.deserialize(sub_obj, sub_class) for sub_obj in obj] - if obj_class in ['int', 'float', 'dict', 'list', 'str', 'bool', 'datetime']: + if 'dict(' in obj_class: + match = re.match('dict\((.*), (.*)\)', obj_class) + sub_class = match.group(2) + return {k: self.deserialize(v, sub_class) for k, v in iteritems(obj)} + + if obj_class in ['int', 'float', 'dict', 'list', 'str', 'bool', 'datetime', "object"]: obj_class = eval(obj_class) else: # not a native type, must be model class obj_class = eval('models.' + obj_class) if obj_class in [int, float, dict, list, str, bool]: return obj_class(obj) + elif obj_class == object: + return object() elif obj_class == datetime: return self.__parse_string_to_datetime(obj) diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/__init__.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/__init__.py index 67ab226c734..128b25dad82 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/__init__.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/__init__.py @@ -2,6 +2,6 @@ from __future__ import absolute_import # import apis into api package from .user_api import UserApi -from .store_api import StoreApi from .pet_api import PetApi +from .store_api import StoreApi 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 19f11fea66b..5f5c2fe81fa 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 @@ -298,7 +298,7 @@ class PetApi(object): header_params['Content-Type'] = self.api_client.select_header_content_type([]) # Authentication setting - auth_settings = ['petstore_auth', 'api_key'] + auth_settings = ['api_key', 'petstore_auth'] 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 1523e3b4be4..1e024cfbb5b 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 @@ -47,7 +47,7 @@ class StoreApi(object): Returns a map of status codes to quantities - :return: map(String, int) + :return: dict(str, int) """ all_params = [] @@ -86,7 +86,7 @@ class StoreApi(object): response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response='map(String, int)', auth_settings=auth_settings) + response='dict(str, int)', auth_settings=auth_settings) return response 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 index 0ef4314d0f4..42d838417fd 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py @@ -88,3 +88,46 @@ class ApiClientTests(unittest.TestCase): content_types = [] content_type = self.api_client.select_header_content_type(content_types) self.assertEqual(content_type, 'application/json') + + def test_deserialize_to_dict(self): + # dict(str, Pet) + json = { + 'pet': { + "id": 0, + "category": { + "id": 0, + "name": "string" + }, + "name": "doggie", + "photoUrls": [ + "string" + ], + "tags": [ + { + "id": 0, + "name": "string" + } + ], + "status": "available" + } + } + + data = self.api_client.deserialize(json, 'dict(str, Pet)') + self.assertTrue(isinstance(data, dict)) + self.assertTrue(isinstance(data['pet'], SwaggerPetstore.Pet)) + + # dict(str, int) + json = { + 'integer': 1 + } + + data = self.api_client.deserialize(json, 'dict(str, int)') + self.assertTrue(isinstance(data, dict)) + self.assertTrue(isinstance(data['integer'], int)) + + def test_deserialize_to_object(self): + data = self.api_client.deserialize("", "object") + self.assertTrue(type(data) == object) + + + diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_store_api.py b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_store_api.py new file mode 100644 index 00000000000..740e2bdc8ad --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_store_api.py @@ -0,0 +1,31 @@ +# coding: utf-8 + +""" +Run the tests. +$ pip install nose (optional) +$ cd SwaggerPetstore-python +$ nosetests -v +""" + +import os +import time +import unittest + +import SwaggerPetstore +from SwaggerPetstore.rest import ApiException + + +class StoreApiTests(unittest.TestCase): + + def setUp(self): + self.store_api = SwaggerPetstore.StoreApi() + + def tearDown(self): + # sleep 1 sec between two every 2 tests + time.sleep(1) + + def test_get_inventory(self): + data = self.store_api.get_inventory() + self.assertIsNotNone(data) + self.assertTrue(isinstance(data, dict)) + self.assertItemsEqual(data.keys(), ['available', 'string', 'sold', 'pending', 'confused', 'active', 'na'])