fix(cpp-httplib-server): generate top-level enum models - #24795
fix(cpp-httplib-server): generate top-level enum models#24795stefanwitkowskiwmb wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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 `&`, 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; |
There was a problem hiding this comment.
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 &, 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 `&`, 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); |
There was a problem hiding this comment.
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}}) |
There was a problem hiding this comment.
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>
PR checklist
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.
Fix for #23902
@ravinikam @stkrwork @etherealjoy @MartinDelille @muttleyxd @aminya
Summary by cubic
Fixes the
cpp-httplib-servergenerator so top-level enum schemas are emitted asenum classtypes with JSON serialization helpers. Previously, standalone enum models were not produced.to_json/from_jsonimplementations in the model header template.TopLevelStatusenum.Written for commit f186960. Summary will update on new commits.