Bug Report Checklist
Description
An API spec with a property define like:
type: string
format: uuid
pattern: "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
generates a field_validator like:
@field_validator('id')
def id_validate_regular_expression(cls, value):
"""Validates the regular expression"""
if value is None:
return value
if not re.match(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", value):
raise ValueError(r"must validate the regular expression /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/")
return value
which raises an type error when calling from_json:
foo = FooGet200Response.from_json('{ "id": "B0D54D06-11DC-4493-BF80-E5C98B0535CC" }')
# will raise TypeError: expected string or bytes-like object, got 'UUID'
openapi-generator version
tested against current master, repo with reproduction: https://github.com/cgoetz-inovex/openapi-generator-python-uuid-regex-issue
OpenAPI declaration file content or url
openapi: 3.1.0
info:
title: Foo
description: Foo
version: 0.0.1
paths:
/foo:
get:
responses:
"200":
description: foo
content:
application/json:
schema:
type: object
properties:
id:
type: string
format: uuid
pattern: "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
Generation Details
see https://github.com/cgoetz-inovex/openapi-generator-python-uuid-regex-issue/blob/main/build.sh
Steps to reproduce
see https://github.com/cgoetz-inovex/openapi-generator-python-uuid-regex-issue/blob/main/readme.md
Related issues/PRs
issue introduced here: #21740
template with validator:
|
if not re.match(r"{{{.}}}", value{{#vendorExtensions.x-modifiers}} ,re.{{{.}}}{{/vendorExtensions.x-modifiers}}): |
issue flagged here by code review bot: https://github.com/OpenAPITools/openapi-generator/pull/23040/changes
Suggest a fix
I'll contribute a PR with a fix, my current plan would be to extend the validator template with sth. like:
if not isinstance(value, str):
try:
value = str(value)
except: # add appropriate error type here
pass
# continue with existing regex validation
Not a python dev, so I first have to try it out.
Bug Report Checklist
Description
An API spec with a property define like:
generates a
field_validatorlike:which raises an type error when calling
from_json:openapi-generator version
tested against current master, repo with reproduction: https://github.com/cgoetz-inovex/openapi-generator-python-uuid-regex-issue
OpenAPI declaration file content or url
Generation Details
see https://github.com/cgoetz-inovex/openapi-generator-python-uuid-regex-issue/blob/main/build.sh
Steps to reproduce
see https://github.com/cgoetz-inovex/openapi-generator-python-uuid-regex-issue/blob/main/readme.md
Related issues/PRs
issue introduced here: #21740
template with validator:
openapi-generator/modules/openapi-generator/src/main/resources/python/model_generic.mustache
Line 56 in e1513f7
issue flagged here by code review bot: https://github.com/OpenAPITools/openapi-generator/pull/23040/changes
Suggest a fix
I'll contribute a PR with a fix, my current plan would be to extend the validator template with sth. like:
Not a python dev, so I first have to try it out.