OpenAPITools / OpenAPITools/openapi-generator

[Python FastAPI] Pattern validators in

Open
#20,559 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

The OpenAPI Specification goes as far as suggesting that the Regular Expression Dialect underlying the keyword "pattern" should be Ecma-262 Edition 5.1 regular expression, but does not constrain how the regular expression should be evaluated, at least I could not find any constraint to that effect.

Generating FastAPI Python source code from a YAML file defining a model attribute whose pattern is "[A-Za-z0-9-_]$" currently results in the following condition:

if not re.match(r"{{{.}}}", value{{#vendorExtensions.x-modifiers}} ,re.{{{.}}}{{/vendorExtensions.x-modifiers}}):
(as per the template model_generic.mustache)

ISSUE: The use of re.match forces matching the pattern from the beginning of the string, causing the matching of "AAA_BBB_1" to fail, even though the last character is in the range set by "[A-Za-z0-9-_]".

I'm inclined to look at this as at least something to should be improved, so developers aren't forced to write regular expression patterns that always consider matching since the beginning of the value.

Besides, given the close relationship to JSON Schema, one could argue that the described issue violates the last paragraph of section 6.4 of JSON Schema Draft 2020-12 specification, by assuming the regular expression pattern to be anchored to the beginning of the tested string.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the model_generic.mustache template and reproduce generation from a YAML model attribute using the pattern "[A-Za-z0-9-_]$". Compare the generated FastAPI validation with the OpenAPI and JSON Schema pattern semantics; done means the generated validator does not implicitly anchor matching at the beginning of the value.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, openapi, python
Domain
api, backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.