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] [csharp-netcore] Double backslash problem in literal string #15101

Closed
5 of 6 tasks
arvindpdmn opened this issue Apr 1, 2023 · 2 comments · Fixed by #15107
Closed
5 of 6 tasks

[BUG] [csharp-netcore] Double backslash problem in literal string #15101

arvindpdmn opened this issue Apr 1, 2023 · 2 comments · Fixed by #15107

Comments

@arvindpdmn
Copy link

arvindpdmn commented Apr 1, 2023

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

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-expressions

Specifically, 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
openapi: 3.0.0
info:
  version: '1.0.0'
  title: 'Regex issue'
paths: {}
components:
  schemas:
    Capability:
      properties:
        dlRate:
          $ref: '#/components/schemas/BitRate'
        ulRate:
          $ref: '#/components/schemas/BitRate'
    BitRate:
      type: string
      pattern: '^\d+(\.\d+)? (bps|Kbps|Mbps|Gbps|Tbps)$'
Generation Details
java -jar lib/openapi-generator-cli.jar generate -i RegExp.yaml -g csharp-netcore -o RegExp --additional-properties=targetFramework=net7.0 --additional-properties=packageName=Com.Example --skip-validate-spec --openapi-normalizer SIMPLIFY_BOOLEAN_ENUM=true,SIMPLIFY_ANYOF_STRING_AND_ENUM_STRING=true,REMOVE_ANYOF_ONEOF_AND_KEEP_PROPERTIES_ONLY=true,ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE=true,SIMPLIFY_ONEOF_ANYOF=true
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.

@devhl-labs
Copy link
Contributor

DefaultCodegen has this

property.pattern = toRegularExpression(p.getPattern());

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.

@arvindpdmn
Copy link
Author

There's one aspect that's not fixed. I have the following:

    Ipv4AddrMask:
      type: string
      pattern: '^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$'
      example: '198.51.0.0/16'

Generated code escapes correctly the first backslash but not the second one:
Regex regexIpv4Mask = new Regex("^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$", RegexOptions.CultureInvariant);

Compilation error in Visual Studio:

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS1009	Unrecognized escape sequence ...

Generated with openapi-generator-cli-6.6.0-20230422.085013-33.jar using the same options as earlier.

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.

2 participants