Skip to content

[BUG][Python] Single quotes in default/example string cause invalid syntax error in generated test code #5981

@fullcircle23

Description

@fullcircle23

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

Single quotes in default or example values of type string (e.g. 0xA2D2’C3B0’A2D2’C3B0) cause invalid syntax error in generated test code for Python. The error is a delimiter collision when a value containing single quote is delimited with the same quote. So in a string assignment in generated test code, the above value becomes ’0xA2D2’C3B0’A2D2’C3B0’ instead of “0xA2D2’C3B0’A2D2’C3B0”.

openapi-generator version

4.2.3, master

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
Command line used for generation

java -jar openapi-generator-cli.jar generate -g python -i example.yaml -o example-client

Steps to reproduce

Run the above command.

Expected output:

def make_instance(self, include_optional):
    if include_optional :
        return InlineObject(
            single_quote_in_default1 = "Text containing 'single' quote", 
            single_quote_in_default2 = "8'998'999'998'000'000", 
            single_quote_in_example1 = "Text containing 'single' quote", 
            single_quote_in_example2 = "8'998'999'998'000'000"
        )
    else :
        return InlineObject(
    )

Actual output:

def make_instance(self, include_optional):
    if include_optional :
        return InlineObject(
            single_quote_in_default1 = 'Text containing 'single' quote', 
            single_quote_in_default2 = '8'998'999'998'000'000', 
            single_quote_in_example1 = 'Text containing 'single' quote', 
            single_quote_in_example2 = '8'998'999'998'000'000'
        )
    else :
        return InlineObject(
    )
Related issues/PRs

#2808 #2809

Suggest a fix
  • Wrap using double quotes to avoid the need to escape any embedded single quotes. The changes are in these 2 lines in PythonClientCodegen.java.

Line 683 (method toDefaultValue):
Current:
return "'" + ((String) p.getDefault()).replaceAll("'", "\'") + "'";
Change to:
return "\"" + p.getDefault() + "\"";

Line 727 (method toExampleValueRecursive):
Current:
example = "'" + example + "'";
Change to:
example = "\"" + example + "\"";

  • Add/update test cases in PythonClientCodegenTest.java.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions