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] DefaultValue does not escape backslash #15541

Closed
2 of 5 tasks
HeikoStudt opened this issue May 16, 2023 · 1 comment · Fixed by #15695
Closed
2 of 5 tasks

[BUG] [Python] DefaultValue does not escape backslash #15541

HeikoStudt opened this issue May 16, 2023 · 1 comment · Fixed by #15695

Comments

@HeikoStudt
Copy link
Contributor

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

We have got an OpenAPI specification which has a defaultValue for a String of "\\" (backslash).

The python-nextgen (nowadays python) generator does not escape the backslash while generating the python model code on two occasions.

openapi-generator version

openapi-generator 6.6.0.0 (using the maven plugin)

OpenAPI declaration file content or url
    TestParam:
      type: object
      properties:
        escape:
          type: string
          minLength: 1
          maxLength: 1
          default: "\\"
Generation Details

Maven-Plugin using "python-nextgen"

Steps to reproduce

Using the above snippet in your OpenAPI spec, generating the python-client and try to import/run it will fail with either of the following errors until you provide an extra backslash (to escape the generated one).

Instead of '\' it should output '\\' for the {{defaultValue}} mustache template.

  File "/python/your_api/models/test_param.py", line 34
    escape: Optional[constr(strict=True, max_length=1, min_length=1)] = '\'

SyntaxError: unterminated string literal (detected at line 34)
  File "/python/your_api/models/test_param.py", line 81
    "escape": obj.get("escape") if obj.get("escape") is not None else '\',

SyntaxError: unterminated string literal (detected at line 81)
Related issues/PRs

#15031

Suggest a fix

The following code escapes line breaks and single quotes, but not the escape char (backslash) before doing so.

AbstractPythonCodegen.java::toDefaultValue (135)

        } else if (ModelUtils.isStringSchema(p)) {
            if (p.getDefault() != null) {
                if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find())
                    return "'''" + p.getDefault() + "'''";
                else
                    return "'" + ((String) p.getDefault()).replace("'", "\'") + "'";
            }

Probably better (untested):

        } else if (ModelUtils.isStringSchema(p)) {
            String defaultValue = (String)p.getDefault();
            if (defaultValue != null) {
                defaultValue = defaultValue.replace("\\", "\\\\") .replace("'", "\'");
                if (Pattern.compile("\r\n|\r|\n").matcher(defaultValue).find())
                    return "'''" + defaultValue + "'''";
                else
                    return "'" + defaultValue + "'";
            }
@wing328
Copy link
Member

wing328 commented May 29, 2023

can you please file a PR with the suggested fix when you've time? we can try to add some tests on top of your fix.

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

Successfully merging a pull request may close this issue.

3 participants