OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.43k stars 6.48k forks source link

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

Open daniellehanks opened 3 years ago

daniellehanks commented 3 years ago

Bug Report Checklist

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.

auto-labeler[bot] commented 3 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.