Skip to content

[BUG] Python: string properties with format uuid and regex: "TypeError: expected string or bytes-like object, got 'UUID'" #23572

@cgoetz-inovex

Description

@cgoetz-inovex

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions