[BUG][C#] IllegalArgumentException if string enum value ends with backslash
Created by: kevinoid
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
(2d24d42e).
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
-
Copy example OpenAPI document into
openapi.json
. -
Run above command.
-
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