Skip to content

fix(cpp-httplib-server): generate top-level enum models - #24795

Open
stefanwitkowskiwmb wants to merge 1 commit into
OpenAPITools:masterfrom
stefanwitkowskiwmb:bugfix/httplib-server_top_level_enum_schema
Open

fix(cpp-httplib-server): generate top-level enum models#24795
stefanwitkowskiwmb wants to merge 1 commit into
OpenAPITools:masterfrom
stefanwitkowskiwmb:bugfix/httplib-server_top_level_enum_schema

Conversation

@stefanwitkowskiwmb

@stefanwitkowskiwmb stefanwitkowskiwmb commented Aug 27, 2026

Copy link
Copy Markdown

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Fix for #23902

@ravinikam @stkrwork @etherealjoy @MartinDelille @muttleyxd @aminya


Summary by cubic

Fixes the cpp-httplib-server generator so top-level enum schemas are emitted as enum class types with JSON serialization helpers. Previously, standalone enum models were not produced.

  • Adds to_json/from_json implementations in the model header template.
  • Includes the enum header in the model source template.
  • Expands the feature-test schema and samples with a TopLevelStatus enum.
  • Updates the model test to assert top-level enums are preserved and processed.

Written for commit f186960. Summary will update on new commits.

Review in cubic

@stefanwitkowskiwmb
stefanwitkowskiwmb marked this pull request as ready for review August 27, 2026 14:58

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 issues found across 6 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/cpp-httplib-server/model-header.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/cpp-httplib-server/model-header.mustache:32">
P1: For string enum values containing `&`, `<`, or quotes, Mustache HTML-escapes `{{value}}` after the C++ escaping step. The generated `to_json` sends entities such as `&amp;`, and `from_json` then rejects the correct wire value; use triple-mustache `{{{value}}}` for the string value in both enum helpers.</violation>

<violation number="2" location="modules/openapi-generator/src/main/resources/cpp-httplib-server/model-header.mustache:40">
P2: The generated from_json in model-header.mustache compares `j == "active"`, but the committed sample TopLevelStatus.h (the expected generator output) uses `const std::string serializedValue = j.get<std::string>(); if (serializedValue == "active")`. The two implementations behave differently for non-string JSON and the sample will not match what the generator emits, so samples and generated code are out of sync. Align the template's from_json with the sample (or regenerate the sample to match the template).</violation>
</file>

<file name="modules/openapi-generator/src/test/java/org/openapitools/codegen/cpphttplibserver/CppHttplibServerCodegenModelTest.java">

<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/cpphttplibserver/CppHttplibServerCodegenModelTest.java:288">
P2: The test asserts enumVars after calling only postProcessAllModels, but enumVars is only populated by postProcessModelsEnum. DefaultCodegen.postProcessAllModels (lines 515-654) never invokes postProcessModelsEnum, and neither AbstractCppCodegen.postProcessAllModels (405-413) nor CppHttplibServerCodegen.postProcessAllModels adds it. The CppHttplibServerCodegen path that computes enumVars (DefaultCodegen.postProcessModelsEnum at 899-910, reached via AbstractCppCodegen.postProcessModels at line 401) is not triggered here, so `processedModel.allowableValues.get("enumVars")` returns null and `.size()` throws a NullPointerException, failing the test that this PR relies on to validate top-level enum handling. Run the model through the enum post-processing step before asserting, mirroring the DefaultGenerator pipeline.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

