OpenAPITools / OpenAPITools/openapi-generator

[BUG][CPP] cpp-oatpp-client: kebab-case enum values generate invalid function names

Open Beginner friendly
#23,227 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist

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

https://github.com/OpenAPITools/openapi-generator/commit/e6d36ff0477ecbc7cb7e380dbb64b7907aeb4bc1#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
  1. Generate code from an OpenAPI declaration containing kebab-case enum values.
  2. 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

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/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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.