Skip to content

[BUG][PYTHON] Broken single quote escaping in python code generator #21022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 tasks done
curdbecker opened this issue Apr 2, 2025 · 1 comment
Closed
6 tasks done

Comments

@curdbecker
Copy link
Contributor

curdbecker commented Apr 2, 2025

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

Currently, the implementation of the code generator for Python can generate invalid Python code if single quotes are used within an OpenAPI specification, e.g. for default values. This stems from invalid single quote escaping within the generator.

This seems to have happened already quite a few years ago during refactoring, since the test seems might have been created as a result of this #5981.

The minimal example from there is still valid and is again used as an example below

openapi-generator version

7.13.0 @ 04169ec

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Single quote example
  version: 1.0.0
paths:
  /singleQuoteExample:
    post:
      summary: Test single quote in default and example values.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                singleQuoteInDefault1:
                  type: string
                  default: Text containing 'single' quote
                singleQuoteInDefault2:
                  type: string
                  default: "8'998'999'998'000'000"
                singleQuoteInExample1:
                  type: string
                  example: "Text containing 'single' quote"
                singleQuoteInExample2:
                  type: string
                  example: 8'998'999'998'000'000
      responses:
        '201':
          description: OK
Generation Details

docker run --rm -v $(pwd)/bla:/bla -v $(pwd):/local openapitools/openapi-generator-cli:latest generate -i /local/bla.yaml -g python -o /bla

Steps to reproduce

Run the above command.

Expected output

bla/openapi_client/models/single_quote_example_post_request.py

class SingleQuoteExamplePostRequest(BaseModel):
    """
    SingleQuoteExamplePostRequest
    """ # noqa: E501
    single_quote_in_default1: Optional[StrictStr] = Field(default='Text containing \'single\' quote', alias="singleQuoteInDefault1")
    single_quote_in_default2: Optional[StrictStr] = Field(default='8\'998\'999\'998\'000\'000', alias="singleQuoteInDefault2")
    single_quote_in_example1: Optional[StrictStr] = Field(default=None, alias="singleQuoteInExample1")
    single_quote_in_example2: Optional[StrictStr] = Field(default=None, alias="singleQuoteInExample2")
    __properties: ClassVar[List[str]] = ["singleQuoteInDefault1", "singleQuoteInDefault2", "singleQuoteInExample1", "singleQuoteInExample2"]
Actual output

bla/openapi_client/models/single_quote_example_post_request.py

class SingleQuoteExamplePostRequest(BaseModel):
    """
    SingleQuoteExamplePostRequest
    """ # noqa: E501
    single_quote_in_default1: Optional[StrictStr] = Field(default='Text containing 'single' quote', alias="singleQuoteInDefault1")
    single_quote_in_default2: Optional[StrictStr] = Field(default='8'998'999'998'000'000', alias="singleQuoteInDefault2")
    single_quote_in_example1: Optional[StrictStr] = Field(default=None, alias="singleQuoteInExample1")
    single_quote_in_example2: Optional[StrictStr] = Field(default=None, alias="singleQuoteInExample2")
    __properties: ClassVar[List[str]] = ["singleQuoteInDefault1", "singleQuoteInDefault2", "singleQuoteInExample1", "singleQuoteInExample2"]
Related issues/PRs

Issue 5981

Suggest a fix

It's kind of hard to spot, but in Java the strings "'" and "\'" (edit: in markdown too apparently 😂) are identical, so the implementation and its test were broken identically.

The simple solution is to use correct escaping in Java by double escaping the backward slash \\, so that a single backward slash is actually included in the output.

Please see here for a PR that fixes the issue as explain before.

@curdbecker
Copy link
Contributor Author

PR fixing this issue has been merged, so issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant