-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
[python] fix regex validator for date-time fields with a pattern #24105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,10 +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}}): | ||
| # 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 |
||
| if not re.match(r"{{{.}}}", s{{#vendorExtensions.x-modifiers}} ,re.{{{.}}}{{/vendorExtensions.x-modifiers}}): | ||
| raise ValueError(r"must validate the regular expression {{{vendorExtensions.x-pattern}}}") | ||
| return value | ||
| {{/vendorExtensions.x-regex}} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| openapi: 3.0.0 | ||
| info: | ||
| title: Test date-time with pattern | ||
| version: 1.0.0 | ||
| paths: {} | ||
| components: | ||
| schemas: | ||
| DatetimeWithPattern: | ||
| type: object | ||
| properties: | ||
| created_at: | ||
| type: string | ||
| format: date-time | ||
| pattern: "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?(?:Z|[+-]\\d{2}:\\d{2})$" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,10 +38,8 @@ 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): | ||
| s = value if isinstance(value, str) else value.isoformat() if hasattr(value, "isoformat") else str(value) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Non-string conversion branches in pattern validator are dead code for this strict string field Prompt for AI agents |
||
| if not re.match(r"^[A-Z].*", s): | ||
| raise ValueError(r"must validate the regular expression /^[A-Z].*/") | ||
| return value | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.