Skip to content

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

@Miatec

Description

@Miatec

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

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

e6d36ff

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:

f939e35

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions