Open dsyer opened 2 years ago
I think this is just a bug in the templates for the C code. Java enums come out legal because they use the "name" not the "value" as the identifier. You could fix the example above like this:
--- a/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache
+++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache
@@ -23,7 +23,7 @@ typedef struct {{classname}}_t {{classname}}_t;
{{#allowableValues}}
// Enum {{enumName}} for {{classVarName}}
-typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e;
+typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{name}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e;
{{/allowableValues}}
char* {{classFilename}}_{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}});
@@ -43,7 +43,7 @@ char* {{classFilename}}_{{classname}}_ToString({{projectName}}_{{classVarName}}_
// Enum {{enumName}} for {{classVarName}}
{{#allowableValues}}
-typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e;
+typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{name}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e;
{{/allowableValues}}
char* {{classFilename}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}});
@@ -60,7 +60,7 @@ char* {{classFilename}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enu
// Enum {{enumName}} for {{classVarName}}
{{#allowableValues}}
-typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}
_{{enumName}}_e;
+typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{name}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_
{{enumName}}_e;
{{/allowableValues}}
char* {{classFilename}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}});
I don't know what to suggest for the best though because this will break existing code that happens not to use spaces in enum values. On the other hand it's clearly broken when there are spaces. So what would be the right thing to do?
I don't think this is limited to the C generator - here's a screenshot of python code being generated with spaces in the middle of enum value names
Spec:
Expected output is code that compiles in the target language (e.g. C). Actual output has syntax errors.
The whistespace in the YAML enum value is transferred directly to the C code which then doesn't compile. The problem is this method in
DefaultCodegen.java
:Seems like you could fix it by simply replacing spaces with underscores (for instance), but that might depend on something I don't know about. You would want the JSON sent to the server by a client to be the same as before.