Skip to content
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

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

Closed
5 of 6 tasks
fullcircle23 opened this issue Apr 20, 2020 · 1 comment · Fixed by #6451
Closed
5 of 6 tasks

Comments

@fullcircle23
Copy link
Contributor

fullcircle23 commented Apr 20, 2020

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.
@auto-labeler
Copy link

auto-labeler bot commented Apr 20, 2020

👍 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.

fullcircle23 added a commit to fullcircle23/openapi-generator that referenced this issue May 27, 2020
fullcircle23 added a commit to fullcircle23/openapi-generator that referenced this issue May 27, 2020
fullcircle23 added a commit to fullcircle23/openapi-generator that referenced this issue May 27, 2020
fullcircle23 added a commit to fullcircle23/openapi-generator that referenced this issue May 28, 2020
fullcircle23 added a commit to fullcircle23/openapi-generator that referenced this issue May 28, 2020
wing328 pushed a commit that referenced this issue Jun 1, 2020
* [python][client] Fix delimiter collision (#5981)

* [python][client] Fix delimiter collision (#5981) update samples

* [python][client] Fix delimiter collision (#5981) update samples

* [python][client] Fix convert to enum var name (#5981)

* [python][client] Fix convert to enum var name (#5981) update samples
wing328 added a commit that referenced this issue Jun 1, 2020
wing328 added a commit that referenced this issue Jun 1, 2020
* Revert "Minor enhancement to Python client generator's code format (#6510)"

This reverts commit 3ddc783.

* Revert "[Python][Client] Fix delimiter collision issue #5981 (#6451)"

This reverts commit 6783b90.
jimschubert added a commit that referenced this issue Jun 2, 2020
* master:
  Update Generate.java (#6515)
  Undo PR #6451 (#6514)
  Minor enhancement to Python client generator's code format (#6510)
  [python-experimental] Quicken package loading (#6437)
  [Python][Client] Fix delimiter collision issue #5981 (#6451)
  [Java][Jersey2] add petstore integration tests (#6508)
  UE4 client generator fixes (#6438)
  Fix docs typos (#6478)
  [php-laravel] Show required PHP version in docs (#6502)
  [php-lumen] Show required PHP version in docs (#6501)
  [Java][Jersey2] Fix typo and script, Log enhancements, HTTP signature, deserialization (#6476)
  Remove deprecations 5.0 (#6060)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant