Permalink
Browse files

Python improved (#5512)

* ignore .vscode

* fixed test case for models requiring mandatory attributes. added deserialize test for enum class

* construct model with mandatory attributes. improve class reflection (e.g. deserialize). disable generated empty model unit test for further FIXME

* rebuilt samples

* FIXME comment

* FIXME comment

* fix deserialization enum test
  • Loading branch information...
1 parent 5bfd723 commit a6b7f60ac74a4cc525d5e93e23bdfed7ae0d46d8 @easz easz committed with wing328 May 17, 2017
Showing with 924 additions and 1,174 deletions.
  1. +1 −0 .gitignore
  2. +8 −7 modules/swagger-codegen/src/main/resources/python/api_client.mustache
  3. +27 −19 modules/swagger-codegen/src/main/resources/python/model.mustache
  4. +3 −1 modules/swagger-codegen/src/main/resources/python/model_test.mustache
  5. +8 −7 samples/client/petstore/python/petstore_api/api_client.py
  6. +19 −17 samples/client/petstore/python/petstore_api/models/additional_properties_class.py
  7. +20 −19 samples/client/petstore/python/petstore_api/models/animal.py
  8. +17 −15 samples/client/petstore/python/petstore_api/models/animal_farm.py
  9. +21 −19 samples/client/petstore/python/petstore_api/models/api_response.py
  10. +17 −15 samples/client/petstore/python/petstore_api/models/array_of_array_of_number_only.py
  11. +17 −15 samples/client/petstore/python/petstore_api/models/array_of_number_only.py
  12. +21 −19 samples/client/petstore/python/petstore_api/models/array_test.py
  13. +28 −26 samples/client/petstore/python/petstore_api/models/capitalization.py
  14. +22 −21 samples/client/petstore/python/petstore_api/models/cat.py
  15. +19 −17 samples/client/petstore/python/petstore_api/models/category.py
  16. +17 −15 samples/client/petstore/python/petstore_api/models/class_model.py
  17. +17 −15 samples/client/petstore/python/petstore_api/models/client.py
  18. +22 −21 samples/client/petstore/python/petstore_api/models/dog.py
  19. +19 −17 samples/client/petstore/python/petstore_api/models/enum_arrays.py
  20. +18 −15 samples/client/petstore/python/petstore_api/models/enum_class.py
  21. +24 −22 samples/client/petstore/python/petstore_api/models/enum_test.py
  22. +46 −48 samples/client/petstore/python/petstore_api/models/format_test.py
  23. +19 −17 samples/client/petstore/python/petstore_api/models/has_only_read_only.py
  24. +17 −15 samples/client/petstore/python/petstore_api/models/list.py
  25. +19 −17 samples/client/petstore/python/petstore_api/models/map_test.py
  26. +21 −19 ...es/client/petstore/python/petstore_api/models/mixed_properties_and_additional_properties_class.py
  27. +19 −17 samples/client/petstore/python/petstore_api/models/model_200_response.py
  28. +17 −15 samples/client/petstore/python/petstore_api/models/model_return.py
  29. +25 −24 samples/client/petstore/python/petstore_api/models/name.py
  30. +17 −15 samples/client/petstore/python/petstore_api/models/number_only.py
  31. +28 −26 samples/client/petstore/python/petstore_api/models/order.py
  32. +18 −15 samples/client/petstore/python/petstore_api/models/outer_enum.py
  33. +30 −30 samples/client/petstore/python/petstore_api/models/pet.py
  34. +19 −17 samples/client/petstore/python/petstore_api/models/read_only_first.py
  35. +17 −15 samples/client/petstore/python/petstore_api/models/special_model_name.py
  36. +19 −17 samples/client/petstore/python/petstore_api/models/tag.py
  37. +32 −30 samples/client/petstore/python/petstore_api/models/user.py
  38. +5 −14 samples/client/petstore/python/test/test_additional_properties_class.py
  39. +5 −14 samples/client/petstore/python/test/test_animal.py
  40. +5 −14 samples/client/petstore/python/test/test_animal_farm.py
  41. +5 −14 samples/client/petstore/python/test/test_api_response.py
  42. +4 −13 samples/client/petstore/python/test/test_array_of_array_of_number_only.py
  43. +4 −13 samples/client/petstore/python/test/test_array_of_number_only.py
  44. +5 −14 samples/client/petstore/python/test/test_array_test.py
  45. +3 −1 samples/client/petstore/python/test/test_capitalization.py
  46. +5 −14 samples/client/petstore/python/test/test_cat.py
  47. +5 −14 samples/client/petstore/python/test/test_category.py
  48. +3 −1 samples/client/petstore/python/test/test_class_model.py
  49. +4 −13 samples/client/petstore/python/test/test_client.py
  50. +5 −14 samples/client/petstore/python/test/test_dog.py
  51. +4 −13 samples/client/petstore/python/test/test_enum_arrays.py
  52. +5 −14 samples/client/petstore/python/test/test_enum_class.py
  53. +5 −14 samples/client/petstore/python/test/test_enum_test.py
  54. +18 −13 samples/client/petstore/python/test/test_fake_api.py
  55. +0 −44 samples/client/petstore/python/test/test_fake_classname_tags_123_api.py
  56. +5 −14 samples/client/petstore/python/test/test_format_test.py
  57. +4 −13 samples/client/petstore/python/test/test_has_only_read_only.py
  58. +4 −13 samples/client/petstore/python/test/test_list.py
  59. +4 −13 samples/client/petstore/python/test/test_map_test.py
  60. +5 −14 samples/client/petstore/python/test/test_mixed_properties_and_additional_properties_class.py
  61. +5 −14 samples/client/petstore/python/test/test_model_200_response.py
  62. +5 −14 samples/client/petstore/python/test/test_model_return.py
  63. +5 −14 samples/client/petstore/python/test/test_name.py
  64. +4 −13 samples/client/petstore/python/test/test_number_only.py
  65. +5 −14 samples/client/petstore/python/test/test_order.py
  66. +3 −1 samples/client/petstore/python/test/test_outer_enum.py
  67. +5 −14 samples/client/petstore/python/test/test_pet.py
  68. +2 −13 samples/client/petstore/python/test/test_pet_api.py
  69. +5 −14 samples/client/petstore/python/test/test_read_only_first.py
  70. +5 −14 samples/client/petstore/python/test/test_special_model_name.py
  71. +2 −13 samples/client/petstore/python/test/test_store_api.py
  72. +5 −14 samples/client/petstore/python/test/test_tag.py
  73. +5 −14 samples/client/petstore/python/test/test_user.py
  74. +2 −13 samples/client/petstore/python/test/test_user_api.py
  75. +1 −3 samples/client/petstore/python/tests/test_api_client.py
  76. +1 −3 samples/client/petstore/python/tests/test_api_exception.py
  77. +20 −0 samples/client/petstore/python/tests/test_deserialization.py
  78. +1 −3 samples/client/petstore/python/tests/test_pet_api.py
  79. +3 −9 samples/client/petstore/python/tests/test_pet_model.py
View
@@ -23,6 +23,7 @@ packages/
.pub
.packages
.vagrant/
+.vscode/
.settings
@@ -608,16 +608,17 @@ class ApiClient(object):
:param klass: class literal.
:return: model object.
"""
- instance = klass()
-
- if not instance.swagger_types:
+ if not klass.swagger_types:
return data
- for attr, attr_type in iteritems(instance.swagger_types):
+ kwargs = {}
+ for attr, attr_type in iteritems(klass.swagger_types):
if data is not None \
- and instance.attribute_map[attr] in data \
+ and klass.attribute_map[attr] in data \
and isinstance(data, (list, dict)):
- value = data[instance.attribute_map[attr]]
- setattr(instance, attr, self.__deserialize(value, attr_type))
+ value = data[klass.attribute_map[attr]]
+ kwargs[attr] = self.__deserialize(value, attr_type)
+
+ instance = klass(**kwargs)
return instance
@@ -14,42 +14,50 @@ class {{classname}}(object):
NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually.
"""
-{{#allowableValues}}
+{{#allowableValues}}
+ """
+ allowed enum values
+ """
{{#enumVars}}
{{name}} = {{{value}}}
{{/enumVars}}
-
{{/allowableValues}}
+
+ """
+ Attributes:
+ swagger_types (dict): The key is attribute name
+ and the value is attribute type.
+ attribute_map (dict): The key is attribute name
+ and the value is json key in definition.
+ """
+ swagger_types = {
+ {{#vars}}'{{name}}': '{{{datatype}}}'{{#hasMore}},
+ {{/hasMore}}{{/vars}}
+ }
+
+ attribute_map = {
+ {{#vars}}'{{name}}': '{{baseName}}'{{#hasMore}},
+ {{/hasMore}}{{/vars}}
+ }
+
def __init__(self{{#vars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}):
"""
{{classname}} - a model defined in Swagger
-
- :param dict swaggerTypes: The key is attribute name
- and the value is attribute type.
- :param dict attributeMap: The key is attribute name
- and the value is json key in definition.
"""
- self.swagger_types = {
- {{#vars}}'{{name}}': '{{{datatype}}}'{{#hasMore}},
- {{/hasMore}}{{/vars}}
- }
-
- self.attribute_map = {
- {{#vars}}'{{name}}': '{{baseName}}'{{#hasMore}},
- {{/hasMore}}{{/vars}}
- }
{{#vars}}
self._{{name}} = None
{{/vars}}
- # TODO: let required properties as mandatory parameter in the constructor.
- # - to check if required property is not None (e.g. by calling setter)
- # - ApiClient.__deserialize_model has to be adapted as well
{{#vars}}
+{{#required}}
+ self.{{name}} = {{name}}
+{{/required}}
+{{^required}}
if {{name}} is not None:
self.{{name}} = {{name}}
+{{/required}}
{{/vars}}
{{#vars}}
@@ -28,7 +28,9 @@ class Test{{classname}}(unittest.TestCase):
"""
Test {{classname}}
"""
- model = {{packageName}}.models.{{classFilename}}.{{classname}}()
+ # FIXME: construct object with mandatory attributes with example values
+ #model = {{packageName}}.models.{{classFilename}}.{{classname}}()
+ pass
{{/model}}
{{/models}}
@@ -617,16 +617,17 @@ def __deserialize_model(self, data, klass):
:param klass: class literal.
:return: model object.
"""
- instance = klass()
-
- if not instance.swagger_types:
+ if not klass.swagger_types:
return data
- for attr, attr_type in iteritems(instance.swagger_types):
+ kwargs = {}
+ for attr, attr_type in iteritems(klass.swagger_types):
if data is not None \
- and instance.attribute_map[attr] in data \
+ and klass.attribute_map[attr] in data \
and isinstance(data, (list, dict)):
- value = data[instance.attribute_map[attr]]
- setattr(instance, attr, self.__deserialize(value, attr_type))
+ value = data[klass.attribute_map[attr]]
+ kwargs[attr] = self.__deserialize(value, attr_type)
+
+ instance = klass(**kwargs)
return instance
@@ -21,31 +21,33 @@ class AdditionalPropertiesClass(object):
NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually.
"""
+
+
+ """
+ Attributes:
+ swagger_types (dict): The key is attribute name
+ and the value is attribute type.
+ attribute_map (dict): The key is attribute name
+ and the value is json key in definition.
+ """
+ swagger_types = {
+ 'map_property': 'dict(str, str)',
+ 'map_of_map_property': 'dict(str, dict(str, str))'
+ }
+
+ attribute_map = {
+ 'map_property': 'map_property',
+ 'map_of_map_property': 'map_of_map_property'
+ }
+
def __init__(self, map_property=None, map_of_map_property=None):
"""
AdditionalPropertiesClass - a model defined in Swagger
-
- :param dict swaggerTypes: The key is attribute name
- and the value is attribute type.
- :param dict attributeMap: The key is attribute name
- and the value is json key in definition.
"""
- self.swagger_types = {
- 'map_property': 'dict(str, str)',
- 'map_of_map_property': 'dict(str, dict(str, str))'
- }
-
- self.attribute_map = {
- 'map_property': 'map_property',
- 'map_of_map_property': 'map_of_map_property'
- }
self._map_property = None
self._map_of_map_property = None
- # TODO: let required properties as mandatory parameter in the constructor.
- # - to check if required property is not None (e.g. by calling setter)
- # - ApiClient.__deserialize_model has to be adapted as well
if map_property is not None:
self.map_property = map_property
if map_of_map_property is not None:
@@ -21,33 +21,34 @@ class Animal(object):
NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually.
"""
+
+
+ """
+ Attributes:
+ swagger_types (dict): The key is attribute name
+ and the value is attribute type.
+ attribute_map (dict): The key is attribute name
+ and the value is json key in definition.
+ """
+ swagger_types = {
+ 'class_name': 'str',
+ 'color': 'str'
+ }
+
+ attribute_map = {
+ 'class_name': 'className',
+ 'color': 'color'
+ }
+
def __init__(self, class_name=None, color='red'):
"""
Animal - a model defined in Swagger
-
- :param dict swaggerTypes: The key is attribute name
- and the value is attribute type.
- :param dict attributeMap: The key is attribute name
- and the value is json key in definition.
"""
- self.swagger_types = {
- 'class_name': 'str',
- 'color': 'str'
- }
-
- self.attribute_map = {
- 'class_name': 'className',
- 'color': 'color'
- }
self._class_name = None
self._color = None
- # TODO: let required properties as mandatory parameter in the constructor.
- # - to check if required property is not None (e.g. by calling setter)
- # - ApiClient.__deserialize_model has to be adapted as well
- if class_name is not None:
- self.class_name = class_name
+ self.class_name = class_name
if color is not None:
self.color = color
@@ -21,27 +21,29 @@ class AnimalFarm(object):
NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually.
"""
+
+
+ """
+ Attributes:
+ swagger_types (dict): The key is attribute name
+ and the value is attribute type.
+ attribute_map (dict): The key is attribute name
+ and the value is json key in definition.
+ """
+ swagger_types = {
+
+ }
+
+ attribute_map = {
+
+ }
+
def __init__(self):
"""
AnimalFarm - a model defined in Swagger
-
- :param dict swaggerTypes: The key is attribute name
- and the value is attribute type.
- :param dict attributeMap: The key is attribute name
- and the value is json key in definition.
"""
- self.swagger_types = {
-
- }
-
- self.attribute_map = {
-
- }
- # TODO: let required properties as mandatory parameter in the constructor.
- # - to check if required property is not None (e.g. by calling setter)
- # - ApiClient.__deserialize_model has to be adapted as well
def to_dict(self):
"""
@@ -21,34 +21,36 @@ class ApiResponse(object):
NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually.
"""
+
+
+ """
+ Attributes:
+ swagger_types (dict): The key is attribute name
+ and the value is attribute type.
+ attribute_map (dict): The key is attribute name
+ and the value is json key in definition.
+ """
+ swagger_types = {
+ 'code': 'int',
+ 'type': 'str',
+ 'message': 'str'
+ }
+
+ attribute_map = {
+ 'code': 'code',
+ 'type': 'type',
+ 'message': 'message'
+ }
+
def __init__(self, code=None, type=None, message=None):
"""
ApiResponse - a model defined in Swagger
-
- :param dict swaggerTypes: The key is attribute name
- and the value is attribute type.
- :param dict attributeMap: The key is attribute name
- and the value is json key in definition.
"""
- self.swagger_types = {
- 'code': 'int',
- 'type': 'str',
- 'message': 'str'
- }
-
- self.attribute_map = {
- 'code': 'code',
- 'type': 'type',
- 'message': 'message'
- }
self._code = None
self._type = None
self._message = None
- # TODO: let required properties as mandatory parameter in the constructor.
- # - to check if required property is not None (e.g. by calling setter)
- # - ApiClient.__deserialize_model has to be adapted as well
if code is not None:
self.code = code
if type is not None:
@@ -21,28 +21,30 @@ class ArrayOfArrayOfNumberOnly(object):
NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually.
"""
+
+
+ """
+ Attributes:
+ swagger_types (dict): The key is attribute name
+ and the value is attribute type.
+ attribute_map (dict): The key is attribute name
+ and the value is json key in definition.
+ """
+ swagger_types = {
+ 'array_array_number': 'list[list[float]]'
+ }
+
+ attribute_map = {
+ 'array_array_number': 'ArrayArrayNumber'
+ }
+
def __init__(self, array_array_number=None):
"""
ArrayOfArrayOfNumberOnly - a model defined in Swagger
-
- :param dict swaggerTypes: The key is attribute name
- and the value is attribute type.
- :param dict attributeMap: The key is attribute name
- and the value is json key in definition.
"""
- self.swagger_types = {
- 'array_array_number': 'list[list[float]]'
- }
-
- self.attribute_map = {
- 'array_array_number': 'ArrayArrayNumber'
- }
self._array_array_number = None
- # TODO: let required properties as mandatory parameter in the constructor.
- # - to check if required property is not None (e.g. by calling setter)
- # - ApiClient.__deserialize_model has to be adapted as well
if array_array_number is not None:
self.array_array_number = array_array_number
Oops, something went wrong.

0 comments on commit a6b7f60

Please sign in to comment.