Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5bc9540
Adds additionalproperties feature in python
spacether Feb 4, 2019
64d5444
Adds ensure-up-to-date updates
spacether Feb 4, 2019
ec73f56
Adds docstring description of the model argument inputs
spacether Feb 4, 2019
5cdfe51
Adds ensure up to date oepnapi3 python updates
spacether Feb 4, 2019
0a52027
Adds test fix
spacether Feb 4, 2019
8be21b0
Adds fix for Shippable, gives the additionalProperties CodegenParamet…
spacether Feb 4, 2019
ba7ea49
Adds function to set freeform types to string of all types
spacether Feb 7, 2019
88f8197
Adds postProcessAllModels function which processes and fixes dataType…
spacether Feb 7, 2019
0590d05
Adds models with additionalproperties of each type and model tests
spacether Feb 9, 2019
372ae60
Adds _check_type parameter to model, adds additionalproperties tests
spacether Feb 11, 2019
5b4f108
Creates utils module, adds additionalproperties map for serialization
spacether Feb 12, 2019
2e33cf1
Removes additional_properties_map, creates model_to_dict function to …
spacether Feb 13, 2019
983f570
Improves docstring for model_to_dict
spacether Feb 14, 2019
f218ed5
Adds class type definition in models
spacether Feb 17, 2019
7656756
Adds datetime and date type classes, adds general OpenApiException, r…
spacether Feb 18, 2019
7ac9285
Adds class imports to models, python generator uses dataType for docs…
spacether Feb 20, 2019
67dbb9c
Model discriminator now stores classes, adds __deserialize_model in a…
spacether Feb 22, 2019
9680dfd
Creates exceptions module, all deserialization tests in py2 except 1 …
spacether Feb 23, 2019
05fe1aa
Adds validate_and_convert_types, python 2.0 tests pass except for tes…
spacether Feb 25, 2019
e8f97fa
Adds anytype deserialization tests in python swagger client
spacether Feb 28, 2019
59b2d4c
Fixes typos
spacether Feb 28, 2019
e158601
Adds file deserialization test
spacether Mar 2, 2019
7d84ef2
Updates all v2 and v3 python samples
spacether Mar 3, 2019
b2446cf
Removes dubug flag, reverts readme, in python generator tweaks suppor…
spacether Mar 3, 2019
9e9b94e
Adds dict instantiationType back in
spacether Mar 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Removes additional_properties_map, creates model_to_dict function to …
…use in model.to_dict and serialization
  • Loading branch information
spacether committed Mar 2, 2019
commit 2e33cf11f0d7ce9bd4da241445cf4c7383854b81
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import tornado.gen
{{/tornado}}

from {{packageName}}.configuration import Configuration
from {{packageName}}.utils import model_to_dict
import {{modelPackage}}
from {{packageName}} import rest

Expand Down Expand Up @@ -222,14 +223,10 @@ class ApiClient(object):
if isinstance(obj, dict):
obj_dict = obj
else:
# Convert model obj to dict except
# attributes `openapi_types`, `attribute_map`
# and attributes which value is not None.
# Convert model obj to dict
# Convert attribute name to json key in
# model definition for request.
obj_dict = {obj.attribute_map[attr]: getattr(obj, attr)
for attr, _ in six.iteritems(obj.openapi_types)
if getattr(obj, attr) is not None}
obj_dict = model_to_dict(obj, serialize=True)

return {key: self.sanitize_for_serialization(val)
for key, val in six.iteritems(obj_dict)}
Expand Down
36 changes: 7 additions & 29 deletions modules/openapi-generator/src/main/resources/python/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import re # noqa: F401

import six

from {{packageName}}.utils import recursive_type, valid_type
from {{packageName}}.utils import model_to_dict, recursive_type, valid_type


