-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- (NB : Specifically in https://editor.swagger.io)
- Have you [tested with the latest master](https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-to-test-with-the-latest-master-of-openapi-generator) 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]([Rust][Bounty] Issues with client code generation. #6178))
Description
The current model-header.mustache template commited by @Kraust defines an enum as follows:
{{#isEnum}}
typedef {{dataType}} {{classname}};
class {{classname}}Values {
public:
{{#allowableValues}}
{{#enumVars}}
static {{classname}} {{value}}(void) { return "{{value}}"; }
{{/enumVars}}
{{/allowableValues}}
};
{{/isEnum}}This generates code like the following:
typedef oatpp::String EnumFoo;
class EnumFooValues {
public:
static EnumFoo Enum-Foo-Foo(void) { return "Enum-Foo-Foo"; }
static EnumFoo Enum-Foo-Bar(void) { return "Enum-Foo-Bar"; }
static EnumFoo Enum-Foo-Baz(void) { return "Enum-Foo-Baz"; }
};This does not compile when enum values use kebab-case, since dashes are not valid in C++ function names.
openapi-generator version
7.20.0
OpenAPI declaration file content or url
e6d36ff#diff-ac3fb2ea2397527c2656af0550da03b4f1d807fc9e8fe78b60d7e545e2baf59b
Generation Details
java -jar c:\tools\openapi-generator\openapi-generator-cli-7.20.0.jar generate \
-i samples\json\enum-validation.json \
-g cpp-oatpp-client \
-o samples\json\cpp-oatpp-client \
-t modules\openapi-generator\src\main\resources\cpp-oatpp-client
Steps to reproduce
- Generate code from an OpenAPI declaration containing kebab-case enum values.
- Inspect the generated C++ code and check whether dashes appear in the generated function names.
Related issues/PRs
Nothing found.
Suggest a fix
There is an obvious issue that can easily be fixed by replacing:
static {{classname}} {{value}}(void) { return "{{value}}"; }
with:
static {{classname}} {{name}}(void) { return "{{value}}"; }
This generates:
class EnumFooValues {
public:
static EnumFoo ENUM_FOO_FOO(void) { return "Enum-Foo-Foo"; }
static EnumFoo ENUM_FOO_BAR(void) { return "Enum-Foo-Bar"; }
static EnumFoo ENUM_FOO_BAZ(void) { return "Enum-Foo-Baz"; }
};I'm not very experienced with contributing yet, so I didn’t feel comfortable opening a proper pull request. However, this fix has been implemented and tested in my fork:
This commit also includes a minimal OpenAPI declaration file used for testing.
Enhancement suggestion
I am a simple oatpp user, so this suggestion should probably be reviewed by the core oatpp team (or at least the initial developer of this template : @Kraust ) , but I was surprised that the template uses this unusual class-with-static-values pattern, whereas oatpp provides a native enum declaration style that is easier to use.
My suggestion is to use the following template block:
{{#isEnum}}
ENUM({{classname}}, int,
{{#allowableValues}}
{{#enumVars}}
VALUE({{name}}, {{-index}}, "{{value}}"){{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
};
{{/isEnum}}which generates an oatpp-style enum declaration:
ENUM(EnumFoo, int,
VALUE(ENUM_FOO_FOO, 1, "Enum-Foo-Foo"),
VALUE(ENUM_FOO_BAR, 2, "Enum-Foo-Bar"),
VALUE(ENUM_FOO_BAZ, 3, "Enum-Foo-Baz")
);This implementation is available in this commit:
NB : If this enhancement is accepted, it most probably have impact in other templates.
NB2 : For this enhancement as for the initial fix suggestion, the changes should probably be reported in cpp-oatpp-server too.