Bug Report Checklist
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 + "'";
}
Bug Report Checklist
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
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.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)
Probably better (untested):