OpenAPITools / OpenAPITools/openapi-generator

[BUG][Python] Python server string pattern regex improperly escaped.

Open
#8,098 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Python
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • 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

Using the python-flask generator, string regex patterns have forward slashes somehow additionally escaped, changing the meaning of the pattern.

I found this bug that seems related. Seems like a reoccurrence.

Generated line:
Actual:

if bar is not None and not re.search(r'^foo\\/bar$', bar):  # noqa: E501

Expected:

if bar is not None and not re.search(r'^foo\/bar$', bar):  # noqa: E501

With the current implementation, the input body "{"bar": "foo/bar"} passes the jsonschema validation (as it should), but then the model conversion fails with the raised ValueError, causing a 500.

openapi-generator version

Verified on 5.0.0-beta2 and 4.3.1.

OpenAPI declaration file content or url
openapi: 3.0.0

info:
    title: Foo API
    description:
        Test out deepObjects with query
    version: 0.1.0

servers:
  - url: 'http://localhost:8080/v1'

components:
  schemas:
    Foo:
      type: object
      properties:
        bar:
          type: string
          pattern: '^foo\/bar$'
      description:
        A Foo object

paths:
  /foo:
    post:
      operationId: CreateFoo
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Foo'
      responses:
        '200':
          description: OK
        '400':
          description: Bad request

Generation Details
Steps to reproduce

Save the above yaml as openapi.yaml, then run either:

openapi-generator-cli generate -i openapi.yaml -g python-flask -o out/

or

docker run --rm -t -v $(pwd):/tmp:rw openapitools/openapi-generator-cli:latest generate -i tmp/openapi.yaml -g python-flask -o tmp/out/
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/1823

Suggest a fix

I suspect something somewhere around here or the base class, since that is what the referenced bug added, but I can't quite seem to figure out what.

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 at AbstractPythonConnexionServerCodegen.java near the referenced line, then inspect the base class as suggested. Reproduce with the provided openapi.yaml and the python-flask generation command; done means the generated re.search pattern preserves the intended slash escaping so foo/bar passes model validation.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.