From e2d441e862732b2333263ea59126c18be0d32dab Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 10 Jun 2015 11:55:12 +0800 Subject: [PATCH 1/3] support map type response for python client --- .../languages/PythonClientCodegen.java | 4 +-- .../main/resources/python/api_client.mustache | 5 +++ .../SwaggerPetstore/__init__.py | 2 +- .../SwaggerPetstore/api_client.py | 5 +++ .../SwaggerPetstore/apis/__init__.py | 2 +- .../SwaggerPetstore/apis/pet_api.py | 2 +- .../SwaggerPetstore/apis/store_api.py | 4 +-- .../tests/test_api_client.py | 36 +++++++++++++++++++ .../tests/test_store_api.py | 31 ++++++++++++++++ 9 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 samples/client/petstore/python/SwaggerPetstore-python/tests/test_store_api.py 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..632b4289bb7 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,7 +46,7 @@ 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"); @@ -111,7 +111,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..76ede445992 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -168,6 +168,11 @@ class ApiClient(object): sub_class = match.group(1) return [self.deserialize(sub_obj, sub_class) for sub_obj in obj] + 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']: obj_class = eval(obj_class) else: # not a native type, must be model class 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..76ede445992 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py @@ -168,6 +168,11 @@ class ApiClient(object): sub_class = match.group(1) return [self.deserialize(sub_obj, sub_class) for sub_obj in obj] + 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']: obj_class = eval(obj_class) else: # not a native type, must be model class 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..720cc2f5329 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,39 @@ 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)) 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']) From ff9623fb5c8c7b83f48f4802c7b78d4fbb3c1ada Mon Sep 17 00:00:00 2001 From: geekerzp Date: Sat, 13 Jun 2015 18:58:27 +0800 Subject: [PATCH 2/3] Support pure object response for python client. --- .../io/swagger/codegen/languages/PythonClientCodegen.java | 1 + .../src/main/resources/python/api_client.mustache | 4 +++- .../SwaggerPetstore-python/SwaggerPetstore/api_client.py | 4 +++- .../python/SwaggerPetstore-python/tests/test_api_client.py | 7 +++++++ 4 files changed, 14 insertions(+), 2 deletions(-) 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 632b4289bb7..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 @@ -50,6 +50,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig 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( 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 76ede445992..e493074ba6d 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -173,13 +173,15 @@ class ApiClient(object): 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']: + 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/api_client.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py index 76ede445992..e493074ba6d 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py @@ -173,13 +173,15 @@ class ApiClient(object): 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']: + 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/tests/test_api_client.py b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py index 720cc2f5329..b318d5ab9ce 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 @@ -124,3 +124,10 @@ class ApiClientTests(unittest.TestCase): 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(isinstance(data, object)) + + + From 718a9a79ed2573997e2f4a688194bb2291648f41 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Sat, 13 Jun 2015 19:05:15 +0800 Subject: [PATCH 3/3] Update test case `test_deserialize_to_object` of python client. --- .../python/SwaggerPetstore-python/tests/test_api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b318d5ab9ce..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 @@ -127,7 +127,7 @@ class ApiClientTests(unittest.TestCase): def test_deserialize_to_object(self): data = self.api_client.deserialize("", "object") - self.assertTrue(isinstance(data, object)) + self.assertTrue(type(data) == object)