-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
fix(cpp-httplib-server): generate top-level enum models #24795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f186960
c877a32
67be047
ffbc0fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,13 +35,17 @@ public class CppHttplibServerCodegenModelTest { | |
| * Wraps a single model the way {@link org.openapitools.codegen.DefaultGenerator} | ||
| * does before calling {@code postProcessAllModels}, so tests can exercise the full | ||
| * enum vendor-extension pipeline (identifier + original-value derivation), not just | ||
| * the intermediate state produced by {@code fromModel}. | ||
| * the intermediate state produced by {@code fromModel}. This mirrors | ||
| * {@code DefaultGenerator}'s {@code processModels}, which runs {@code postProcessModels} | ||
| * (and, for C++, {@code postProcessModelsEnum} with it) before {@code postProcessAllModels} | ||
| * is ever invoked. | ||
| */ | ||
| private Map<String, ModelsMap> wrapForPostProcessAllModels(String name, CodegenModel model) { | ||
| private Map<String, ModelsMap> wrapForPostProcessAllModels(CppHttplibServerCodegen codegen, String name, CodegenModel model) { | ||
| final ModelMap modelMap = new ModelMap(); | ||
| modelMap.setModel(model); | ||
| final ModelsMap modelsMap = new ModelsMap(); | ||
| modelsMap.setModels(Collections.singletonList(modelMap)); | ||
| codegen.postProcessModels(modelsMap); | ||
| final HashMap<String, ModelsMap> allModels = new HashMap<>(); | ||
| allModels.put(name, modelsMap); | ||
| return allModels; | ||
|
|
@@ -173,7 +177,7 @@ public void arrayOfEnumsDeclaresValidUpperCaseIdentifiersTest() { | |
|
|
||
| final CodegenModel model = codegen.fromModel("ModelWithColorArray", schema); | ||
| final CodegenModel processedModel = codegen.postProcessAllModels( | ||
| wrapForPostProcessAllModels("ModelWithColorArray", model)) | ||
| wrapForPostProcessAllModels(codegen, "ModelWithColorArray", model)) | ||
| .get("ModelWithColorArray").getModels().get(0).getModel(); | ||
|
|
||
| CodegenProperty arrayProp = processedModel.vars.get(0); | ||
|
|
@@ -221,7 +225,7 @@ public void numericEnumPropertyTest() { | |
| // postProcessAllModels, since that's the single place both the identifier and the | ||
| // original spec value are derived together (see enumSerializationUsesOriginalSpecValueTest). | ||
| final CodegenModel processedModel = codegen.postProcessAllModels( | ||
| wrapForPostProcessAllModels("UserStatusModel", model)) | ||
| wrapForPostProcessAllModels(codegen, "UserStatusModel", model)) | ||
| .get("UserStatusModel").getModels().get(0).getModel(); | ||
| CodegenProperty statusProp = processedModel.vars.get(0); | ||
| Assert.assertTrue((boolean) statusProp.vendorExtensions.getOrDefault("isEnum", false)); | ||
|
|
@@ -244,7 +248,7 @@ public void enumSerializationUsesOriginalSpecValueTest() { | |
|
|
||
| final CodegenModel model = codegen.fromModel("Pet", schema); | ||
| final CodegenModel processedModel = codegen.postProcessAllModels( | ||
| wrapForPostProcessAllModels("Pet", model)) | ||
| wrapForPostProcessAllModels(codegen, "Pet", model)) | ||
| .get("Pet").getModels().get(0).getModel(); | ||
|
|
||
| CodegenProperty statusProp = processedModel.vars.get(0); | ||
|
|
@@ -274,15 +278,20 @@ public void enumModelTest() { | |
|
|
||
| final CodegenModel model = codegen.fromModel("Status", enumSchema); | ||
|
|
||
| // Note: The C++ httplib server generator may not process enum-only models | ||
| // in the same way as regular object models. The model might be null or empty. | ||
| if (model != null) { | ||
| Assert.assertEquals(model.name, "Status"); | ||
| // Check if it's marked as an enum in vendor extensions | ||
| if (model.vendorExtensions.containsKey("x-is-enum")) { | ||
| Assert.assertEquals(model.vendorExtensions.get("x-is-enum"), true); | ||
| } | ||
| } | ||
| Assert.assertNotNull(model); | ||
| Assert.assertEquals(model.name, "Status"); | ||
| Assert.assertTrue(model.isEnum, "top-level enum schemas must remain enum models"); | ||
| Assert.assertEquals(model.vendorExtensions.get("isStringEnum"), true, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: The new assertion only verifies the internal Prompt for AI agents |
||
| "string-backed top-level enums must serialize their values as JSON strings"); | ||
| Assert.assertNotNull(model.allowableValues); | ||
| Assert.assertEquals(model.allowableValues.get("values"), | ||
| java.util.Arrays.asList("ACTIVE", "INACTIVE", "PENDING")); | ||
|
|
||
| final CodegenModel processedModel = codegen.postProcessAllModels( | ||
| wrapForPostProcessAllModels(codegen, "Status", model)) | ||
| .get("Status").getModels().get(0).getModel(); | ||
| Assert.assertNotNull(processedModel.vendorExtensions.get("modelClassName")); | ||
| Assert.assertEquals(((List<?>) processedModel.allowableValues.get("enumVars")).size(), 3); | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Test(description = "convert model with nullable property") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
|
|
||
| /** | ||
| * This file is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
| * https://openapi-generator.tech | ||
| * Do not edit the class manually. | ||
| */ | ||
| #include "TopLevelStatus.h" | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * This file is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
| * https://openapi-generator.tech | ||
| * Do not edit the class manually. | ||
| */ | ||
|
|
||
| #pragma once | ||
| // System headers | ||
| #include <nlohmann/json.hpp> | ||
|
|
||
|
|
||
|
|
||
| namespace models { | ||
|
|
||
| enum class TopLevelStatus { | ||
|
|
||
| ACTIVE, | ||
|
|
||
| INACTIVE, | ||
|
|
||
| PENDING | ||
|
|
||
| }; | ||
|
|
||
| inline void to_json(nlohmann::json& j, const TopLevelStatus& value) | ||
| { | ||
| switch (value) | ||
| { | ||
|
|
||
| case TopLevelStatus::ACTIVE: j = "active"; break; | ||
|
|
||
| case TopLevelStatus::INACTIVE: j = "inactive"; break; | ||
|
|
||
| case TopLevelStatus::PENDING: j = "pending"; break; | ||
|
|
||
| } | ||
| } | ||
|
|
||
| inline void from_json(const nlohmann::json& j, TopLevelStatus& value) | ||
| { | ||
|
|
||
| if (j == "active") | ||
| { | ||
| value = TopLevelStatus::ACTIVE; | ||
| return; | ||
| } | ||
|
|
||
| if (j == "inactive") | ||
| { | ||
| value = TopLevelStatus::INACTIVE; | ||
| return; | ||
| } | ||
|
|
||
| if (j == "pending") | ||
| { | ||
| value = TopLevelStatus::PENDING; | ||
| return; | ||
| } | ||
|
|
||
| throw nlohmann::json::type_error::create(302, "Invalid value for TopLevelStatus", &j); | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: When a top-level enum omits
type: stringbut contains string values,isStringEnumcan be false and the generator emitsj = availableinstead ofj = "available". Determine stringness from the enum values as a fallback, or preserve the per-enum string flag for untyped enums.Prompt for AI agents