From f0e09ae33a60c93edd0083710ce8663614b207af Mon Sep 17 00:00:00 2001 From: geekerzp Date: Thu, 16 Jul 2015 17:24:42 +0800 Subject: [PATCH] Add enum support for model property of python client --- .../src/main/resources/python/api.mustache | 3 +- .../main/resources/python/api_client.mustache | 2 +- .../src/main/resources/python/model.mustache | 14 ++- .../python/swagger_client/api_client.py | 2 +- .../python/swagger_client/apis/pet_api.py | 4 - .../python/swagger_client/apis/store_api.py | 2 - .../python/swagger_client/apis/user_api.py | 3 - .../python/swagger_client/models/category.py | 22 ++++- .../python/swagger_client/models/order.py | 69 +++++++++++++-- .../python/swagger_client/models/pet.py | 69 +++++++++++++-- .../python/swagger_client/models/tag.py | 22 ++++- .../python/swagger_client/models/user.py | 88 +++++++++++++++++-- .../petstore/python/tests/test_order_model.py | 25 ++++++ 13 files changed, 287 insertions(+), 38 deletions(-) create mode 100644 samples/client/petstore/python/tests/test_order_model.py diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index 8c9e719e00..2525497b32 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -51,8 +51,7 @@ class {{classname}}(object): {{/allParams}} :return: {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}None{{/returnType}} """ - {{#allParams}}{{#required}} - # verify the required parameter '{{paramName}}' is set + {{#allParams}}{{#required}}# verify the required parameter '{{paramName}}' is set if {{paramName}} is None: raise ValueError("Missing the required parameter `{{paramName}}` when calling `{{nickname}}`") {{/required}}{{/allParams}} diff --git a/modules/swagger-codegen/src/main/resources/python/api_client.mustache b/modules/swagger-codegen/src/main/resources/python/api_client.mustache index 9188c85132..d6fe9c3793 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -149,7 +149,7 @@ class ApiClient(object): # Convert model obj to dict except attributes `swagger_types`, `attribute_map` # and attributes which value is not None. # Convert attribute name to json key in model definition for request. - obj_dict = {obj.attribute_map[key]: val + obj_dict = {obj.attribute_map[key[1:]]: val for key, val in iteritems(obj.__dict__) if key != 'swagger_types' and key != 'attribute_map' and val is not None} return {key: self.sanitize_for_serialization(val) diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index 429eac33ec..3dda53963a 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -43,9 +43,21 @@ class {{classname}}(object): } {{#vars}} {{#description}}# {{description}}{{/description}} - self.{{name}} = None # {{{datatype}}} + self._{{name}} = None # {{{datatype}}} {{/vars}} + {{#vars}} + @property + def {{name}}(self): + return self._{{name}} + @{{name}}.setter + def {{name}}(self, {{name}}): + {{#isEnum}}allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] + if {{name}} not in allowed_values: + raise ValueError("Invalid value for `{{name}}`, must be one of {0}".format(allowed_values)) + {{/isEnum}} + self._{{name}} = {{name}} + {{/vars}} def __repr__(self): properties = [] for p in self.__dict__: diff --git a/samples/client/petstore/python/swagger_client/api_client.py b/samples/client/petstore/python/swagger_client/api_client.py index 9188c85132..d6fe9c3793 100644 --- a/samples/client/petstore/python/swagger_client/api_client.py +++ b/samples/client/petstore/python/swagger_client/api_client.py @@ -149,7 +149,7 @@ class ApiClient(object): # Convert model obj to dict except attributes `swagger_types`, `attribute_map` # and attributes which value is not None. # Convert attribute name to json key in model definition for request. - obj_dict = {obj.attribute_map[key]: val + obj_dict = {obj.attribute_map[key[1:]]: val for key, val in iteritems(obj.__dict__) if key != 'swagger_types' and key != 'attribute_map' and val is not None} return {key: self.sanitize_for_serialization(val) diff --git a/samples/client/petstore/python/swagger_client/apis/pet_api.py b/samples/client/petstore/python/swagger_client/apis/pet_api.py index 683e807adb..656ce8a576 100644 --- a/samples/client/petstore/python/swagger_client/apis/pet_api.py +++ b/samples/client/petstore/python/swagger_client/apis/pet_api.py @@ -258,7 +258,6 @@ class PetApi(object): :return: Pet """ - # verify the required parameter 'pet_id' is set if pet_id is None: raise ValueError("Missing the required parameter `pet_id` when calling `get_pet_by_id`") @@ -317,7 +316,6 @@ class PetApi(object): :return: None """ - # verify the required parameter 'pet_id' is set if pet_id is None: raise ValueError("Missing the required parameter `pet_id` when calling `update_pet_with_form`") @@ -379,7 +377,6 @@ class PetApi(object): :return: None """ - # verify the required parameter 'pet_id' is set if pet_id is None: raise ValueError("Missing the required parameter `pet_id` when calling `delete_pet`") @@ -439,7 +436,6 @@ class PetApi(object): :return: None """ - # verify the required parameter 'pet_id' is set if pet_id is None: raise ValueError("Missing the required parameter `pet_id` when calling `upload_file`") diff --git a/samples/client/petstore/python/swagger_client/apis/store_api.py b/samples/client/petstore/python/swagger_client/apis/store_api.py index 06775fdc86..e1e825d1c6 100644 --- a/samples/client/petstore/python/swagger_client/apis/store_api.py +++ b/samples/client/petstore/python/swagger_client/apis/store_api.py @@ -152,7 +152,6 @@ class StoreApi(object): :return: Order """ - # verify the required parameter 'order_id' is set if order_id is None: raise ValueError("Missing the required parameter `order_id` when calling `get_order_by_id`") @@ -209,7 +208,6 @@ class StoreApi(object): :return: None """ - # verify the required parameter 'order_id' is set if order_id is None: raise ValueError("Missing the required parameter `order_id` when calling `delete_order`") diff --git a/samples/client/petstore/python/swagger_client/apis/user_api.py b/samples/client/petstore/python/swagger_client/apis/user_api.py index 8299a95c2a..ba0fd5e0f1 100644 --- a/samples/client/petstore/python/swagger_client/apis/user_api.py +++ b/samples/client/petstore/python/swagger_client/apis/user_api.py @@ -307,7 +307,6 @@ class UserApi(object): :return: User """ - # verify the required parameter 'username' is set if username is None: raise ValueError("Missing the required parameter `username` when calling `get_user_by_name`") @@ -365,7 +364,6 @@ class UserApi(object): :return: None """ - # verify the required parameter 'username' is set if username is None: raise ValueError("Missing the required parameter `username` when calling `update_user`") @@ -423,7 +421,6 @@ class UserApi(object): :return: None """ - # verify the required parameter 'username' is set if username is None: raise ValueError("Missing the required parameter `username` when calling `delete_user`") diff --git a/samples/client/petstore/python/swagger_client/models/category.py b/samples/client/petstore/python/swagger_client/models/category.py index cfd69ee955..f2be305118 100644 --- a/samples/client/petstore/python/swagger_client/models/category.py +++ b/samples/client/petstore/python/swagger_client/models/category.py @@ -41,12 +41,30 @@ class Category(object): } - self.id = None # int + self._id = None # int - self.name = None # str + self._name = None # str + + @property + def id(self): + return self._id + @id.setter + def id(self, id): + + self._id = id + + @property + def name(self): + return self._name + + @name.setter + def name(self, name): + + self._name = name + def __repr__(self): properties = [] for p in self.__dict__: diff --git a/samples/client/petstore/python/swagger_client/models/order.py b/samples/client/petstore/python/swagger_client/models/order.py index 2e448f5f42..34774c6c96 100644 --- a/samples/client/petstore/python/swagger_client/models/order.py +++ b/samples/client/petstore/python/swagger_client/models/order.py @@ -49,24 +49,81 @@ class Order(object): } - self.id = None # int + self._id = None # int - self.pet_id = None # int + self._pet_id = None # int - self.quantity = None # int + self._quantity = None # int - self.ship_date = None # datetime + self._ship_date = None # datetime # Order Status - self.status = None # str + self._status = None # str - self.complete = None # bool + self._complete = None # bool + + @property + def id(self): + return self._id + @id.setter + def id(self, id): + + self._id = id + + @property + def pet_id(self): + return self._pet_id + + @pet_id.setter + def pet_id(self, pet_id): + + self._pet_id = pet_id + + @property + def quantity(self): + return self._quantity + + @quantity.setter + def quantity(self, quantity): + + self._quantity = quantity + + @property + def ship_date(self): + return self._ship_date + + @ship_date.setter + def ship_date(self, ship_date): + + self._ship_date = ship_date + + @property + def status(self): + return self._status + + @status.setter + def status(self, status): + allowed_values = ["placed", "approved", "delivered"] + if status not in allowed_values: + raise ValueError("Invalid value for `status`, must be one of {0}".format(allowed_values)) + + self._status = status + + @property + def complete(self): + return self._complete + + @complete.setter + def complete(self, complete): + + self._complete = complete + def __repr__(self): properties = [] for p in self.__dict__: diff --git a/samples/client/petstore/python/swagger_client/models/pet.py b/samples/client/petstore/python/swagger_client/models/pet.py index a943b63966..37a4ca89b4 100644 --- a/samples/client/petstore/python/swagger_client/models/pet.py +++ b/samples/client/petstore/python/swagger_client/models/pet.py @@ -49,24 +49,81 @@ class Pet(object): } - self.id = None # int + self._id = None # int - self.category = None # Category + self._category = None # Category - self.name = None # str + self._name = None # str - self.photo_urls = None # list[str] + self._photo_urls = None # list[str] - self.tags = None # list[Tag] + self._tags = None # list[Tag] # pet status in the store - self.status = None # str + self._status = None # str + + @property + def id(self): + return self._id + @id.setter + def id(self, id): + + self._id = id + + @property + def category(self): + return self._category + + @category.setter + def category(self, category): + + self._category = category + + @property + def name(self): + return self._name + + @name.setter + def name(self, name): + + self._name = name + + @property + def photo_urls(self): + return self._photo_urls + + @photo_urls.setter + def photo_urls(self, photo_urls): + + self._photo_urls = photo_urls + + @property + def tags(self): + return self._tags + + @tags.setter + def tags(self, tags): + + self._tags = tags + + @property + def status(self): + return self._status + + @status.setter + def status(self, status): + allowed_values = ["available", "pending", "sold"] + if status not in allowed_values: + raise ValueError("Invalid value for `status`, must be one of {0}".format(allowed_values)) + + self._status = status + def __repr__(self): properties = [] for p in self.__dict__: diff --git a/samples/client/petstore/python/swagger_client/models/tag.py b/samples/client/petstore/python/swagger_client/models/tag.py index 6347352eba..63c1f3291b 100644 --- a/samples/client/petstore/python/swagger_client/models/tag.py +++ b/samples/client/petstore/python/swagger_client/models/tag.py @@ -41,12 +41,30 @@ class Tag(object): } - self.id = None # int + self._id = None # int - self.name = None # str + self._name = None # str + + @property + def id(self): + return self._id + @id.setter + def id(self, id): + + self._id = id + + @property + def name(self): + return self._name + + @name.setter + def name(self, name): + + self._name = name + def __repr__(self): properties = [] for p in self.__dict__: diff --git a/samples/client/petstore/python/swagger_client/models/user.py b/samples/client/petstore/python/swagger_client/models/user.py index 83efcd9ee2..7049e1e8c9 100644 --- a/samples/client/petstore/python/swagger_client/models/user.py +++ b/samples/client/petstore/python/swagger_client/models/user.py @@ -53,30 +53,102 @@ class User(object): } - self.id = None # int + self._id = None # int - self.username = None # str + self._username = None # str - self.first_name = None # str + self._first_name = None # str - self.last_name = None # str + self._last_name = None # str - self.email = None # str + self._email = None # str - self.password = None # str + self._password = None # str - self.phone = None # str + self._phone = None # str # User Status - self.user_status = None # int + self._user_status = None # int + + @property + def id(self): + return self._id + @id.setter + def id(self, id): + + self._id = id + + @property + def username(self): + return self._username + + @username.setter + def username(self, username): + + self._username = username + + @property + def first_name(self): + return self._first_name + + @first_name.setter + def first_name(self, first_name): + + self._first_name = first_name + + @property + def last_name(self): + return self._last_name + + @last_name.setter + def last_name(self, last_name): + + self._last_name = last_name + + @property + def email(self): + return self._email + + @email.setter + def email(self, email): + + self._email = email + + @property + def password(self): + return self._password + + @password.setter + def password(self, password): + + self._password = password + + @property + def phone(self): + return self._phone + + @phone.setter + def phone(self, phone): + + self._phone = phone + + @property + def user_status(self): + return self._user_status + + @user_status.setter + def user_status(self, user_status): + + self._user_status = user_status + def __repr__(self): properties = [] for p in self.__dict__: diff --git a/samples/client/petstore/python/tests/test_order_model.py b/samples/client/petstore/python/tests/test_order_model.py new file mode 100644 index 0000000000..e411ca0103 --- /dev/null +++ b/samples/client/petstore/python/tests/test_order_model.py @@ -0,0 +1,25 @@ +# coding: utf-8 + +""" +Run the tests. +$ pip install nose (optional) +$ cd swagger_client-python +$ nosetests -v +""" + +import os +import time +import unittest + +import swagger_client + + +class OrderModelTests(unittest.TestCase): + + def test_status(self): + order = swagger_client.Order() + order.status = "placed" + self.assertEqual("placed", order.status) + + with self.assertRaises(ValueError): + order.status = "invalid"