diff --git a/modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/models/base_model_.mustache b/modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/models/base_model_.mustache index 54517a06d53a32e8ab53cc2e652b4eca0188adb3..0104723a478e0318bd354f9409925e6d3e9633d3 100644 --- a/modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/models/base_model_.mustache +++ b/modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/models/base_model_.mustache @@ -22,29 +22,33 @@ class Model(object): attribute_map = {} @classmethod - def from_dict(cls{{^supportPython2}}: typing.Type[T]{{/supportPython2}}, dikt){{^supportPython2}} -> T{{/supportPython2}}: + def from_dict(cls{{^supportPython2}}: typing.Type[T]{{/supportPython2}}, dikt, attr_map=True){{^supportPython2}} -> T{{/supportPython2}}: """Returns the dict as a model""" - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) - def to_dict(self): + def to_dict(self, attr_map=True): """Returns the model properties as a dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :rtype: dict """ result = {} for attr, _ in six.iteritems(self.swagger_types): value = getattr(self, attr) + + attr = self.attribute_map[attr] if attr_map else attr if isinstance(value, list): result[attr] = list(map( - lambda x: x.to_dict() if hasattr(x, "to_dict") else x, + lambda x: x.to_dict(attr_map=attr_map) if hasattr(x, "to_dict") else x, value )) elif hasattr(value, "to_dict"): - result[attr] = value.to_dict() + result[attr] = value.to_dict(attr_map=attr_map) elif isinstance(value, dict): result[attr] = dict(map( - lambda item: (item[0], item[1].to_dict()) + lambda item: (item[0], item[1].to_dict(attr_map=attr_map)) if hasattr(item[1], "to_dict") else item, value.items() )) diff --git a/modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/models/model.mustache b/modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/models/model.mustache index 78157b3187163976bdfd6b2cf992a6f02f495978..bdf2e219ee0bc5c2d6864673756b03de34280216 100644 --- a/modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/models/model.mustache +++ b/modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/models/model.mustache @@ -52,15 +52,17 @@ class {{classname}}(Model): {{/vars}} @classmethod - def from_dict(cls, dikt){{^supportPython2}} -> '{{classname}}'{{/supportPython2}}: + def from_dict(cls, dikt, attr_map=True){{^supportPython2}} -> '{{classname}}'{{/supportPython2}}: """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The {{name}} of this {{classname}}. # noqa: E501 :rtype: {{classname}} """ - return util.deserialize_model(dikt, cls){{#vars}}{{#-first}} + return util.deserialize_model(dikt, cls, attr_map=attr_map){{#vars}}{{#-first}} {{/-first}} @property diff --git a/modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/util.mustache b/modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/util.mustache index 9763c8fc85003506285df7ebab9412f9c089c481..818289d73a1d189544223abb8ecb645094c5b2dc 100644 --- a/modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/util.mustache +++ b/modules/openapi-generator/src/main/resources/python-blueplanet/app/{{packageName}}/util.mustache @@ -91,24 +91,33 @@ def deserialize_datetime(string): return string -def deserialize_model(data, klass): +def deserialize_model(data, klass, attr_map=True): """Deserializes list or dict to model. :param data: dict, list. :type data: dict | list :param klass: class literal. + :param attr_map: defines if attribute_map is used in dict. + :type attr_map: bool :return: model object. """ instance = klass() + # should be included in return types if not instance.swagger_types: return data - for attr, attr_type in six.iteritems(instance.swagger_types): - if data is not None \ - and instance.attribute_map[attr] in data \ - and isinstance(data, (list, dict)): - value = data[instance.attribute_map[attr]] + if data is None: + return instance + + if not isinstance(data, (list, dict)): + return instance + + for attr, attr_type in six.iteritems(instance.openapi_types): + dict_attr = instance.attribute_map[attr] if attr_map else attr + + if dict_attr in data: + value = data[dict_attr] setattr(instance, attr, _deserialize(value, attr_type)) return instance diff --git a/modules/openapi-generator/src/main/resources/python-flask/base_model_.mustache b/modules/openapi-generator/src/main/resources/python-flask/base_model_.mustache index e41e51e2a257b7ced773c35cd1c86b1a1209d0e4..f7a10c472f8e8eb5d130e9d526f056fc9785c1ec 100644 --- a/modules/openapi-generator/src/main/resources/python-flask/base_model_.mustache +++ b/modules/openapi-generator/src/main/resources/python-flask/base_model_.mustache @@ -22,29 +22,33 @@ class Model(object): attribute_map = {} @classmethod - def from_dict(cls{{^supportPython2}}: typing.Type[T]{{/supportPython2}}, dikt){{^supportPython2}} -> T{{/supportPython2}}: + def from_dict(cls{{^supportPython2}}: typing.Type[T]{{/supportPython2}}, dikt, attr_map=True){{^supportPython2}} -> T{{/supportPython2}}: """Returns the dict as a model""" - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) - def to_dict(self): + def to_dict(self, attr_map=True): """Returns the model properties as a dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :rtype: dict """ result = {} for attr, _ in six.iteritems(self.openapi_types): value = getattr(self, attr) + + attr = self.attribute_map[attr] if attr_map else attr if isinstance(value, list): result[attr] = list(map( - lambda x: x.to_dict() if hasattr(x, "to_dict") else x, + lambda x: x.to_dict(attr_map=attr_map) if hasattr(x, "to_dict") else x, value )) elif hasattr(value, "to_dict"): - result[attr] = value.to_dict() + result[attr] = value.to_dict(attr_map=attr_map) elif isinstance(value, dict): result[attr] = dict(map( - lambda item: (item[0], item[1].to_dict()) + lambda item: (item[0], item[1].to_dict(attr_map=attr_map)) if hasattr(item[1], "to_dict") else item, value.items() )) diff --git a/modules/openapi-generator/src/main/resources/python-flask/model.mustache b/modules/openapi-generator/src/main/resources/python-flask/model.mustache index 0c4309a867aaab7ae3ef46ad5fee5bbae9f90e4f..cdf5fd7591f06046cdf8638cc7a1913394c5216f 100644 --- a/modules/openapi-generator/src/main/resources/python-flask/model.mustache +++ b/modules/openapi-generator/src/main/resources/python-flask/model.mustache @@ -62,15 +62,17 @@ class {{classname}}(Model): {{/vars}} @classmethod - def from_dict(cls, dikt){{^supportPython2}} -> '{{classname}}'{{/supportPython2}}: + def from_dict(cls, dikt, attr_map=True){{^supportPython2}} -> '{{classname}}'{{/supportPython2}}: """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The {{name}} of this {{classname}}. # noqa: E501 :rtype: {{classname}} """ - return util.deserialize_model(dikt, cls){{#vars}}{{#-first}} + return util.deserialize_model(dikt, cls, attr_map=attr_map){{#vars}}{{#-first}} {{/-first}} @property diff --git a/modules/openapi-generator/src/main/resources/python-flask/util.mustache b/modules/openapi-generator/src/main/resources/python-flask/util.mustache index f2b90009a46692e1e83da26b0aef5036fe5c4057..3778a5dbf34027036a4bd2cb095f1706f11a52a5 100644 --- a/modules/openapi-generator/src/main/resources/python-flask/util.mustache +++ b/modules/openapi-generator/src/main/resources/python-flask/util.mustache @@ -91,24 +91,33 @@ def deserialize_datetime(string): return string -def deserialize_model(data, klass): +def deserialize_model(data, klass, attr_map=True): """Deserializes list or dict to model. :param data: dict, list. :type data: dict | list :param klass: class literal. + :param attr_map: defines if attribute_map is used in dict. + :type attr_map: bool :return: model object. """ instance = klass() + # should be included in return types if not instance.openapi_types: return data + if data is None: + return instance + + if not isinstance(data, (list, dict)): + return instance + for attr, attr_type in six.iteritems(instance.openapi_types): - if data is not None \ - and instance.attribute_map[attr] in data \ - and isinstance(data, (list, dict)): - value = data[instance.attribute_map[attr]] + dict_attr = instance.attribute_map[attr] if attr_map else attr + + if dict_attr in data: + value = data[dict_attr] setattr(instance, attr, _deserialize(value, attr_type)) return instance diff --git a/samples/server/petstore/python-blueplanet/app/openapi_server/models/api_response.py b/samples/server/petstore/python-blueplanet/app/openapi_server/models/api_response.py index 9c876ad632c470b8e642765573998d9166a48b9d..10e27a8faa0ebb8d8ff013729201ef7e7cb11098 100644 --- a/samples/server/petstore/python-blueplanet/app/openapi_server/models/api_response.py +++ b/samples/server/petstore/python-blueplanet/app/openapi_server/models/api_response.py @@ -42,15 +42,17 @@ class ApiResponse(Model): self._message = message @classmethod - def from_dict(cls, dikt) -> 'ApiResponse': + def from_dict(cls, dikt, attr_map=True) -> 'ApiResponse': """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The ApiResponse of this ApiResponse. # noqa: E501 :rtype: ApiResponse """ - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) @property def code(self) -> int: diff --git a/samples/server/petstore/python-blueplanet/app/openapi_server/models/base_model_.py b/samples/server/petstore/python-blueplanet/app/openapi_server/models/base_model_.py index b835b909b700f27572e39c9cd95a5f5cb721d4c6..e82bddf208979e00bc298888b52f37c60d4d08ad 100644 --- a/samples/server/petstore/python-blueplanet/app/openapi_server/models/base_model_.py +++ b/samples/server/petstore/python-blueplanet/app/openapi_server/models/base_model_.py @@ -18,29 +18,33 @@ class Model(object): attribute_map = {} @classmethod - def from_dict(cls: typing.Type[T], dikt) -> T: + def from_dict(cls: typing.Type[T], dikt, attr_map=True) -> T: """Returns the dict as a model""" - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) - def to_dict(self): + def to_dict(self, attr_map=True): """Returns the model properties as a dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :rtype: dict """ result = {} for attr, _ in six.iteritems(self.swagger_types): value = getattr(self, attr) + + attr = self.attribute_map[attr] if attr_map else attr if isinstance(value, list): result[attr] = list(map( - lambda x: x.to_dict() if hasattr(x, "to_dict") else x, + lambda x: x.to_dict(attr_map=attr_map) if hasattr(x, "to_dict") else x, value )) elif hasattr(value, "to_dict"): - result[attr] = value.to_dict() + result[attr] = value.to_dict(attr_map=attr_map) elif isinstance(value, dict): result[attr] = dict(map( - lambda item: (item[0], item[1].to_dict()) + lambda item: (item[0], item[1].to_dict(attr_map=attr_map)) if hasattr(item[1], "to_dict") else item, value.items() )) diff --git a/samples/server/petstore/python-blueplanet/app/openapi_server/models/category.py b/samples/server/petstore/python-blueplanet/app/openapi_server/models/category.py index 2b407d025090b8db132603adee5ae049302299e8..171741f1a9cb28a10e4be7303c6a35bde9070b0f 100644 --- a/samples/server/petstore/python-blueplanet/app/openapi_server/models/category.py +++ b/samples/server/petstore/python-blueplanet/app/openapi_server/models/category.py @@ -37,15 +37,17 @@ class Category(Model): self._name = name @classmethod - def from_dict(cls, dikt) -> 'Category': + def from_dict(cls, dikt, attr_map=True) -> 'Category': """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The Category of this Category. # noqa: E501 :rtype: Category """ - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) @property def id(self) -> int: diff --git a/samples/server/petstore/python-blueplanet/app/openapi_server/models/order.py b/samples/server/petstore/python-blueplanet/app/openapi_server/models/order.py index e222f5ee0069dbb383352d37536a24db5e77ec3c..90fe2b73271cfcd2321d10b15818b8c27e9b769f 100644 --- a/samples/server/petstore/python-blueplanet/app/openapi_server/models/order.py +++ b/samples/server/petstore/python-blueplanet/app/openapi_server/models/order.py @@ -57,15 +57,17 @@ class Order(Model): self._complete = complete @classmethod - def from_dict(cls, dikt) -> 'Order': + def from_dict(cls, dikt, attr_map=True) -> 'Order': """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The Order of this Order. # noqa: E501 :rtype: Order """ - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) @property def id(self) -> int: diff --git a/samples/server/petstore/python-blueplanet/app/openapi_server/models/pet.py b/samples/server/petstore/python-blueplanet/app/openapi_server/models/pet.py index 0a427ef53e19c34d421e5041d1e7aec9a308d98a..eddc14e135d45c9f856450ac7a774c2eb941bfaa 100644 --- a/samples/server/petstore/python-blueplanet/app/openapi_server/models/pet.py +++ b/samples/server/petstore/python-blueplanet/app/openapi_server/models/pet.py @@ -59,15 +59,17 @@ class Pet(Model): self._status = status @classmethod - def from_dict(cls, dikt) -> 'Pet': + def from_dict(cls, dikt, attr_map=True) -> 'Pet': """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The Pet of this Pet. # noqa: E501 :rtype: Pet """ - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) @property def id(self) -> int: diff --git a/samples/server/petstore/python-blueplanet/app/openapi_server/models/tag.py b/samples/server/petstore/python-blueplanet/app/openapi_server/models/tag.py index 19a8f833ac259bbedb6aa21a4e6d25c9d98a5ab2..11c6ebb396b181499c41167c20161fe039fcc865 100644 --- a/samples/server/petstore/python-blueplanet/app/openapi_server/models/tag.py +++ b/samples/server/petstore/python-blueplanet/app/openapi_server/models/tag.py @@ -37,15 +37,17 @@ class Tag(Model): self._name = name @classmethod - def from_dict(cls, dikt) -> 'Tag': + def from_dict(cls, dikt, attr_map=True) -> 'Tag': """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The Tag of this Tag. # noqa: E501 :rtype: Tag """ - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) @property def id(self) -> int: diff --git a/samples/server/petstore/python-blueplanet/app/openapi_server/models/user.py b/samples/server/petstore/python-blueplanet/app/openapi_server/models/user.py index e45a8a55a4d8b637ef335d805e98adcf34192eca..6354e994c1321e6e65c2f45ac36ebcecb9d3fe83 100644 --- a/samples/server/petstore/python-blueplanet/app/openapi_server/models/user.py +++ b/samples/server/petstore/python-blueplanet/app/openapi_server/models/user.py @@ -67,15 +67,17 @@ class User(Model): self._user_status = user_status @classmethod - def from_dict(cls, dikt) -> 'User': + def from_dict(cls, dikt, attr_map=True) -> 'User': """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The User of this User. # noqa: E501 :rtype: User """ - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) @property def id(self) -> int: diff --git a/samples/server/petstore/python-blueplanet/app/openapi_server/util.py b/samples/server/petstore/python-blueplanet/app/openapi_server/util.py index fc9f8b16f0b3f5a76cde0f3808081c03125a4b38..c78361ff386bcb8811a48e38b48847e8d47658de 100644 --- a/samples/server/petstore/python-blueplanet/app/openapi_server/util.py +++ b/samples/server/petstore/python-blueplanet/app/openapi_server/util.py @@ -91,24 +91,33 @@ def deserialize_datetime(string): return string -def deserialize_model(data, klass): +def deserialize_model(data, klass, attr_map=True): """Deserializes list or dict to model. :param data: dict, list. :type data: dict | list :param klass: class literal. + :param attr_map: defines if attribute_map is used in dict. + :type attr_map: bool :return: model object. """ instance = klass() + # should be included in return types if not instance.swagger_types: return data - for attr, attr_type in six.iteritems(instance.swagger_types): - if data is not None \ - and instance.attribute_map[attr] in data \ - and isinstance(data, (list, dict)): - value = data[instance.attribute_map[attr]] + if data is None: + return instance + + if not isinstance(data, (list, dict)): + return instance + + for attr, attr_type in six.iteritems(instance.openapi_types): + dict_attr = instance.attribute_map[attr] if attr_map else attr + + if dict_attr in data: + value = data[dict_attr] setattr(instance, attr, _deserialize(value, attr_type)) return instance diff --git a/samples/server/petstore/python-flask/openapi_server/models/api_response.py b/samples/server/petstore/python-flask/openapi_server/models/api_response.py index 1e23da9c2304ed01c8e44c26d7d642094946a709..a1e15a10d8c66054c41663c21259b5984bd20b18 100644 --- a/samples/server/petstore/python-flask/openapi_server/models/api_response.py +++ b/samples/server/petstore/python-flask/openapi_server/models/api_response.py @@ -42,15 +42,17 @@ class ApiResponse(Model): self._message = message @classmethod - def from_dict(cls, dikt) -> 'ApiResponse': + def from_dict(cls, dikt, attr_map=True) -> 'ApiResponse': """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The ApiResponse of this ApiResponse. # noqa: E501 :rtype: ApiResponse """ - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) @property def code(self): diff --git a/samples/server/petstore/python-flask/openapi_server/models/base_model_.py b/samples/server/petstore/python-flask/openapi_server/models/base_model_.py index ec33e3ba3cff3295c23bd0cef2443f8c8ba464d6..604c60a057e55cadcf4671e54ac184067c81b5d1 100644 --- a/samples/server/petstore/python-flask/openapi_server/models/base_model_.py +++ b/samples/server/petstore/python-flask/openapi_server/models/base_model_.py @@ -18,29 +18,33 @@ class Model(object): attribute_map = {} @classmethod - def from_dict(cls: typing.Type[T], dikt) -> T: + def from_dict(cls: typing.Type[T], dikt, attr_map=True) -> T: """Returns the dict as a model""" - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) - def to_dict(self): + def to_dict(self, attr_map=True): """Returns the model properties as a dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :rtype: dict """ result = {} for attr, _ in six.iteritems(self.openapi_types): value = getattr(self, attr) + + attr = self.attribute_map[attr] if attr_map else attr if isinstance(value, list): result[attr] = list(map( - lambda x: x.to_dict() if hasattr(x, "to_dict") else x, + lambda x: x.to_dict(attr_map=attr_map) if hasattr(x, "to_dict") else x, value )) elif hasattr(value, "to_dict"): - result[attr] = value.to_dict() + result[attr] = value.to_dict(attr_map=attr_map) elif isinstance(value, dict): result[attr] = dict(map( - lambda item: (item[0], item[1].to_dict()) + lambda item: (item[0], item[1].to_dict(attr_map=attr_map)) if hasattr(item[1], "to_dict") else item, value.items() )) diff --git a/samples/server/petstore/python-flask/openapi_server/models/category.py b/samples/server/petstore/python-flask/openapi_server/models/category.py index 3a68d86c250c8def470431bdf0904431b3d679e0..1d2a1ffd3da19de989e0cd8399b149430569c100 100644 --- a/samples/server/petstore/python-flask/openapi_server/models/category.py +++ b/samples/server/petstore/python-flask/openapi_server/models/category.py @@ -37,15 +37,17 @@ class Category(Model): self._name = name @classmethod - def from_dict(cls, dikt) -> 'Category': + def from_dict(cls, dikt, attr_map=True) -> 'Category': """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The Category of this Category. # noqa: E501 :rtype: Category """ - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) @property def id(self): diff --git a/samples/server/petstore/python-flask/openapi_server/models/order.py b/samples/server/petstore/python-flask/openapi_server/models/order.py index aa8a7a71c393fe517abfef471b4af37bcb1769f9..db9b75435dd7ef82a65969315f52ccae7bb1190e 100644 --- a/samples/server/petstore/python-flask/openapi_server/models/order.py +++ b/samples/server/petstore/python-flask/openapi_server/models/order.py @@ -57,15 +57,17 @@ class Order(Model): self._complete = complete @classmethod - def from_dict(cls, dikt) -> 'Order': + def from_dict(cls, dikt, attr_map=True) -> 'Order': """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The Order of this Order. # noqa: E501 :rtype: Order """ - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) @property def id(self): diff --git a/samples/server/petstore/python-flask/openapi_server/models/pet.py b/samples/server/petstore/python-flask/openapi_server/models/pet.py index e61674165e1cddaafee4d3f25558a0861e4d40e3..f43c0fce878377d3957ce2cb3dd5571a987efbf2 100644 --- a/samples/server/petstore/python-flask/openapi_server/models/pet.py +++ b/samples/server/petstore/python-flask/openapi_server/models/pet.py @@ -61,15 +61,17 @@ class Pet(Model): self._status = status @classmethod - def from_dict(cls, dikt) -> 'Pet': + def from_dict(cls, dikt, attr_map=True) -> 'Pet': """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The Pet of this Pet. # noqa: E501 :rtype: Pet """ - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) @property def id(self): diff --git a/samples/server/petstore/python-flask/openapi_server/models/tag.py b/samples/server/petstore/python-flask/openapi_server/models/tag.py index bd6fff169078cc6ca0cfc0f3b27507c02a8a0b9f..cc30d615ed9c9095f92ef700fcf5049b87fc357e 100644 --- a/samples/server/petstore/python-flask/openapi_server/models/tag.py +++ b/samples/server/petstore/python-flask/openapi_server/models/tag.py @@ -37,15 +37,17 @@ class Tag(Model): self._name = name @classmethod - def from_dict(cls, dikt) -> 'Tag': + def from_dict(cls, dikt, attr_map=True) -> 'Tag': """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The Tag of this Tag. # noqa: E501 :rtype: Tag """ - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) @property def id(self): diff --git a/samples/server/petstore/python-flask/openapi_server/models/user.py b/samples/server/petstore/python-flask/openapi_server/models/user.py index 1b1f4bdae7268dce5c6434f13660ebe31ce7cb74..9dacdd7fff9e9f96105a39148302fc2a0c593698 100644 --- a/samples/server/petstore/python-flask/openapi_server/models/user.py +++ b/samples/server/petstore/python-flask/openapi_server/models/user.py @@ -67,15 +67,17 @@ class User(Model): self._user_status = user_status @classmethod - def from_dict(cls, dikt) -> 'User': + def from_dict(cls, dikt, attr_map=True) -> 'User': """Returns the dict as a model :param dikt: A dict. :type: dict + :param attr_map: Defines if attribute_map is used in dict. + :type: bool :return: The User of this User. # noqa: E501 :rtype: User """ - return util.deserialize_model(dikt, cls) + return util.deserialize_model(dikt, cls, attr_map=attr_map) @property def id(self): diff --git a/samples/server/petstore/python-flask/openapi_server/test/test_attribute_map_order.py b/samples/server/petstore/python-flask/openapi_server/test/test_attribute_map_order.py new file mode 100644 index 0000000000000000000000000000000000000000..1db2605c207838091d95e917551c785abcd96d71 --- /dev/null +++ b/samples/server/petstore/python-flask/openapi_server/test/test_attribute_map_order.py @@ -0,0 +1,68 @@ +# coding: utf-8 + +from __future__ import absolute_import +import unittest +from datetime import datetime + +from openapi_server.models.api_response import ApiResponse # noqa: E501 +from openapi_server.models.order import Order +from openapi_server.test import BaseTestCase + + +class TestOrderAttributeMap(BaseTestCase): + """Model serialization has to respect attribute_map on the class""" + + def setUp(self) -> None: + now = datetime.now() + + self.data = { + "id": 0, + "pet_id": 1, + "quantity": 25, + "ship_date": now, + "status": "placed", + "complete": True + } + + self.desired_data = { + "id": 0, + "petId": 1, + "quantity": 25, + "shipDate": now, + "status": "placed", + "complete": True + } + + def test_order_to_dict_default(self): + """ + By default `attribute_map` uses `True` value, + attribute_map of the class is respected. + """ + order = Order(**self.data) + dikt = order.to_dict() + self.assertEqual(dikt, self.desired_data) + + def test_order_to_dict_false(self): + """ + Passing `False` into `to_dict` method does not use + `attribute_map` on the class. + """ + order = Order(**self.data) + dikt = order.to_dict(attr_map=False) + self.assertEqual(dikt, self.data) + + def test_order_from_dict_to_dict(self): + """ + Creates new `Order` instance by using `from_dict` method, + passing in data from existing `Order` `to_dict` method. + Instances and their `dict` data are equal. + """ + order = Order(**self.data) + order2 = Order.from_dict(order.to_dict()) + + self.assertEqual(order.to_dict(), order2.to_dict()) + self.assertEqual(order, order2) + + +if __name__ == '__main__': + unittest.main() diff --git a/samples/server/petstore/python-flask/openapi_server/test/test_attribute_map_pet.py b/samples/server/petstore/python-flask/openapi_server/test/test_attribute_map_pet.py new file mode 100644 index 0000000000000000000000000000000000000000..53dfebf287bb48ac980f1fedbceac52ae8b53f7a --- /dev/null +++ b/samples/server/petstore/python-flask/openapi_server/test/test_attribute_map_pet.py @@ -0,0 +1,88 @@ +# coding: utf-8 + +from __future__ import absolute_import +import unittest + +from openapi_server.models.api_response import ApiResponse # noqa: E501 +from openapi_server.models.pet import Pet +from openapi_server.test import BaseTestCase + + +class TestPetAttributeMap(BaseTestCase): + """Model serialization has to respect attribute_map on the class""" + + def setUp(self) -> None: + + self.data = { + "photo_urls": ["photoUrls", "photoUrls"], + "name": "doggie", + "id": 0, + "category": { + "name": "name", + "id": 6 + }, + "tags": [{ + "name": "name", + "id": 1 + }, { + "name": "name", + "id": 1 + }], + "status": "available" + } + + self.desired_data = { + "photoUrls": ["photoUrls", "photoUrls"], + "name": "doggie", + "id": 0, + "category": { + "name": "name", + "id": 6 + }, + "tags": [{ + "name": "name", + "id": 1 + }, { + "name": "name", + "id": 1 + }], + "status": "available" + } + + def test_pet_to_dict_default(self): + """ + By default `attribute_map` uses `True` value, + attribute_map of the class is respected. + """ + pet = Pet(**self.data) + dikt = pet.to_dict() + self.assertEqual(dikt, self.desired_data) + + def test_pet_to_dict_false(self): + """ + Passing `False` into `to_dict` method does not use + `attribute_map` on the class. + """ + pet = Pet(**self.data) + dikt = pet.to_dict(attr_map=False) + self.assertEqual(dikt, self.data) + + def test_pet_from_dict_to_dict(self): + """ + Creates new `Pet` instance by using `from_dict` method + passing in data from existing pet `to_dict` method. + Instances and their `dict` data are equal. + """ + pet = Pet(**self.data) + pet2 = Pet.from_dict(pet.to_dict()) + + self.assertEqual(pet.to_dict(), pet2.to_dict()) + + self.assertIsInstance(pet, Pet) + self.assertIsInstance(pet2, Pet) + + self.assertEqual(pet, pet2) + + +if __name__ == '__main__': + unittest.main() diff --git a/samples/server/petstore/python-flask/openapi_server/test/test_attribute_map_user.py b/samples/server/petstore/python-flask/openapi_server/test/test_attribute_map_user.py new file mode 100644 index 0000000000000000000000000000000000000000..566ab20d4c208a39aa6ac86a10493b49986a50d7 --- /dev/null +++ b/samples/server/petstore/python-flask/openapi_server/test/test_attribute_map_user.py @@ -0,0 +1,70 @@ +# coding: utf-8 + +from __future__ import absolute_import +import unittest + +from openapi_server.models.api_response import ApiResponse # noqa: E501 +from openapi_server.models.user import User +from openapi_server.test import BaseTestCase + + +class TestUserAttributeMap(BaseTestCase): + """Model serialization has to respect attribute_map on the class""" + + def setUp(self) -> None: + + self.data = { + 'id': 8, + 'username': "john", + 'first_name': "doe", + 'last_name': "johm", + 'email': "example@example.com", + 'password': "123", + 'phone': "123", + 'user_status': 1 + } + + self.desired_data = { + 'id': 8, + 'username': "john", + 'firstName': "doe", + 'lastName': "johm", + 'email': "example@example.com", + 'password': "123", + 'phone': "123", + 'userStatus': 1 + } + + def test_user_to_dict_default(self): + """ + By default `attribute_map` uses `True` value, + attribute_map of the class is respected. + """ + user = User(**self.data) + dikt = user.to_dict() + self.assertEqual(dikt, self.desired_data) + + def test_user_to_dict_false(self): + """ + Passing `False` into `to_dict` method does not use + `attribute_map` on the class. + """ + user = User(**self.data) + dikt = user.to_dict(attr_map=False) + self.assertEqual(dikt, self.data) + + def test_user_from_dict_to_dict(self): + """ + Creates new `User` instance by using `from_dict` method + passing in data from existing user `to_dict` method. + Instances are their `dict` data are equal. + """ + user = User(**self.data) + user2 = User.from_dict(user.to_dict()) + + self.assertEqual(user.to_dict(), user2.to_dict()) + self.assertEqual(user, user2) + + +if __name__ == '__main__': + unittest.main() diff --git a/samples/server/petstore/python-flask/openapi_server/util.py b/samples/server/petstore/python-flask/openapi_server/util.py index e1185a713ec9002e4cb6f0f189bf0f2359a7cfc4..e0fff158e8a93c834b3ed81f55eecb8f47256766 100644 --- a/samples/server/petstore/python-flask/openapi_server/util.py +++ b/samples/server/petstore/python-flask/openapi_server/util.py @@ -91,24 +91,33 @@ def deserialize_datetime(string): return string -def deserialize_model(data, klass): +def deserialize_model(data, klass, attr_map=True): """Deserializes list or dict to model. :param data: dict, list. :type data: dict | list :param klass: class literal. + :param attr_map: defines if attribute_map is used in dict. + :type attr_map: bool :return: model object. """ instance = klass() + # should be included in return types if not instance.openapi_types: return data + if data is None: + return instance + + if not isinstance(data, (list, dict)): + return instance + for attr, attr_type in six.iteritems(instance.openapi_types): - if data is not None \ - and instance.attribute_map[attr] in data \ - and isinstance(data, (list, dict)): - value = data[instance.attribute_map[attr]] + dict_attr = instance.attribute_map[attr] if attr_map else attr + + if dict_attr in data: + value = data[dict_attr] setattr(instance, attr, _deserialize(value, attr_type)) return instance