forked from loafle/openapi-generator-original
Merge pull request #855 from geekerzp/develop_2.0_python_deserialize_map
[Python] bug fix for map response
This commit is contained in:
commit
e65a079f0e
@ -46,10 +46,11 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
typeMapping.put("long", "int");
|
typeMapping.put("long", "int");
|
||||||
typeMapping.put("double", "float");
|
typeMapping.put("double", "float");
|
||||||
typeMapping.put("array", "list");
|
typeMapping.put("array", "list");
|
||||||
typeMapping.put("map", "map");
|
typeMapping.put("map", "dict");
|
||||||
typeMapping.put("boolean", "bool");
|
typeMapping.put("boolean", "bool");
|
||||||
typeMapping.put("string", "str");
|
typeMapping.put("string", "str");
|
||||||
typeMapping.put("date", "datetime");
|
typeMapping.put("date", "datetime");
|
||||||
|
typeMapping.put("object", "object");
|
||||||
|
|
||||||
// from https://docs.python.org/release/2.5.4/ref/keywords.html
|
// from https://docs.python.org/release/2.5.4/ref/keywords.html
|
||||||
reservedWords = new HashSet<String>(
|
reservedWords = new HashSet<String>(
|
||||||
@ -111,7 +112,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
MapProperty mp = (MapProperty) p;
|
MapProperty mp = (MapProperty) p;
|
||||||
Property inner = mp.getAdditionalProperties();
|
Property inner = mp.getAdditionalProperties();
|
||||||
|
|
||||||
return getSwaggerType(p) + "(String, " + getTypeDeclaration(inner) + ")";
|
return getSwaggerType(p) + "(str, " + getTypeDeclaration(inner) + ")";
|
||||||
}
|
}
|
||||||
return super.getTypeDeclaration(p);
|
return super.getTypeDeclaration(p);
|
||||||
}
|
}
|
||||||
|
@ -168,13 +168,20 @@ class ApiClient(object):
|
|||||||
sub_class = match.group(1)
|
sub_class = match.group(1)
|
||||||
return [self.deserialize(sub_obj, sub_class) for sub_obj in obj]
|
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)
|
obj_class = eval(obj_class)
|
||||||
else: # not a native type, must be model class
|
else: # not a native type, must be model class
|
||||||
obj_class = eval('models.' + obj_class)
|
obj_class = eval('models.' + obj_class)
|
||||||
|
|
||||||
if obj_class in [int, float, dict, list, str, bool]:
|
if obj_class in [int, float, dict, list, str, bool]:
|
||||||
return obj_class(obj)
|
return obj_class(obj)
|
||||||
|
elif obj_class == object:
|
||||||
|
return object()
|
||||||
elif obj_class == datetime:
|
elif obj_class == datetime:
|
||||||
return self.__parse_string_to_datetime(obj)
|
return self.__parse_string_to_datetime(obj)
|
||||||
|
|
||||||
|
@ -9,8 +9,8 @@ from .models.order import Order
|
|||||||
|
|
||||||
# import apis into sdk package
|
# import apis into sdk package
|
||||||
from .apis.user_api import UserApi
|
from .apis.user_api import UserApi
|
||||||
from .apis.store_api import StoreApi
|
|
||||||
from .apis.pet_api import PetApi
|
from .apis.pet_api import PetApi
|
||||||
|
from .apis.store_api import StoreApi
|
||||||
|
|
||||||
# import ApiClient
|
# import ApiClient
|
||||||
from .api_client import ApiClient
|
from .api_client import ApiClient
|
||||||
|
@ -168,13 +168,20 @@ class ApiClient(object):
|
|||||||
sub_class = match.group(1)
|
sub_class = match.group(1)
|
||||||
return [self.deserialize(sub_obj, sub_class) for sub_obj in obj]
|
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)
|
obj_class = eval(obj_class)
|
||||||
else: # not a native type, must be model class
|
else: # not a native type, must be model class
|
||||||
obj_class = eval('models.' + obj_class)
|
obj_class = eval('models.' + obj_class)
|
||||||
|
|
||||||
if obj_class in [int, float, dict, list, str, bool]:
|
if obj_class in [int, float, dict, list, str, bool]:
|
||||||
return obj_class(obj)
|
return obj_class(obj)
|
||||||
|
elif obj_class == object:
|
||||||
|
return object()
|
||||||
elif obj_class == datetime:
|
elif obj_class == datetime:
|
||||||
return self.__parse_string_to_datetime(obj)
|
return self.__parse_string_to_datetime(obj)
|
||||||
|
|
||||||
|
@ -2,6 +2,6 @@ from __future__ import absolute_import
|
|||||||
|
|
||||||
# import apis into api package
|
# import apis into api package
|
||||||
from .user_api import UserApi
|
from .user_api import UserApi
|
||||||
from .store_api import StoreApi
|
|
||||||
from .pet_api import PetApi
|
from .pet_api import PetApi
|
||||||
|
from .store_api import StoreApi
|
||||||
|
|
||||||
|
@ -298,7 +298,7 @@ class PetApi(object):
|
|||||||
header_params['Content-Type'] = self.api_client.select_header_content_type([])
|
header_params['Content-Type'] = self.api_client.select_header_content_type([])
|
||||||
|
|
||||||
# Authentication setting
|
# 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,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
|
@ -47,7 +47,7 @@ class StoreApi(object):
|
|||||||
Returns a map of status codes to quantities
|
Returns a map of status codes to quantities
|
||||||
|
|
||||||
|
|
||||||
:return: map(String, int)
|
:return: dict(str, int)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
all_params = []
|
all_params = []
|
||||||
@ -86,7 +86,7 @@ class StoreApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
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
|
return response
|
||||||
|
|
||||||
|
@ -88,3 +88,46 @@ class ApiClientTests(unittest.TestCase):
|
|||||||
content_types = []
|
content_types = []
|
||||||
content_type = self.api_client.select_header_content_type(content_types)
|
content_type = self.api_client.select_header_content_type(content_types)
|
||||||
self.assertEqual(content_type, 'application/json')
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -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'])
|
Loading…
x
Reference in New Issue
Block a user