support map type response for python client

This commit is contained in:
geekerzp 2015-06-10 11:55:12 +08:00
parent 672fcd5a14
commit e2d441e862
9 changed files with 84 additions and 7 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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))

View File

@ -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'])