From 45fa4384e7d9b05c69ff58d271c3e6710216e8df Mon Sep 17 00:00:00 2001 From: vcutrona Date: Thu, 10 Oct 2024 08:57:04 +0200 Subject: [PATCH] [python] Check if the given input is a container (Array or Map) when validating enum values (#19316) * checks if input is Array or Map in validate_enum * update samples --- .../src/main/resources/python/model_generic.mustache | 11 +++++++++-- .../python-aiohttp/petstore_api/models/map_test.py | 5 +++-- .../petstore/python/petstore_api/models/map_test.py | 5 +++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/model_generic.mustache b/modules/openapi-generator/src/main/resources/python/model_generic.mustache index 768f4f75072..b18464dad9a 100644 --- a/modules/openapi-generator/src/main/resources/python/model_generic.mustache +++ b/modules/openapi-generator/src/main/resources/python/model_generic.mustache @@ -73,15 +73,22 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} {{/isNullable}} {{/required}} + {{#isContainer}} {{#isArray}} for i in value: if i not in set([{{#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}} + {{#isMap}} + for i in value.values(): + if i not in set([{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]): + raise ValueError("dict values must be one of enum values ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})") + {{/isMap}} + {{/isContainer}} + {{^isContainer}} if value not in set([{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]): raise ValueError("must be one of enum values ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})") - {{/isArray}} + {{/isContainer}} return value {{/isEnum}} {{/vars}} diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/map_test.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/map_test.py index eb0cd245609..2a056ab5532 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/map_test.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/map_test.py @@ -38,8 +38,9 @@ class MapTest(BaseModel): if value is None: return value - if value not in set(['UPPER', 'lower']): - raise ValueError("must be one of enum values ('UPPER', 'lower')") + for i in value.values(): + if i not in set(['UPPER', 'lower']): + raise ValueError("dict values must be one of enum values ('UPPER', 'lower')") return value model_config = ConfigDict( diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/map_test.py b/samples/openapi3/client/petstore/python/petstore_api/models/map_test.py index 153a14a90d3..4cefd36427f 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/models/map_test.py +++ b/samples/openapi3/client/petstore/python/petstore_api/models/map_test.py @@ -39,8 +39,9 @@ class MapTest(BaseModel): if value is None: return value - if value not in set(['UPPER', 'lower']): - raise ValueError("must be one of enum values ('UPPER', 'lower')") + for i in value.values(): + if i not in set(['UPPER', 'lower']): + raise ValueError("dict values must be one of enum values ('UPPER', 'lower')") return value model_config = ConfigDict(