Skip to content

[BUG] All csharp generators incorrectly render default strings containing e.g. backslashes due to incorrect unescaping #15031

@doctorpangloss

Description

@doctorpangloss

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

Running the generator against a stablediffusion webui on Windows creates invalid escape sequences because its openapi.json includes defaults that are properly escaped, but OpenAPI incorrectly unescapes them.

 java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate  -i openapi.json  -g csharp-netcore --library unityWebRequest --skip-validate-spec

The generator needs to not unescape backslashes and use the default strings literally.

openapi.zip

openapi-generator version

a601002 (HEAD at time of writing)

OpenAPI declaration file content or url

attached

critical section:

          "data_dir": {
            "title": "Data Dir",
            "type": "string",
            "description": "base path where all user data is stored",
            "default": "C:\\Users\\bberman\\Documents\\stable-diffusion-webui"
          },
Generation Details
 java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate  -i http://127.0.0.1:7860/openapi.json  -g csharp-netcore --library unityWebRequest --skip-validate-spec
Steps to reproduce
  1. Download the attached zip.
  2. Unpack it.
  3. Run the generation command.
Related issues/PRs

(none)

Suggest a fix

{{{defaultValue}}} for strings in csharp-netcore should be verbatim strings (i.e. @"...")

diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java
index 651a015111b..571061a6587 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java
@@ -1121,7 +1121,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
             if (p.getDefault() != null) {
                 String _default = String.valueOf(p.getDefault());
                 if (p.getEnum() == null) {
-                    return "\"" + _default + "\"";
+                    return "@\"" + _default + "\"";
                 } else {
                     // convert to enum var name later in postProcessModels
                     return _default;

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