[python] fix regex validator for date-time fields with a pattern#24105
Closed
timonrieger wants to merge 3 commits into
Closed
[python] fix regex validator for date-time fields with a pattern#24105timonrieger wants to merge 3 commits into
timonrieger wants to merge 3 commits into
Conversation
wing328
reviewed
Jun 23, 2026
Contributor
There was a problem hiding this comment.
2 issues found across 15 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/openapi3/client/petstore/python-httpx/petstore_api/models/nullable_property.py">
<violation number="1" location="samples/openapi3/client/petstore/python-httpx/petstore_api/models/nullable_property.py:41">
P2: Non-string conversion branches in pattern validator are dead code for this strict string field</violation>
</file>
<file name="modules/openapi-generator/src/main/resources/python/model_generic.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/python/model_generic.mustache:57">
P2: Pattern validation uses isoformat() which normalizes datetime representations (e.g., Z → +00:00), causing valid inputs matching patterns that expect the original lexical form to incorrectly fail validation.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| value = str(value) | ||
|
|
||
| if not re.match(r"^[A-Z].*", value): | ||
| s = value if isinstance(value, str) else value.isoformat() if hasattr(value, "isoformat") else str(value) |
Contributor
There was a problem hiding this comment.
P2: Non-string conversion branches in pattern validator are dead code for this strict string field
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/openapi3/client/petstore/python-httpx/petstore_api/models/nullable_property.py, line 41:
<comment>Non-string conversion branches in pattern validator are dead code for this strict string field</comment>
<file context>
@@ -38,10 +38,8 @@ def name_validate_regular_expression(cls, value):
- value = str(value)
-
- if not re.match(r"^[A-Z].*", value):
+ s = value if isinstance(value, str) else value.isoformat() if hasattr(value, "isoformat") else str(value)
+ if not re.match(r"^[A-Z].*", s):
raise ValueError(r"must validate the regular expression /^[A-Z].*/")
</file context>
|
|
||
| if not re.match(r"{{{.}}}", value{{#vendorExtensions.x-modifiers}} ,re.{{{.}}}{{/vendorExtensions.x-modifiers}}): | ||
| # match against the ISO string of parsed values (e.g. datetime) without mutating value | ||
| s = value if isinstance(value, str) else value.isoformat() if hasattr(value, "isoformat") else str(value) |
Contributor
There was a problem hiding this comment.
P2: Pattern validation uses isoformat() which normalizes datetime representations (e.g., Z → +00:00), causing valid inputs matching patterns that expect the original lexical form to incorrectly fail validation.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/python/model_generic.mustache, line 57:
<comment>Pattern validation uses isoformat() which normalizes datetime representations (e.g., Z → +00:00), causing valid inputs matching patterns that expect the original lexical form to incorrectly fail validation.</comment>
<file context>
@@ -53,10 +53,9 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
-
- if not re.match(r"{{{.}}}", value{{#vendorExtensions.x-modifiers}} ,re.{{{.}}}{{/vendorExtensions.x-modifiers}}):
+ # match against the ISO string of parsed values (e.g. datetime) without mutating value
+ s = value if isinstance(value, str) else value.isoformat() if hasattr(value, "isoformat") else str(value)
+ if not re.match(r"{{{.}}}", s{{#vendorExtensions.x-modifiers}} ,re.{{{.}}}{{/vendorExtensions.x-modifiers}}):
raise ValueError(r"must validate the regular expression {{{vendorExtensions.x-pattern}}}")
</file context>
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes the regex
@field_validatorgenerated for a property that has bothformat: date-time(ordate/time) and apattern, in thepython(pydantic v2) generator.Because the field is typed
datetime, pydantic parses the JSON string into adatetimebefore the after-mode validator runs. The previous template did:Two defects:
str(datetime)is space-separated and fails any ISO-8601 pattern, so every valid patterned date-time field raised ValidationError on deserialization.The validator now matches against a derived string and returns the original value:
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
cc @cbornet (2017/09) @tomplus (2018/10) @krjakbrjak (2023/02) @fa0311 (2023/10) @multani (2023/10)