Skip to content

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

@HeikoStudt

Description

@HeikoStudt

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 + "'";
            }

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