diff --git a/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache b/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache index 71661abb123..d4f1306e833 100644 --- a/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache +++ b/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache @@ -32,6 +32,18 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} @validator('{{{name}}}') def {{{name}}}_validate_regular_expression(cls, v): + {{^required}} + if v is None: + return v + + {{/required}} + {{#required}} + {{#isNullable}} + if v is None: + return v + + {{/isNullable}} + {{/required}} if not re.match(r"{{{.}}}", v{{#vendorExtensions.x-modifiers}} ,re.{{{.}}}{{/vendorExtensions.x-modifiers}}): raise ValueError(r"must validate the regular expression {{{vendorExtensions.x-pattern}}}") return v @@ -43,6 +55,14 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} {{^required}} if v is None: return v + + {{/required}} + {{#required}} + {{#isNullable}} + if v is None: + return v + + {{/isNullable}} {{/required}} {{#isArray}} for i in v: diff --git a/samples/client/echo_api/python-nextgen/openapi_client/models/default_value.py b/samples/client/echo_api/python-nextgen/openapi_client/models/default_value.py index 0d54cfc46c2..5fa33c19aba 100644 --- a/samples/client/echo_api/python-nextgen/openapi_client/models/default_value.py +++ b/samples/client/echo_api/python-nextgen/openapi_client/models/default_value.py @@ -42,6 +42,7 @@ class DefaultValue(BaseModel): def array_string_enum_default_validate_enum(cls, v): if v is None: return v + for i in v: if i not in ('success', 'failure', 'unclassified'): raise ValueError("each list item must be one of ('success', 'failure', 'unclassified')") diff --git a/samples/client/echo_api/python-nextgen/openapi_client/models/pet.py b/samples/client/echo_api/python-nextgen/openapi_client/models/pet.py index d34df26fa5c..383e8691b35 100644 --- a/samples/client/echo_api/python-nextgen/openapi_client/models/pet.py +++ b/samples/client/echo_api/python-nextgen/openapi_client/models/pet.py @@ -41,6 +41,7 @@ class Pet(BaseModel): def status_validate_enum(cls, v): if v is None: return v + if v not in ('available', 'pending', 'sold'): raise ValueError("must be one of enum values ('available', 'pending', 'sold')") return v diff --git a/samples/client/echo_api/python-nextgen/openapi_client/models/query.py b/samples/client/echo_api/python-nextgen/openapi_client/models/query.py index 531f366ed5c..4f932a13a86 100644 --- a/samples/client/echo_api/python-nextgen/openapi_client/models/query.py +++ b/samples/client/echo_api/python-nextgen/openapi_client/models/query.py @@ -35,6 +35,7 @@ class Query(BaseModel): def outcomes_validate_enum(cls, v): if v is None: return v + for i in v: if i not in ('SUCCESS', 'FAILURE', 'SKIPPED'): raise ValueError("each list item must be one of ('SUCCESS', 'FAILURE', 'SKIPPED')") diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_arrays.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_arrays.py index a3665c53ded..c56f6e927c1 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_arrays.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_arrays.py @@ -34,6 +34,7 @@ class EnumArrays(BaseModel): def just_symbol_validate_enum(cls, v): if v is None: return v + if v not in ('>=', '$'): raise ValueError("must be one of enum values ('>=', '$')") return v @@ -42,6 +43,7 @@ class EnumArrays(BaseModel): def array_enum_validate_enum(cls, v): if v is None: return v + for i in v: if i not in ('fish', 'crab'): raise ValueError("each list item must be one of ('fish', 'crab')") diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_test.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_test.py index fc634228f93..aa8fd784f08 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_test.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_test.py @@ -45,6 +45,7 @@ class EnumTest(BaseModel): def enum_string_validate_enum(cls, v): if v is None: return v + if v not in ('UPPER', 'lower', ''): raise ValueError("must be one of enum values ('UPPER', 'lower', '')") return v @@ -59,6 +60,7 @@ class EnumTest(BaseModel): def enum_integer_default_validate_enum(cls, v): if v is None: return v + if v not in (1, 5, 14): raise ValueError("must be one of enum values (1, 5, 14)") return v @@ -67,6 +69,7 @@ class EnumTest(BaseModel): def enum_integer_validate_enum(cls, v): if v is None: return v + if v not in (1, -1): raise ValueError("must be one of enum values (1, -1)") return v @@ -75,6 +78,7 @@ class EnumTest(BaseModel): def enum_number_validate_enum(cls, v): if v is None: return v + if v not in (1.1, -1.2): raise ValueError("must be one of enum values (1.1, -1.2)") return v diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/format_test.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/format_test.py index 811d3a4ee27..f961ba92fe4 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/format_test.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/format_test.py @@ -47,24 +47,36 @@ class FormatTest(BaseModel): @validator('string') def string_validate_regular_expression(cls, v): + if v is None: + return v + if not re.match(r"[a-z]", v ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /[a-z]/i") return v @validator('string_with_double_quote_pattern') def string_with_double_quote_pattern_validate_regular_expression(cls, v): + if v is None: + return v + if not re.match(r"this is \"something\"", v): raise ValueError(r"must validate the regular expression /this is \"something\"/") return v @validator('pattern_with_digits') def pattern_with_digits_validate_regular_expression(cls, v): + if v is None: + return v + if not re.match(r"^\d{10}$", v): raise ValueError(r"must validate the regular expression /^\d{10}$/") return v @validator('pattern_with_digits_and_delimiter') def pattern_with_digits_and_delimiter_validate_regular_expression(cls, v): + if v is None: + return v + if not re.match(r"^image_\d{1,3}$", v ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /^image_\d{1,3}$/i") return v diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/map_test.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/map_test.py index 36d415ccbf6..512f09d73eb 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/map_test.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/map_test.py @@ -36,6 +36,7 @@ class MapTest(BaseModel): def map_of_enum_string_validate_enum(cls, v): if v is None: return v + if v not in ('UPPER', 'lower'): raise ValueError("must be one of enum values ('UPPER', 'lower')") return v diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/order.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/order.py index c44c3a2eea9..7a61819b8b1 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/order.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/order.py @@ -38,6 +38,7 @@ class Order(BaseModel): def status_validate_enum(cls, v): if v is None: return v + if v not in ('placed', 'approved', 'delivered'): raise ValueError("must be one of enum values ('placed', 'approved', 'delivered')") return v diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/pet.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/pet.py index f8bb0d46377..edaed4375f9 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/pet.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/pet.py @@ -40,6 +40,7 @@ class Pet(BaseModel): def status_validate_enum(cls, v): if v is None: return v + if v not in ('available', 'pending', 'sold'): raise ValueError("must be one of enum values ('available', 'pending', 'sold')") return v diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/special_name.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/special_name.py index 811569b1c98..bdb0c8931b0 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/special_name.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/special_name.py @@ -36,6 +36,7 @@ class SpecialName(BaseModel): def var_schema_validate_enum(cls, v): if v is None: return v + if v not in ('available', 'pending', 'sold'): raise ValueError("must be one of enum values ('available', 'pending', 'sold')") return v diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_arrays.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_arrays.py index 82b7bcfc92d..f270d1cd14d 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_arrays.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_arrays.py @@ -35,6 +35,7 @@ class EnumArrays(BaseModel): def just_symbol_validate_enum(cls, v): if v is None: return v + if v not in ('>=', '$'): raise ValueError("must be one of enum values ('>=', '$')") return v @@ -43,6 +44,7 @@ class EnumArrays(BaseModel): def array_enum_validate_enum(cls, v): if v is None: return v + for i in v: if i not in ('fish', 'crab'): raise ValueError("each list item must be one of ('fish', 'crab')") diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_test.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_test.py index 20db0d562a7..ac1490a8a7d 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_test.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_test.py @@ -46,6 +46,7 @@ class EnumTest(BaseModel): def enum_string_validate_enum(cls, v): if v is None: return v + if v not in ('UPPER', 'lower', ''): raise ValueError("must be one of enum values ('UPPER', 'lower', '')") return v @@ -60,6 +61,7 @@ class EnumTest(BaseModel): def enum_integer_default_validate_enum(cls, v): if v is None: return v + if v not in (1, 5, 14): raise ValueError("must be one of enum values (1, 5, 14)") return v @@ -68,6 +70,7 @@ class EnumTest(BaseModel): def enum_integer_validate_enum(cls, v): if v is None: return v + if v not in (1, -1): raise ValueError("must be one of enum values (1, -1)") return v @@ -76,6 +79,7 @@ class EnumTest(BaseModel): def enum_number_validate_enum(cls, v): if v is None: return v + if v not in (1.1, -1.2): raise ValueError("must be one of enum values (1.1, -1.2)") return v diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/format_test.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/format_test.py index 89f85aa14cd..a543c85c81a 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/format_test.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/format_test.py @@ -48,24 +48,36 @@ class FormatTest(BaseModel): @validator('string') def string_validate_regular_expression(cls, v): + if v is None: + return v + if not re.match(r"[a-z]", v ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /[a-z]/i") return v @validator('string_with_double_quote_pattern') def string_with_double_quote_pattern_validate_regular_expression(cls, v): + if v is None: + return v + if not re.match(r"this is \"something\"", v): raise ValueError(r"must validate the regular expression /this is \"something\"/") return v @validator('pattern_with_digits') def pattern_with_digits_validate_regular_expression(cls, v): + if v is None: + return v + if not re.match(r"^\d{10}$", v): raise ValueError(r"must validate the regular expression /^\d{10}$/") return v @validator('pattern_with_digits_and_delimiter') def pattern_with_digits_and_delimiter_validate_regular_expression(cls, v): + if v is None: + return v + if not re.match(r"^image_\d{1,3}$", v ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /^image_\d{1,3}$/i") return v diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/map_test.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/map_test.py index ac702eb0a7f..30cc0146b07 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/map_test.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/map_test.py @@ -37,6 +37,7 @@ class MapTest(BaseModel): def map_of_enum_string_validate_enum(cls, v): if v is None: return v + if v not in ('UPPER', 'lower'): raise ValueError("must be one of enum values ('UPPER', 'lower')") return v diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/order.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/order.py index cdd825329e9..babf76251b2 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/order.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/order.py @@ -39,6 +39,7 @@ class Order(BaseModel): def status_validate_enum(cls, v): if v is None: return v + if v not in ('placed', 'approved', 'delivered'): raise ValueError("must be one of enum values ('placed', 'approved', 'delivered')") return v diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/pet.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/pet.py index ae46c225407..2a028cde3c7 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/pet.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/pet.py @@ -41,6 +41,7 @@ class Pet(BaseModel): def status_validate_enum(cls, v): if v is None: return v + if v not in ('available', 'pending', 'sold'): raise ValueError("must be one of enum values ('available', 'pending', 'sold')") return v diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/special_name.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/special_name.py index e0a6c30fa77..4715e8dac30 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/special_name.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/special_name.py @@ -37,6 +37,7 @@ class SpecialName(BaseModel): def var_schema_validate_enum(cls, v): if v is None: return v + if v not in ('available', 'pending', 'sold'): raise ValueError("must be one of enum values ('available', 'pending', 'sold')") return v diff --git a/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py b/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py index c32a36c7460..9674d1b6d59 100644 --- a/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py +++ b/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py @@ -314,6 +314,10 @@ class ModelTests(unittest.TestCase): except ValueError as e: self.assertTrue(r"must validate the regular expression /^image_\d{1,3}$/i" in str(e)) + # test None with optional string (with regualr expression) + a = petstore_api.FormatTest(number=123.45, byte=bytes("string", 'utf-8'), date="2013-09-17", password="testing09876") + a.string = None # shouldn't throw an exception + a.pattern_with_digits_and_delimiter = "IMAGE_123" self.assertEqual(a.pattern_with_digits_and_delimiter, "IMAGE_123") a.pattern_with_digits_and_delimiter = "image_123"