OpenAPITools / OpenAPITools/openapi-generator

[BUG][C#] IllegalArgumentException if string enum value ends with backslash

Open
#4,952 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: C-Sharp Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

If a string enum value ends with a backslash, openapi-generator fails to generate code for csharp due to java.lang.IllegalArgumentException.

openapi-generator version

Tested with 4.2.2 and current master (2d24d42e65).

OpenAPI declaration file content or url
Example OpenAPI document
{
  "swagger": "2.0",
  "info": {
    "title": "Problematic enum strings example",
    "version": "1.0.0"
  },
  "definitions": {
    "ErrorMessage": {
      "type": "string",
      "enum": [
        "Error: Invalid character: \\",
        "RangeError: Too many items."
      ]
    }
  },
  "paths": {
    "/count": {
      "get": {
        "operationId": "getCount",
        "responses": {
          "200": {
            "description": "Success",
            "schema": {
              "type": "integer"
            }
          },
          "default": {
            "description": "Error",
            "schema": {
              "$ref": "#/definitions/ErrorMessage"
            }
          }
        }
      }
    }
  }
}
Command line used for generation
openapi-generator generate -g csharp -i openapi.json -o generated
Steps to reproduce
  1. Copy example OpenAPI document into openapi.json.

  2. Run above command.

  3. Observe output similar to the following:

     [main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: csharp (client)
     [main] INFO  o.o.codegen.DefaultGenerator - Generator 'csharp' is considered stable.
     [main] INFO  o.o.c.l.AbstractCSharpCodegen - Environment variable CSHARP_POST_PROCESS_FILE not defined so the C# code may not be properly formatted by uncrustify (0.66 or later) or other code formatter. To define it, try `export CSHARP_POST_PROCESS_FILE="/usr/local/bin/uncrustify --no-backup" && export UNCRUSTIFY_CONFIG=/path/to/uncrustify-rules.cfg` (Linux/Mac). Note: replace /path/to with the location of uncrustify-rules.cfg
     [main] INFO  o.o.c.l.AbstractCSharpCodegen - NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
     [main] INFO  o.o.c.languages.CSharpClientCodegen - Generating code for .NET Framework v4.5
     [main] WARN  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
     [main] WARN  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
     Exception in thread "main" java.lang.RuntimeException: Could not process model 'ErrorMessage'.Please make sure that your schema is correct!
     	at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:472)
     	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:923)
     	at org.openapitools.codegen.cmd.Generate.run(Generate.java:416)
     	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:61)
     Caused by: java.lang.IllegalArgumentException: character to be escaped is missing
     	at java.base/java.util.regex.Matcher.appendExpandedReplacement(Matcher.java:1020)
     	at java.base/java.util.regex.Matcher.appendReplacement(Matcher.java:998)
     	at java.base/java.util.regex.Matcher.replaceAll(Matcher.java:1182)
     	at org.openapitools.codegen.utils.StringUtils.camelize(StringUtils.java:96)
     	at org.openapitools.codegen.utils.StringUtils.camelize(StringUtils.java:55)
     	at org.openapitools.codegen.languages.CSharpClientCodegen.toEnumVarName(CSharpClientCodegen.java:757)
     	at org.openapitools.codegen.DefaultCodegen.postProcessModelsEnum(DefaultCodegen.java:438)
     	at org.openapitools.codegen.languages.AbstractCSharpCodegen.postProcessModels(AbstractCSharpCodegen.java:405)
     	at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1199)
     	at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:467)
     	... 3 more
    
Related issues/PRs

None that I could find.

Suggest a fix

StringUtils.camelize could use Matcher.quoteReplacement on the replacement string.

Thanks for considering,
Kevin

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/StringUtils.java and the StringUtils.camelize stack-trace location, then reproduce with the supplied OpenAPI document and csharp generation command. Check CSharpClientCodegen.toEnumVarName as the caller; done means generation handles an enum value ending in a backslash without the reported IllegalArgumentException.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.