From e2d441e862732b2333263ea59126c18be0d32dab Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 10 Jun 2015 11:55:12 +0800 Subject: [PATCH] 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 9553f4ad13..632b4289bb 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 b8cc4cc2a8..76ede44599 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 30e9360827..3e7b51b846 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 b8cc4cc2a8..76ede44599 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 67ab226c73..128b25dad8 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 19f11fea66..5f5c2fe81f 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 1523e3b4be..1e024cfbb5 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 0ef4314d0f..720cc2f532 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 0000000000..740e2bdc8a --- /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'])