From 65f925f45c9a07e99f9b32363b3fd548a11dae88 Mon Sep 17 00:00:00 2001 From: wing328 Date: Fri, 7 Oct 2016 15:50:57 +0800 Subject: [PATCH] fix python required property check and validation for optional properties --- .../src/main/resources/python/model.mustache | 16 ++++----- .../python/petstore_api/models/animal.py | 2 ++ .../python/petstore_api/models/cat.py | 2 ++ .../python/petstore_api/models/dog.py | 2 ++ .../python/petstore_api/models/format_test.py | 34 +++++++++++-------- .../python/petstore_api/models/name.py | 2 ++ .../python/petstore_api/models/pet.py | 4 +++ 7 files changed, 39 insertions(+), 23 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index 78fd187b2da..68e064bd190 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -88,37 +88,37 @@ class {{classname}}(object): {{/isContainer}} {{/isEnum}} {{^isEnum}} -{{#hasValidation}} {{#required}} if {{name}} is None: raise ValueError("Invalid value for `{{name}}`, must not be `None`") {{/required}} +{{#hasValidation}} {{#maxLength}} - if len({{name}}) > {{maxLength}}: + if {{name}} is not None and len({{name}}) > {{maxLength}}: raise ValueError("Invalid value for `{{name}}`, length must be less than or equal to `{{maxLength}}`") {{/maxLength}} {{#minLength}} - if len({{name}}) < {{minLength}}: + if {{name}} is not None and len({{name}}) < {{minLength}}: raise ValueError("Invalid value for `{{name}}`, length must be greater than or equal to `{{minLength}}`") {{/minLength}} {{#maximum}} - if {{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}: + if {{name}} is not None and {{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}: raise ValueError("Invalid value for `{{name}}`, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}`{{maximum}}`") {{/maximum}} {{#minimum}} - if {{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}: + if {{name}} is not None and {{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}: raise ValueError("Invalid value for `{{name}}`, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}`{{minimum}}`") {{/minimum}} {{#pattern}} - if not re.search('{{{vendorExtensions.x-regex}}}', {{name}}{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}}): + if {{name}} is not None and not re.search('{{{vendorExtensions.x-regex}}}', {{name}}{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}}): raise ValueError("Invalid value for `{{name}}`, must be a follow pattern or equal to `{{{pattern}}}`") {{/pattern}} {{#maxItems}} - if len({{name}}) > {{maxItems}}: + if {{name}} is not None and len({{name}}) > {{maxItems}}: raise ValueError("Invalid value for `{{name}}`, number of items must be less than or equal to `{{maxItems}}`") {{/maxItems}} {{#minItems}} - if len({{name}}) < {{minItems}}: + if {{name}} is not None and len({{name}}) < {{minItems}}: raise ValueError("Invalid value for `{{name}}`, number of items must be greater than or equal to `{{minItems}}`") {{/minItems}} {{/hasValidation}} diff --git a/samples/client/petstore/python/petstore_api/models/animal.py b/samples/client/petstore/python/petstore_api/models/animal.py index fb902912fbe..23bcbe07457 100644 --- a/samples/client/petstore/python/petstore_api/models/animal.py +++ b/samples/client/petstore/python/petstore_api/models/animal.py @@ -75,6 +75,8 @@ class Animal(object): :param class_name: The class_name of this Animal. :type: str """ + if class_name is None: + raise ValueError("Invalid value for `class_name`, must not be `None`") self._class_name = class_name diff --git a/samples/client/petstore/python/petstore_api/models/cat.py b/samples/client/petstore/python/petstore_api/models/cat.py index 7bd40711c59..361d5d091ae 100644 --- a/samples/client/petstore/python/petstore_api/models/cat.py +++ b/samples/client/petstore/python/petstore_api/models/cat.py @@ -78,6 +78,8 @@ class Cat(object): :param class_name: The class_name of this Cat. :type: str """ + if class_name is None: + raise ValueError("Invalid value for `class_name`, must not be `None`") self._class_name = class_name diff --git a/samples/client/petstore/python/petstore_api/models/dog.py b/samples/client/petstore/python/petstore_api/models/dog.py index 5e84656c958..36a1f1a3cb4 100644 --- a/samples/client/petstore/python/petstore_api/models/dog.py +++ b/samples/client/petstore/python/petstore_api/models/dog.py @@ -78,6 +78,8 @@ class Dog(object): :param class_name: The class_name of this Dog. :type: str """ + if class_name is None: + raise ValueError("Invalid value for `class_name`, must not be `None`") self._class_name = class_name diff --git a/samples/client/petstore/python/petstore_api/models/format_test.py b/samples/client/petstore/python/petstore_api/models/format_test.py index b6dcf81f37f..3c8682fdeca 100644 --- a/samples/client/petstore/python/petstore_api/models/format_test.py +++ b/samples/client/petstore/python/petstore_api/models/format_test.py @@ -108,9 +108,9 @@ class FormatTest(object): :param integer: The integer of this FormatTest. :type: int """ - if integer > 100.0: + if integer is not None and integer > 100.0: raise ValueError("Invalid value for `integer`, must be a value less than or equal to `100.0`") - if integer < 10.0: + if integer is not None and integer < 10.0: raise ValueError("Invalid value for `integer`, must be a value greater than or equal to `10.0`") self._integer = integer @@ -135,9 +135,9 @@ class FormatTest(object): :param int32: The int32 of this FormatTest. :type: int """ - if int32 > 200.0: + if int32 is not None and int32 > 200.0: raise ValueError("Invalid value for `int32`, must be a value less than or equal to `200.0`") - if int32 < 20.0: + if int32 is not None and int32 < 20.0: raise ValueError("Invalid value for `int32`, must be a value greater than or equal to `20.0`") self._int32 = int32 @@ -185,11 +185,11 @@ class FormatTest(object): :param number: The number of this FormatTest. :type: float """ - if not number: + if number is None: raise ValueError("Invalid value for `number`, must not be `None`") - if number > 543.2: + if number is not None and number > 543.2: raise ValueError("Invalid value for `number`, must be a value less than or equal to `543.2`") - if number < 32.1: + if number is not None and number < 32.1: raise ValueError("Invalid value for `number`, must be a value greater than or equal to `32.1`") self._number = number @@ -214,9 +214,9 @@ class FormatTest(object): :param float: The float of this FormatTest. :type: float """ - if float > 987.6: + if float is not None and float > 987.6: raise ValueError("Invalid value for `float`, must be a value less than or equal to `987.6`") - if float < 54.3: + if float is not None and float < 54.3: raise ValueError("Invalid value for `float`, must be a value greater than or equal to `54.3`") self._float = float @@ -241,9 +241,9 @@ class FormatTest(object): :param double: The double of this FormatTest. :type: float """ - if double > 123.4: + if double is not None and double > 123.4: raise ValueError("Invalid value for `double`, must be a value less than or equal to `123.4`") - if double < 67.8: + if double is not None and double < 67.8: raise ValueError("Invalid value for `double`, must be a value greater than or equal to `67.8`") self._double = double @@ -268,7 +268,7 @@ class FormatTest(object): :param string: The string of this FormatTest. :type: str """ - if not re.search('[a-z]', string, flags=re.IGNORECASE): + if string is not None and not re.search('[a-z]', string, flags=re.IGNORECASE): raise ValueError("Invalid value for `string`, must be a follow pattern or equal to `/[a-z]/i`") self._string = string @@ -293,6 +293,8 @@ class FormatTest(object): :param byte: The byte of this FormatTest. :type: str """ + if byte is None: + raise ValueError("Invalid value for `byte`, must not be `None`") self._byte = byte @@ -339,6 +341,8 @@ class FormatTest(object): :param date: The date of this FormatTest. :type: date """ + if date is None: + raise ValueError("Invalid value for `date`, must not be `None`") self._date = date @@ -408,11 +412,11 @@ class FormatTest(object): :param password: The password of this FormatTest. :type: str """ - if not password: + if password is None: raise ValueError("Invalid value for `password`, must not be `None`") - if len(password) > 64: + if password is not None and len(password) > 64: raise ValueError("Invalid value for `password`, length must be less than or equal to `64`") - if len(password) < 10: + if password is not None and len(password) < 10: raise ValueError("Invalid value for `password`, length must be greater than or equal to `10`") self._password = password diff --git a/samples/client/petstore/python/petstore_api/models/name.py b/samples/client/petstore/python/petstore_api/models/name.py index cb5c08f3736..a613643c624 100644 --- a/samples/client/petstore/python/petstore_api/models/name.py +++ b/samples/client/petstore/python/petstore_api/models/name.py @@ -81,6 +81,8 @@ class Name(object): :param name: The name of this Name. :type: int """ + if name is None: + raise ValueError("Invalid value for `name`, must not be `None`") self._name = name diff --git a/samples/client/petstore/python/petstore_api/models/pet.py b/samples/client/petstore/python/petstore_api/models/pet.py index be58ba7af95..93d3699cd0d 100644 --- a/samples/client/petstore/python/petstore_api/models/pet.py +++ b/samples/client/petstore/python/petstore_api/models/pet.py @@ -133,6 +133,8 @@ class Pet(object): :param name: The name of this Pet. :type: str """ + if name is None: + raise ValueError("Invalid value for `name`, must not be `None`") self._name = name @@ -156,6 +158,8 @@ class Pet(object): :param photo_urls: The photo_urls of this Pet. :type: list[str] """ + if photo_urls is None: + raise ValueError("Invalid value for `photo_urls`, must not be `None`") self._photo_urls = photo_urls