-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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] [csharp-netcore] Double backslash problem in literal string #15101
Comments
DefaultCodegen has this
Which eventually calls this // override with any special text escaping logic
@Override
@SuppressWarnings("static-method")
public String escapeText(String input) {
if (input == null) {
return input;
}
// remove \t, \n, \r
// replace \ with \\
// replace " with \"
// outer unescape to retain the original multi-byte characters
// finally escalate characters avoiding code injection
return escapeUnsafeCharacters(
StringEscapeUtils.unescapeJava(
StringEscapeUtils.escapeJava(input)
.replace("\\/", "/"))
.replaceAll("[\\t\\n\\r]", " ")
.replace("\\", "\\\\")
.replace("\"", "\\\""));
} The regex appears to be escaped intentionally due to code injection concerns, so the pr just makes the string not be a literal. |
There's one aspect that's not fixed. I have the following:
Generated code escapes correctly the first backslash but not the second one: Compilation error in Visual Studio:
Generated with openapi-generator-cli-6.6.0-20230422.085013-33.jar using the same options as earlier. |
Bug Report Checklist
Description
When a string in constraint with a regex pattern, the generated code contains extra (redundant) backslash. Pattern is generated in C# as a literal string
@"string-here"
but the string contains double-backslash instead of single-backslash. Single backslash should be present since it's a literal string. See https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expressionsSpecifically, for the example given below, generated code is
new Regex(@"^\\d+(\\.\\d+)? (bps|Kbps|Mbps|Gbps|Tbps)$", RegexOptions.CultureInvariant);
when it should be
new Regex(@"^\d+(\.\d+)? (bps|Kbps|Mbps|Gbps|Tbps)$", RegexOptions.CultureInvariant);
[Edit]
This problem is also present in the SanitizeFilename() method of ClientUtils class.
openapi-generator version
openapi-generator-cli-6.5.0-20230329.141901-70.jar from https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/6.5.0-SNAPSHOT/
OpenAPI declaration file content or url
Generation Details
Steps to reproduce
As above
Related issues/PRs
None, at least not that's labelled with C#.
Suggest a fix
Don't add the extra backslash. Keep string as literal.
The text was updated successfully, but these errors were encountered: