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 1b9d0f7d3fd8..ff65695b605a 100644 --- a/modules/openapi-generator/src/main/resources/python/model_generic.mustache +++ b/modules/openapi-generator/src/main/resources/python/model_generic.mustache @@ -38,7 +38,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} {{#vars}} {{#vendorExtensions.x-regex}} - @field_validator('{{{name}}}') + @field_validator('{{{name}}}', mode="before") def {{{name}}}_validate_regular_expression(cls, value): """Validates the regular expression""" {{^required}} @@ -53,10 +53,7 @@ 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}}): + if isinstance(value, str) and 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 {{/vendorExtensions.x-regex}} 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 9fdebaa549d2..92f3cacf7f91 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 @@ -50,55 +50,43 @@ class FormatTest(BaseModel): pattern_with_digits_and_delimiter: Optional[Annotated[str, Field(strict=True)]] = Field(default=None, description="A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01.") __properties: ClassVar[List[str]] = ["integer", "int32", "int64", "number", "float", "double", "decimal", "string", "string_with_double_quote_pattern", "byte", "binary", "date", "dateTime", "uuid", "password", "pattern_with_digits", "pattern_with_digits_and_delimiter"] - @field_validator('string') + @field_validator('string', mode="before") def string_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"[a-z]", value ,re.IGNORECASE): + if isinstance(value, str) and not re.match(r"[a-z]", value ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /[a-z]/i") return value - @field_validator('string_with_double_quote_pattern') + @field_validator('string_with_double_quote_pattern', mode="before") def string_with_double_quote_pattern_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"this is \"something\"", value): + if isinstance(value, str) and not re.match(r"this is \"something\"", value): raise ValueError(r"must validate the regular expression /this is \"something\"/") return value - @field_validator('pattern_with_digits') + @field_validator('pattern_with_digits', mode="before") def pattern_with_digits_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"^\d{10}$", value): + if isinstance(value, str) and not re.match(r"^\d{10}$", value): raise ValueError(r"must validate the regular expression /^\d{10}$/") return value - @field_validator('pattern_with_digits_and_delimiter') + @field_validator('pattern_with_digits_and_delimiter', mode="before") def pattern_with_digits_and_delimiter_validate_regular_expression(cls, value): """Validates the regular expression""" 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): + if isinstance(value, str) and 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 f396a5f10045..819cc52809b9 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 @@ -32,16 +32,13 @@ class NullableProperty(BaseModel): name: Optional[Annotated[str, Field(strict=True)]] __properties: ClassVar[List[str]] = ["id", "name"] - @field_validator('name') + @field_validator('name', mode="before") def name_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"^[A-Z].*", value): + if isinstance(value, str) and 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 9936e04769f1..31c7b3567a6c 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 @@ -31,16 +31,13 @@ class UuidWithPattern(BaseModel): id: Optional[UUID] = None __properties: ClassVar[List[str]] = ["id"] - @field_validator('id') + @field_validator('id', mode="before") def id_validate_regular_expression(cls, value): """Validates the regular expression""" 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): + if isinstance(value, str) and 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 9fdebaa549d2..92f3cacf7f91 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 @@ -50,55 +50,43 @@ class FormatTest(BaseModel): pattern_with_digits_and_delimiter: Optional[Annotated[str, Field(strict=True)]] = Field(default=None, description="A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01.") __properties: ClassVar[List[str]] = ["integer", "int32", "int64", "number", "float", "double", "decimal", "string", "string_with_double_quote_pattern", "byte", "binary", "date", "dateTime", "uuid", "password", "pattern_with_digits", "pattern_with_digits_and_delimiter"] - @field_validator('string') + @field_validator('string', mode="before") def string_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"[a-z]", value ,re.IGNORECASE): + if isinstance(value, str) and not re.match(r"[a-z]", value ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /[a-z]/i") return value - @field_validator('string_with_double_quote_pattern') + @field_validator('string_with_double_quote_pattern', mode="before") def string_with_double_quote_pattern_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"this is \"something\"", value): + if isinstance(value, str) and not re.match(r"this is \"something\"", value): raise ValueError(r"must validate the regular expression /this is \"something\"/") return value - @field_validator('pattern_with_digits') + @field_validator('pattern_with_digits', mode="before") def pattern_with_digits_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"^\d{10}$", value): + if isinstance(value, str) and not re.match(r"^\d{10}$", value): raise ValueError(r"must validate the regular expression /^\d{10}$/") return value - @field_validator('pattern_with_digits_and_delimiter') + @field_validator('pattern_with_digits_and_delimiter', mode="before") def pattern_with_digits_and_delimiter_validate_regular_expression(cls, value): """Validates the regular expression""" 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): + if isinstance(value, str) and 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 f396a5f10045..819cc52809b9 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 @@ -32,16 +32,13 @@ class NullableProperty(BaseModel): name: Optional[Annotated[str, Field(strict=True)]] __properties: ClassVar[List[str]] = ["id", "name"] - @field_validator('name') + @field_validator('name', mode="before") def name_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"^[A-Z].*", value): + if isinstance(value, str) and 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 9936e04769f1..31c7b3567a6c 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 @@ -31,16 +31,13 @@ class UuidWithPattern(BaseModel): id: Optional[UUID] = None __properties: ClassVar[List[str]] = ["id"] - @field_validator('id') + @field_validator('id', mode="before") def id_validate_regular_expression(cls, value): """Validates the regular expression""" 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): + if isinstance(value, str) and 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 a26767d39f56..1c0459f06e04 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 @@ -51,55 +51,43 @@ class FormatTest(BaseModel): additional_properties: Dict[str, Any] = {} __properties: ClassVar[List[str]] = ["integer", "int32", "int64", "number", "float", "double", "decimal", "string", "string_with_double_quote_pattern", "byte", "binary", "date", "dateTime", "uuid", "password", "pattern_with_digits", "pattern_with_digits_and_delimiter"] - @field_validator('string') + @field_validator('string', mode="before") def string_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"[a-z]", value ,re.IGNORECASE): + if isinstance(value, str) and not re.match(r"[a-z]", value ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /[a-z]/i") return value - @field_validator('string_with_double_quote_pattern') + @field_validator('string_with_double_quote_pattern', mode="before") def string_with_double_quote_pattern_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"this is \"something\"", value): + if isinstance(value, str) and not re.match(r"this is \"something\"", value): raise ValueError(r"must validate the regular expression /this is \"something\"/") return value - @field_validator('pattern_with_digits') + @field_validator('pattern_with_digits', mode="before") def pattern_with_digits_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"^\d{10}$", value): + if isinstance(value, str) and not re.match(r"^\d{10}$", value): raise ValueError(r"must validate the regular expression /^\d{10}$/") return value - @field_validator('pattern_with_digits_and_delimiter') + @field_validator('pattern_with_digits_and_delimiter', mode="before") def pattern_with_digits_and_delimiter_validate_regular_expression(cls, value): """Validates the regular expression""" 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): + if isinstance(value, str) and 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 0d838a27738e..38c9b57b0652 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 @@ -33,16 +33,13 @@ class NullableProperty(BaseModel): additional_properties: Dict[str, Any] = {} __properties: ClassVar[List[str]] = ["id", "name"] - @field_validator('name') + @field_validator('name', mode="before") def name_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"^[A-Z].*", value): + if isinstance(value, str) and 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 e86c87f16359..5dd5ec4932ed 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 @@ -32,16 +32,13 @@ class UuidWithPattern(BaseModel): additional_properties: Dict[str, Any] = {} __properties: ClassVar[List[str]] = ["id"] - @field_validator('id') + @field_validator('id', mode="before") def id_validate_regular_expression(cls, value): """Validates the regular expression""" 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): + if isinstance(value, str) and 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 a26767d39f56..1c0459f06e04 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 @@ -51,55 +51,43 @@ class FormatTest(BaseModel): additional_properties: Dict[str, Any] = {} __properties: ClassVar[List[str]] = ["integer", "int32", "int64", "number", "float", "double", "decimal", "string", "string_with_double_quote_pattern", "byte", "binary", "date", "dateTime", "uuid", "password", "pattern_with_digits", "pattern_with_digits_and_delimiter"] - @field_validator('string') + @field_validator('string', mode="before") def string_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"[a-z]", value ,re.IGNORECASE): + if isinstance(value, str) and not re.match(r"[a-z]", value ,re.IGNORECASE): raise ValueError(r"must validate the regular expression /[a-z]/i") return value - @field_validator('string_with_double_quote_pattern') + @field_validator('string_with_double_quote_pattern', mode="before") def string_with_double_quote_pattern_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"this is \"something\"", value): + if isinstance(value, str) and not re.match(r"this is \"something\"", value): raise ValueError(r"must validate the regular expression /this is \"something\"/") return value - @field_validator('pattern_with_digits') + @field_validator('pattern_with_digits', mode="before") def pattern_with_digits_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"^\d{10}$", value): + if isinstance(value, str) and not re.match(r"^\d{10}$", value): raise ValueError(r"must validate the regular expression /^\d{10}$/") return value - @field_validator('pattern_with_digits_and_delimiter') + @field_validator('pattern_with_digits_and_delimiter', mode="before") def pattern_with_digits_and_delimiter_validate_regular_expression(cls, value): """Validates the regular expression""" 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): + if isinstance(value, str) and 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 0d838a27738e..38c9b57b0652 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 @@ -33,16 +33,13 @@ class NullableProperty(BaseModel): additional_properties: Dict[str, Any] = {} __properties: ClassVar[List[str]] = ["id", "name"] - @field_validator('name') + @field_validator('name', mode="before") def name_validate_regular_expression(cls, value): """Validates the regular expression""" if value is None: return value - if not isinstance(value, str): - value = str(value) - - if not re.match(r"^[A-Z].*", value): + if isinstance(value, str) and 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 e86c87f16359..5dd5ec4932ed 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 @@ -32,16 +32,13 @@ class UuidWithPattern(BaseModel): additional_properties: Dict[str, Any] = {} __properties: ClassVar[List[str]] = ["id"] - @field_validator('id') + @field_validator('id', mode="before") def id_validate_regular_expression(cls, value): """Validates the regular expression""" 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): + if isinstance(value, str) and 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