From 1271dee512427cdf1aed1c4b8ba6163b48291ebb Mon Sep 17 00:00:00 2001 From: Carlo Goetz Date: Mon, 20 Apr 2026 11:49:13 +0200 Subject: [PATCH 1/2] fix(python) str non-str values before validating with regex --- .../src/main/resources/python/model_generic.mustache | 3 +++ 1 file changed, 3 insertions(+) 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 0bea1daa40bd..1041464af86a 100644 --- a/modules/openapi-generator/src/main/resources/python/model_generic.mustache +++ b/modules/openapi-generator/src/main/resources/python/model_generic.mustache @@ -53,6 +53,9 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} {{/isNullable}} {{/required}} + if not isinstance(value, str): + value = str(value) + if not re.match(r"{{{.}}}", value{{#vendorExtensions.x-modifiers}} ,re.{{{.}}}{{/vendorExtensions.x-modifiers}}): raise ValueError(r"must validate the regular expression {{{vendorExtensions.x-pattern}}}") return value From d37dd4bae8b2fd73132cf0f075aea54f998c193b Mon Sep 17 00:00:00 2001 From: Carlo Goetz Date: Mon, 20 Apr 2026 12:05:47 +0200 Subject: [PATCH 2/2] generate samples --- .../petstore_api/models/format_test.py | 12 ++++++++++++ .../petstore_api/models/nullable_property.py | 3 +++ .../petstore_api/models/uuid_with_pattern.py | 3 +++ .../python-httpx/petstore_api/models/format_test.py | 12 ++++++++++++ .../petstore_api/models/nullable_property.py | 3 +++ .../petstore_api/models/uuid_with_pattern.py | 3 +++ .../petstore_api/models/format_test.py | 12 ++++++++++++ .../petstore_api/models/nullable_property.py | 3 +++ .../petstore_api/models/uuid_with_pattern.py | 3 +++ .../python/petstore_api/models/format_test.py | 12 ++++++++++++ .../python/petstore_api/models/nullable_property.py | 3 +++ .../python/petstore_api/models/uuid_with_pattern.py | 3 +++ 12 files changed, 72 insertions(+) diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/format_test.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/format_test.py index 484f490c10f9..c1314e23cbcf 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/format_test.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/format_test.py @@ -56,6 +56,9 @@ def string_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"[a-z]", value ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /[a-z]/i") return value @@ -66,6 +69,9 @@ def string_with_double_quote_pattern_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"this is \"something\"", value): raise ValueError(r"must validate the regular expression /this is \"something\"/") return value @@ -76,6 +82,9 @@ def pattern_with_digits_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^\d{10}$", value): raise ValueError(r"must validate the regular expression /^\d{10}$/") return value @@ -86,6 +95,9 @@ def pattern_with_digits_and_delimiter_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^image_\d{1,3}$", value ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /^image_\d{1,3}$/i") return value diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/nullable_property.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/nullable_property.py index 2a412024a9c5..f396a5f10045 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/nullable_property.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/nullable_property.py @@ -38,6 +38,9 @@ def name_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^[A-Z].*", value): raise ValueError(r"must validate the regular expression /^[A-Z].*/") return value diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/uuid_with_pattern.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/uuid_with_pattern.py index 2cad18d153e2..9936e04769f1 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/uuid_with_pattern.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/uuid_with_pattern.py @@ -37,6 +37,9 @@ def id_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$", value): raise ValueError(r"must validate the regular expression /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/") return value diff --git a/samples/openapi3/client/petstore/python-httpx/petstore_api/models/format_test.py b/samples/openapi3/client/petstore/python-httpx/petstore_api/models/format_test.py index 484f490c10f9..c1314e23cbcf 100644 --- a/samples/openapi3/client/petstore/python-httpx/petstore_api/models/format_test.py +++ b/samples/openapi3/client/petstore/python-httpx/petstore_api/models/format_test.py @@ -56,6 +56,9 @@ def string_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"[a-z]", value ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /[a-z]/i") return value @@ -66,6 +69,9 @@ def string_with_double_quote_pattern_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"this is \"something\"", value): raise ValueError(r"must validate the regular expression /this is \"something\"/") return value @@ -76,6 +82,9 @@ def pattern_with_digits_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^\d{10}$", value): raise ValueError(r"must validate the regular expression /^\d{10}$/") return value @@ -86,6 +95,9 @@ def pattern_with_digits_and_delimiter_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^image_\d{1,3}$", value ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /^image_\d{1,3}$/i") return value diff --git a/samples/openapi3/client/petstore/python-httpx/petstore_api/models/nullable_property.py b/samples/openapi3/client/petstore/python-httpx/petstore_api/models/nullable_property.py index 2a412024a9c5..f396a5f10045 100644 --- a/samples/openapi3/client/petstore/python-httpx/petstore_api/models/nullable_property.py +++ b/samples/openapi3/client/petstore/python-httpx/petstore_api/models/nullable_property.py @@ -38,6 +38,9 @@ def name_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^[A-Z].*", value): raise ValueError(r"must validate the regular expression /^[A-Z].*/") return value diff --git a/samples/openapi3/client/petstore/python-httpx/petstore_api/models/uuid_with_pattern.py b/samples/openapi3/client/petstore/python-httpx/petstore_api/models/uuid_with_pattern.py index 2cad18d153e2..9936e04769f1 100644 --- a/samples/openapi3/client/petstore/python-httpx/petstore_api/models/uuid_with_pattern.py +++ b/samples/openapi3/client/petstore/python-httpx/petstore_api/models/uuid_with_pattern.py @@ -37,6 +37,9 @@ def id_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$", value): raise ValueError(r"must validate the regular expression /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/") return value diff --git a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/format_test.py b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/format_test.py index 1d124e894510..f3f6888b0c7f 100644 --- a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/format_test.py +++ b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/format_test.py @@ -57,6 +57,9 @@ def string_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"[a-z]", value ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /[a-z]/i") return value @@ -67,6 +70,9 @@ def string_with_double_quote_pattern_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"this is \"something\"", value): raise ValueError(r"must validate the regular expression /this is \"something\"/") return value @@ -77,6 +83,9 @@ def pattern_with_digits_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^\d{10}$", value): raise ValueError(r"must validate the regular expression /^\d{10}$/") return value @@ -87,6 +96,9 @@ def pattern_with_digits_and_delimiter_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^image_\d{1,3}$", value ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /^image_\d{1,3}$/i") return value diff --git a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/nullable_property.py b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/nullable_property.py index d8b2bdf98938..0d838a27738e 100644 --- a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/nullable_property.py +++ b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/nullable_property.py @@ -39,6 +39,9 @@ def name_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^[A-Z].*", value): raise ValueError(r"must validate the regular expression /^[A-Z].*/") return value diff --git a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/uuid_with_pattern.py b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/uuid_with_pattern.py index 8132a2474d21..e86c87f16359 100644 --- a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/uuid_with_pattern.py +++ b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/uuid_with_pattern.py @@ -38,6 +38,9 @@ def id_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$", value): raise ValueError(r"must validate the regular expression /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/") return value diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/format_test.py b/samples/openapi3/client/petstore/python/petstore_api/models/format_test.py index 1d124e894510..f3f6888b0c7f 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/models/format_test.py +++ b/samples/openapi3/client/petstore/python/petstore_api/models/format_test.py @@ -57,6 +57,9 @@ def string_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"[a-z]", value ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /[a-z]/i") return value @@ -67,6 +70,9 @@ def string_with_double_quote_pattern_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"this is \"something\"", value): raise ValueError(r"must validate the regular expression /this is \"something\"/") return value @@ -77,6 +83,9 @@ def pattern_with_digits_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^\d{10}$", value): raise ValueError(r"must validate the regular expression /^\d{10}$/") return value @@ -87,6 +96,9 @@ def pattern_with_digits_and_delimiter_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^image_\d{1,3}$", value ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /^image_\d{1,3}$/i") return value diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/nullable_property.py b/samples/openapi3/client/petstore/python/petstore_api/models/nullable_property.py index d8b2bdf98938..0d838a27738e 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/models/nullable_property.py +++ b/samples/openapi3/client/petstore/python/petstore_api/models/nullable_property.py @@ -39,6 +39,9 @@ def name_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^[A-Z].*", value): raise ValueError(r"must validate the regular expression /^[A-Z].*/") return value diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/uuid_with_pattern.py b/samples/openapi3/client/petstore/python/petstore_api/models/uuid_with_pattern.py index 8132a2474d21..e86c87f16359 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/models/uuid_with_pattern.py +++ b/samples/openapi3/client/petstore/python/petstore_api/models/uuid_with_pattern.py @@ -38,6 +38,9 @@ def id_validate_regular_expression(cls, value): if value is None: return value + if not isinstance(value, str): + value = str(value) + if not re.match(r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$", value): raise ValueError(r"must validate the regular expression /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/") return value