{{#models}}
Expand All @@ -34,12 +34,10 @@ class {{classname}}(object):
Optional and required variables only.
attribute_map (dict): The key is attribute name
and the value is json key in definition.
Optional and required variables only.
Optional, required variables, and
additional properties.
{{#additionalProperties}}
additional_properties_type (str): The attribute type.
additional_properties variables only.
additional_properties_map (dict): The key is attribute name
and the value is json key in definition.
additional_properties_type (str): The attribute type for
additional_properties variables only.
{{/additionalProperties}}
"""
Expand All @@ -55,7 +53,6 @@ class {{classname}}(object):
}
{{#additionalProperties}}
additional_properties_type = '{{{dataType}}}'
additional_properties_map = {}
{{/additionalProperties}}
{{#discriminator}}
discriminator_value_class_map = {
Expand Down Expand Up @@ -119,8 +116,8 @@ class {{classname}}(object):
{{#additionalProperties}}
# set a variable name value for json serialization
if (name not in self.openapi_types and
name not in self.additional_properties_map):
self.additional_properties_map[name] = name
name not in self.attribute_map):
self.attribute_map[name] = name
{{/additionalProperties}}

def __getitem__(self, name):
Expand Down Expand Up @@ -237,26 +234,7 @@ class {{classname}}(object):
{{/discriminator}}
def to_dict(self):
"""Returns the model properties as a dict"""
result = {}

for attr, value in six.iteritems(self._data_store):
if isinstance(value, list):
result[attr] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
value
))
elif hasattr(value, "to_dict"):
result[attr] = value.to_dict()
elif isinstance(value, dict):
result[attr] = dict(map(
lambda item: (item[0], item[1].to_dict())
if hasattr(item[1], "to_dict") else item,
value.items()
))
else:
result[attr] = value

return result
return model_to_dict(self, serialize=False)

def to_str(self):
"""Returns the string representation of the model"""
Expand Down
31 changes: 31 additions & 0 deletions modules/openapi-generator/src/main/resources/python/utils.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,34 @@ def get_types_remainder(type_string):
return set([type_prefix[:4]]), type_string[len(type_prefix):-1]
type_set = set(type_string.split('|'))
return type_set, ''


def model_to_dict(model_instance, serialize=True):
"""Returns the model properties as a dict

Kwargs:
serialize (bool): if True, the keys in the dict will be values from
attribute_map
"""
result = {}

for attr, value in six.iteritems(model_instance._data_store):
if serialize:
attr = model_instance.attribute_map[attr]
if isinstance(value, list):
result[attr] = list(map(
lambda x: model_to_dict(x, serialize=serialize)
if hasattr(x, '_data_store') else x, value
))
elif hasattr(value, '_data_store'):
result[attr] = model_to_dict(value, serialize=serialize)
elif isinstance(value, dict):
result[attr] = dict(map(
lambda item: (item[0], model_to_dict(item[1], serialize=serialize))
if hasattr(item[1], '_data_store') else item,
value.items()
))
else:
result[attr] = value

return result
9 changes: 3 additions & 6 deletions samples/client/petstore/python/petstore_api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from six.moves.urllib.parse import quote

from petstore_api.configuration import Configuration
from petstore_api.utils import model_to_dict
import petstore_api.models
from petstore_api import rest

Expand Down Expand Up @@ -215,14 +216,10 @@ def sanitize_for_serialization(self, obj):
if isinstance(obj, dict):
obj_dict = obj
else:
# Convert model obj to dict except
# attributes `openapi_types`, `attribute_map`
# and attributes which value is not None.
# Convert model obj to dict
# Convert attribute name to json key in
# model definition for request.
obj_dict = {obj.attribute_map[attr]: getattr(obj, attr)
for attr, _ in six.iteritems(obj.openapi_types)
if getattr(obj, attr) is not None}
obj_dict = model_to_dict(obj, serialize=True)

return {key: self.sanitize_for_serialization(val)
for key, val in six.iteritems(obj_dict)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import six

from petstore_api.utils import recursive_type, valid_type
from petstore_api.utils import model_to_dict, recursive_type, valid_type


class AdditionalPropertiesAnyType(object):
Expand All @@ -32,11 +32,9 @@ class AdditionalPropertiesAnyType(object):
Optional and required variables only.
attribute_map (dict): The key is attribute name
and the value is json key in definition.
Optional and required variables only.
additional_properties_type (str): The attribute type.
additional_properties variables only.
additional_properties_map (dict): The key is attribute name
and the value is json key in definition.
Optional, required variables, and
additional properties.
additional_properties_type (str): The attribute type for
additional_properties variables only.
"""
openapi_types = {
Expand All @@ -46,7 +44,6 @@ class AdditionalPropertiesAnyType(object):
'name': 'name'
}
additional_properties_type = 'str|float|int|bool|list|dict'
additional_properties_map = {}

def __init__(self, _check_type=False, **kwargs): # noqa: E501
"""AdditionalPropertiesAnyType - a model defined in OpenAPI
Expand Down Expand Up @@ -90,8 +87,8 @@ def __setitem__(self, name, value):
self._data_store[name] = value
# set a variable name value for json serialization
if (name not in self.openapi_types and
name not in self.additional_properties_map):
self.additional_properties_map[name] = name
name not in self.attribute_map):
self.attribute_map[name] = name

def __getitem__(self, name):
if name in self.openapi_types:
Expand Down Expand Up @@ -124,26 +121,7 @@ def name(self, name):

def to_dict(self):
"""Returns the model properties as a dict"""
result = {}

for attr, value in six.iteritems(self._data_store):
if isinstance(value, list):
result[attr] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
value
))
elif hasattr(value, "to_dict"):
result[attr] = value.to_dict()
elif isinstance(value, dict):
result[attr] = dict(map(
lambda item: (item[0], item[1].to_dict())
if hasattr(item[1], "to_dict") else item,
value.items()
))
else:
result[attr] = value

return result
return model_to_dict(self, serialize=False)

def to_str(self):
"""Returns the string representation of the model"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import six

from petstore_api.utils import recursive_type, valid_type
from petstore_api.utils import model_to_dict, recursive_type, valid_type


class AdditionalPropertiesArray(object):
Expand All @@ -32,11 +32,9 @@ class AdditionalPropertiesArray(object):
Optional and required variables only.
attribute_map (dict): The key is attribute name
and the value is json key in definition.
Optional and required variables only.
additional_properties_type (str): The attribute type.
additional_properties variables only.
additional_properties_map (dict): The key is attribute name
and the value is json key in definition.
Optional, required variables, and
additional properties.
additional_properties_type (str): The attribute type for
additional_properties variables only.
"""
openapi_types = {
Expand All @@ -46,7 +44,6 @@ class AdditionalPropertiesArray(object):
'name': 'name'
}
additional_properties_type = 'list[str|float|int|bool|list|dict]'
additional_properties_map = {}

def __init__(self, _check_type=False, **kwargs): # noqa: E501
"""AdditionalPropertiesArray - a model defined in OpenAPI
Expand Down Expand Up @@ -90,8 +87,8 @@ def __setitem__(self, name, value):
self._data_store[name] = value
# set a variable name value for json serialization
if (name not in self.openapi_types and
name not in self.additional_properties_map):
self.additional_properties_map[name] = name
name not in self.attribute_map):
self.attribute_map[name] = name

def __getitem__(self, name):
if name in self.openapi_types:
Expand Down Expand Up @@ -124,26 +121,7 @@ def name(self, name):

def to_dict(self):
"""Returns the model properties as a dict"""
result = {}

for attr, value in six.iteritems(self._data_store):
if isinstance(value, list):
result[attr] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
value
))
elif hasattr(value, "to_dict"):
result[attr] = value.to_dict()
elif isinstance(value, dict):
result[attr] = dict(map(
lambda item: (item[0], item[1].to_dict())
if hasattr(item[1], "to_dict") else item,
value.items()
))
else:
result[attr] = value

return result
return model_to_dict(self, serialize=False)

def to_str(self):
"""Returns the string representation of the model"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import six

from petstore_api.utils import recursive_type, valid_type
from petstore_api.utils import model_to_dict, recursive_type, valid_type


class AdditionalPropertiesBoolean(object):
Expand All @@ -32,11 +32,9 @@ class AdditionalPropertiesBoolean(object):
Optional and required variables only.
attribute_map (dict): The key is attribute name
and the value is json key in definition.
Optional and required variables only.
additional_properties_type (str): The attribute type.
additional_properties variables only.
additional_properties_map (dict): The key is attribute name
and the value is json key in definition.
Optional, required variables, and
additional properties.
additional_properties_type (str): The attribute type for
additional_properties variables only.
"""
openapi_types = {
Expand All @@ -46,7 +44,6 @@ class AdditionalPropertiesBoolean(object):
'name': 'name'
}
additional_properties_type = 'bool'
additional_properties_map = {}

def __init__(self, _check_type=False, **kwargs): # noqa: E501
"""AdditionalPropertiesBoolean - a model defined in OpenAPI
Expand Down Expand Up @@ -90,8 +87,8 @@ def __setitem__(self, name, value):
self._data_store[name] = value
# set a variable name value for json serialization
if (name not in self.openapi_types and
name not in self.additional_properties_map):
self.additional_properties_map[name] = name
name not in self.attribute_map):
self.attribute_map[name] = name

def __getitem__(self, name):
if name in self.openapi_types:
Expand Down Expand Up @@ -124,26 +121,7 @@ def name(self, name):

def to_dict(self):
"""Returns the model properties as a dict"""
result = {}

for attr, value in six.iteritems(self._data_store):
if isinstance(value, list):
result[attr] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
value
))
elif hasattr(value, "to_dict"):
result[attr] = value.to_dict()
elif isinstance(value, dict):
result[attr] = dict(map(
lambda item: (item[0], item[1].to_dict())
if hasattr(item[1], "to_dict") else item,
value.items()
))
else:
result[attr] = value

return result
return model_to_dict(self, serialize=False)

def to_str(self):
"""Returns the string representation of the model"""
Expand Down
Loading