-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
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: OKGeneration 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
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.