From aa066ab6fafef24713f31aeec9887161b17764eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kar=C3=A1sek?= Date: Sat, 18 Mar 2023 13:27:26 +0100 Subject: [PATCH] [python-nextgen] Fix validation of list of enums (#14809) * [python-nextgen] check enum arrady values better * [python-nextgen] re-generate exapmles for #14809 --- .../python-nextgen/model_generic.mustache | 10 ++++++++-- .../openapi_client/models/default_value.py | 6 +++--- .../python-nextgen/openapi_client/models/pet.py | 3 +-- .../python-nextgen/openapi_client/models/query.py | 6 +++--- .../petstore_api/models/enum_arrays.py | 9 ++++----- .../petstore_api/models/enum_test.py | 14 +++++--------- .../petstore_api/models/map_test.py | 3 +-- .../petstore_api/models/order.py | 3 +-- .../petstore_api/models/pet.py | 3 +-- .../petstore_api/models/special_name.py | 3 +-- .../python-nextgen-aiohttp/tests/test_model.py | 2 +- .../petstore_api/models/enum_arrays.py | 9 ++++----- .../petstore_api/models/enum_test.py | 14 +++++--------- .../python-nextgen/petstore_api/models/map_test.py | 3 +-- .../python-nextgen/petstore_api/models/order.py | 3 +-- .../python-nextgen/petstore_api/models/pet.py | 3 +-- .../petstore_api/models/special_name.py | 3 +-- .../petstore/python-nextgen/tests/test_model.py | 2 +- 18 files changed, 43 insertions(+), 56 deletions(-) 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 a503f6c667..575e5940f5 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 @@ -43,10 +43,16 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} {{^required}} if v is None: return v - {{/required}} + {{#isArray}} + for i in v: + if i not in ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}): + raise ValueError("each list item must be one of ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})") + {{/isArray}} + {{^isArray}} if v not in ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}): - raise ValueError("must validate the enum values ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})") + raise ValueError("must be one of enum values ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})") + {{/isArray}} return v {{/isEnum}} {{/vars}} 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 92ecd2aba3..b4a7d1160c 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,9 +42,9 @@ class DefaultValue(BaseModel): def array_string_enum_default_validate_enum(cls, v): if v is None: return v - - if v not in ('success', 'failure', 'unclassified'): - raise ValueError("must validate the enum values ('success', 'failure', 'unclassified')") + for i in v: + if i not in ('success', 'failure', 'unclassified'): + raise ValueError("each list item must be one of ('success', 'failure', 'unclassified')") return v class Config: 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 00eb3d7be1..d34df26fa5 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,9 +41,8 @@ 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 validate the enum values ('available', 'pending', 'sold')") + raise ValueError("must be one of enum values ('available', 'pending', 'sold')") return v class Config: 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 d52e87c6f5..531f366ed5 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,9 +35,9 @@ class Query(BaseModel): def outcomes_validate_enum(cls, v): if v is None: return v - - if v not in ('SUCCESS', 'FAILURE', 'SKIPPED'): - raise ValueError("must validate the enum values ('SUCCESS', 'FAILURE', 'SKIPPED')") + for i in v: + if i not in ('SUCCESS', 'FAILURE', 'SKIPPED'): + raise ValueError("each list item must be one of ('SUCCESS', 'FAILURE', 'SKIPPED')") return v class Config: 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 ce596ea349..a3665c53de 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,18 +34,17 @@ class EnumArrays(BaseModel): def just_symbol_validate_enum(cls, v): if v is None: return v - if v not in ('>=', '$'): - raise ValueError("must validate the enum values ('>=', '$')") + raise ValueError("must be one of enum values ('>=', '$')") return v @validator('array_enum') def array_enum_validate_enum(cls, v): if v is None: return v - - if v not in ('fish', 'crab'): - raise ValueError("must validate the enum values ('fish', 'crab')") + for i in v: + if i not in ('fish', 'crab'): + raise ValueError("each list item must be one of ('fish', 'crab')") return v class Config: 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 e610ed2c1a..5f097942ba 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,42 +45,38 @@ 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 validate the enum values ('UPPER', 'lower', '')") + raise ValueError("must be one of enum values ('UPPER', 'lower', '')") return v @validator('enum_string_required') def enum_string_required_validate_enum(cls, v): if v not in ('UPPER', 'lower', ''): - raise ValueError("must validate the enum values ('UPPER', 'lower', '')") + raise ValueError("must be one of enum values ('UPPER', 'lower', '')") return v @validator('enum_integer_default') def enum_integer_default_validate_enum(cls, v): if v is None: return v - if v not in (1, 5, 14): - raise ValueError("must validate the enum values (1, 5, 14)") + raise ValueError("must be one of enum values (1, 5, 14)") return v @validator('enum_integer') def enum_integer_validate_enum(cls, v): if v is None: return v - if v not in (1, -1): - raise ValueError("must validate the enum values (1, -1)") + raise ValueError("must be one of enum values (1, -1)") return v @validator('enum_number') def enum_number_validate_enum(cls, v): if v is None: return v - if v not in (1.1, -1.2): - raise ValueError("must validate the enum values (1.1, -1.2)") + raise ValueError("must be one of enum values (1.1, -1.2)") return v class Config: 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 14e866474e..36d415ccbf 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,9 +36,8 @@ 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 validate the enum values ('UPPER', 'lower')") + raise ValueError("must be one of enum values ('UPPER', 'lower')") return v class Config: 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 cb8cb38994..c44c3a2eea 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,9 +38,8 @@ 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 validate the enum values ('placed', 'approved', 'delivered')") + raise ValueError("must be one of enum values ('placed', 'approved', 'delivered')") return v class Config: 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 9967660b38..f8bb0d4637 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,9 +40,8 @@ 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 validate the enum values ('available', 'pending', 'sold')") + raise ValueError("must be one of enum values ('available', 'pending', 'sold')") return v class Config: 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 372f43fe05..811569b1c9 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,9 +36,8 @@ 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 validate the enum values ('available', 'pending', 'sold')") + raise ValueError("must be one of enum values ('available', 'pending', 'sold')") return v class Config: diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/tests/test_model.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/tests/test_model.py index b2954d10e0..b67782c774 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/tests/test_model.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/tests/test_model.py @@ -235,4 +235,4 @@ class ModelTests(unittest.TestCase): self.pet.status = "error" self.assertTrue(False) # this line shouldn't execute except ValueError as e: - self.assertTrue("must validate the enum values ('available', 'pending', 'sold')" in str(e)) + self.assertTrue("must be one of enum values ('available', 'pending', 'sold')" in str(e)) 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 0c096aa0fc..82b7bcfc92 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,18 +35,17 @@ class EnumArrays(BaseModel): def just_symbol_validate_enum(cls, v): if v is None: return v - if v not in ('>=', '$'): - raise ValueError("must validate the enum values ('>=', '$')") + raise ValueError("must be one of enum values ('>=', '$')") return v @validator('array_enum') def array_enum_validate_enum(cls, v): if v is None: return v - - if v not in ('fish', 'crab'): - raise ValueError("must validate the enum values ('fish', 'crab')") + for i in v: + if i not in ('fish', 'crab'): + raise ValueError("each list item must be one of ('fish', 'crab')") return v class Config: 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 8a562bd094..560b95f7bb 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,42 +46,38 @@ 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 validate the enum values ('UPPER', 'lower', '')") + raise ValueError("must be one of enum values ('UPPER', 'lower', '')") return v @validator('enum_string_required') def enum_string_required_validate_enum(cls, v): if v not in ('UPPER', 'lower', ''): - raise ValueError("must validate the enum values ('UPPER', 'lower', '')") + raise ValueError("must be one of enum values ('UPPER', 'lower', '')") return v @validator('enum_integer_default') def enum_integer_default_validate_enum(cls, v): if v is None: return v - if v not in (1, 5, 14): - raise ValueError("must validate the enum values (1, 5, 14)") + raise ValueError("must be one of enum values (1, 5, 14)") return v @validator('enum_integer') def enum_integer_validate_enum(cls, v): if v is None: return v - if v not in (1, -1): - raise ValueError("must validate the enum values (1, -1)") + raise ValueError("must be one of enum values (1, -1)") return v @validator('enum_number') def enum_number_validate_enum(cls, v): if v is None: return v - if v not in (1.1, -1.2): - raise ValueError("must validate the enum values (1.1, -1.2)") + raise ValueError("must be one of enum values (1.1, -1.2)") return v class Config: 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 9d2d840b1f..ac702eb0a7 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,9 +37,8 @@ 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 validate the enum values ('UPPER', 'lower')") + raise ValueError("must be one of enum values ('UPPER', 'lower')") return v class Config: 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 a6866f134e..cdd825329e 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,9 +39,8 @@ 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 validate the enum values ('placed', 'approved', 'delivered')") + raise ValueError("must be one of enum values ('placed', 'approved', 'delivered')") return v class Config: 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 a23db1e79b..ae46c22540 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,9 +41,8 @@ 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 validate the enum values ('available', 'pending', 'sold')") + raise ValueError("must be one of enum values ('available', 'pending', 'sold')") return v class Config: 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 58e7a59047..e0a6c30fa7 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,9 +37,8 @@ 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 validate the enum values ('available', 'pending', 'sold')") + raise ValueError("must be one of enum values ('available', 'pending', 'sold')") return v class Config: 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 e437c7b6d8..3fbcbc493a 100644 --- a/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py +++ b/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py @@ -319,7 +319,7 @@ class ModelTests(unittest.TestCase): self.pet.status = "error" self.assertTrue(False) # this line shouldn't execute except ValueError as e: - self.assertTrue("must validate the enum values ('available', 'pending', 'sold')" in str(e)) + self.assertTrue("must be one of enum values ('available', 'pending', 'sold')" in str(e)) def test_object_id(self): pet_ap = petstore_api.Pet(name="test name", photo_urls=["string"])