switch (value)
{
{{#allowableValues}}{{#enumVars}}
case {{vendorExtensions.modelClassName}}::{{name}}: j = {{#isString}}"{{value}}"{{/isString}}{{^isString}}{{value}}{{/isString}}; break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: For string enum values containing &, <, or quotes, Mustache HTML-escapes {{value}} after the C++ escaping step. The generated to_json sends entities such as &amp;, and from_json then rejects the correct wire value; use triple-mustache {{{value}}} for the string value in both enum helpers.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/cpp-httplib-server/model-header.mustache, line 32:

<comment>For string enum values containing `&`, `<`, or quotes, Mustache HTML-escapes `{{value}}` after the C++ escaping step. The generated `to_json` sends entities such as `&amp;`, and `from_json` then rejects the correct wire value; use triple-mustache `{{{value}}}` for the string value in both enum helpers.</comment>

<file context>
@@ -16,6 +17,37 @@
+    switch (value)
+    {
+        {{#allowableValues}}{{#enumVars}}
+        case {{vendorExtensions.modelClassName}}::{{name}}: j = {{#isString}}"{{value}}"{{/isString}}{{^isString}}{{value}}{{/isString}}; break;
+        {{/enumVars}}{{/allowableValues}}
+    }
</file context>

wrapForPostProcessAllModels("Status", model))
.get("Status").getModels().get(0).getModel();
Assert.assertNotNull(processedModel.vendorExtensions.get("modelClassName"));
Assert.assertEquals(((List<?>) processedModel.allowableValues.get("enumVars")).size(), 3);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The test asserts enumVars after calling only postProcessAllModels, but enumVars is only populated by postProcessModelsEnum. DefaultCodegen.postProcessAllModels (lines 515-654) never invokes postProcessModelsEnum, and neither AbstractCppCodegen.postProcessAllModels (405-413) nor CppHttplibServerCodegen.postProcessAllModels adds it. The CppHttplibServerCodegen path that computes enumVars (DefaultCodegen.postProcessModelsEnum at 899-910, reached via AbstractCppCodegen.postProcessModels at line 401) is not triggered here, so processedModel.allowableValues.get("enumVars") returns null and .size() throws a NullPointerException, failing the test that this PR relies on to validate top-level enum handling. Run the model through the enum post-processing step before asserting, mirroring the DefaultGenerator pipeline.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/test/java/org/openapitools/codegen/cpphttplibserver/CppHttplibServerCodegenModelTest.java, line 288:

<comment>The test asserts enumVars after calling only postProcessAllModels, but enumVars is only populated by postProcessModelsEnum. DefaultCodegen.postProcessAllModels (lines 515-654) never invokes postProcessModelsEnum, and neither AbstractCppCodegen.postProcessAllModels (405-413) nor CppHttplibServerCodegen.postProcessAllModels adds it. The CppHttplibServerCodegen path that computes enumVars (DefaultCodegen.postProcessModelsEnum at 899-910, reached via AbstractCppCodegen.postProcessModels at line 401) is not triggered here, so `processedModel.allowableValues.get("enumVars")` returns null and `.size()` throws a NullPointerException, failing the test that this PR relies on to validate top-level enum handling. Run the model through the enum post-processing step before asserting, mirroring the DefaultGenerator pipeline.</comment>

<file context>
@@ -274,15 +274,18 @@ public void enumModelTest() {
+                wrapForPostProcessAllModels("Status", model))
+                .get("Status").getModels().get(0).getModel();
+        Assert.assertNotNull(processedModel.vendorExtensions.get("modelClassName"));
+        Assert.assertEquals(((List<?>) processedModel.allowableValues.get("enumVars")).size(), 3);
     }
 
</file context>

inline void from_json(const nlohmann::json& j, {{vendorExtensions.modelClassName}}& value)
{
{{#allowableValues}}{{#enumVars}}
if (j == {{#isString}}"{{value}}"{{/isString}}{{^isString}}{{value}}{{/isString}})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The generated from_json in model-header.mustache compares j == "active", but the committed sample TopLevelStatus.h (the expected generator output) uses const std::string serializedValue = j.get<std::string>(); if (serializedValue == "active"). The two implementations behave differently for non-string JSON and the sample will not match what the generator emits, so samples and generated code are out of sync. Align the template's from_json with the sample (or regenerate the sample to match the template).

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/cpp-httplib-server/model-header.mustache, line 40:

<comment>The generated from_json in model-header.mustache compares `j == "active"`, but the committed sample TopLevelStatus.h (the expected generator output) uses `const std::string serializedValue = j.get<std::string>(); if (serializedValue == "active")`. The two implementations behave differently for non-string JSON and the sample will not match what the generator emits, so samples and generated code are out of sync. Align the template's from_json with the sample (or regenerate the sample to match the template).</comment>

<file context>
@@ -16,6 +17,37 @@
+inline void from_json(const nlohmann::json& j, {{vendorExtensions.modelClassName}}& value)
+{
+    {{#allowableValues}}{{#enumVars}}
+    if (j == {{#isString}}"{{value}}"{{/isString}}{{^isString}}{{value}}{{/isString}})
+    {
+        value = {{vendorExtensions.modelClassName}}::{{name}};
</file context>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant