Skip to content

OpenAPI: pattern for Regex Constraint should use htmlPattern #4022

Description

@stchr

API Platform version(s) affected: 2.6.1

Description
Given a pattern with regex delimiters for \Symfony\Component\Validator\Constraints\Regex, it cannot be used as http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute

How to reproduce
with '/^([-a-zA-Z0-9_])+$/' is used in OpenAPI documentation for property:

...
      properties:
        property:
          pattern: '/^([-a-zA-Z0-9_])+$/
          description: '...'
          type: string
...

Correct would be (without delimiters):

...
      properties:
        property:
          pattern: '([-a-zA-Z0-9_])+'
          description: '...'
          type: string
...

Possible Solution
In \ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRegexRestriction::create(), instead of $constraint->pattern use $constraint->getHtmlPattern()

See https://github.com/symfony/validator/blob/367afc5864bfdb07508668dc0dfc06e56b6b89d5/Constraints/Regex.php#L99

Additional Context
https://swagger.io/docs/specification/data-models/data-types/#pattern

Metadata

Metadata

Assignees

No one assigned

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions