OpenAPITools / OpenAPITools/openapi-generator
[BUG][CPP] cpp-oatpp-client: kebab-case enum values generate invalid function names
Nobody has claimed this yet.
- 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?
- (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](https://github.com/OpenAPITools/openapi-generator/issues/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
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:
https://github.com/OpenAPITools/openapi-generator/commit/e6d36ff0477ecbc7cb7e380dbb64b7907aeb4bc1
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:
https://github.com/OpenAPITools/openapi-generator/commit/f939e35fe3b8c7a1c61ee26caf5445c546625682
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/cpp-oatpp-client/model-header.mustache and inspect how enumVars exposes name versus value. Generate the sample using the command and input described in the issue, then compile or inspect the generated C++ for kebab-case enum values. Done means generated function identifiers are valid C++ names while returned strings preserve the original enum values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100