|
45 | 45 | import org.openapitools.codegen.languages.features.BeanValidationFeatures; |
46 | 46 | import org.openapitools.codegen.languages.features.DocumentationProviderFeatures; |
47 | 47 | import org.openapitools.codegen.meta.features.*; |
| 48 | +import org.openapitools.codegen.model.EnumVarMap; |
48 | 49 | import org.openapitools.codegen.model.ModelMap; |
49 | 50 | import org.openapitools.codegen.model.ModelsMap; |
50 | 51 | import org.openapitools.codegen.model.OperationMap; |
51 | 52 | import org.openapitools.codegen.model.OperationsMap; |
52 | 53 | import org.openapitools.codegen.templating.mustache.EscapeJavaDocLambda; |
53 | 54 | import org.openapitools.codegen.utils.CamelizeOption; |
| 55 | +import org.openapitools.codegen.utils.EnumUtils; |
54 | 56 | import org.openapitools.codegen.utils.ModelUtils; |
55 | 57 | import org.slf4j.Logger; |
56 | 58 | import org.slf4j.LoggerFactory; |
@@ -2356,6 +2358,41 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo |
2356 | 2358 | return objs; |
2357 | 2359 | } |
2358 | 2360 |
|
| 2361 | + /** |
| 2362 | + * Applies string-based enum rendering adjustments to a property that has already |
| 2363 | + * been marked with {@code x-enum-as-string} by upstream processing. |
| 2364 | + * <p> |
| 2365 | + * This method updates Java generation details by switching |
| 2366 | + * {@code datatypeWithEnum} to the plain data type, converting enum constant |
| 2367 | + * defaults to their string literal values, and adding {@code Set}/{@code HashSet} |
| 2368 | + * imports used by string-value validation logic in Java templates. |
| 2369 | + * <p> |
| 2370 | + * It does not set the {@code x-enum-as-string} vendor extension itself. |
| 2371 | + */ |
| 2372 | + protected void handleEnumAsString(CodegenProperty var, CodegenModel cm, List<Map<String, String>> imports) { |
| 2373 | + // treat enum string as just string |
| 2374 | + var.datatypeWithEnum = var.dataType; |
| 2375 | + |
| 2376 | + if (StringUtils.isNotEmpty(var.defaultValue)) { // has default value |
| 2377 | + String defaultValue = var.defaultValue.substring(var.defaultValue.lastIndexOf('.') + 1); |
| 2378 | + for (EnumVarMap enumVars : EnumUtils.getEnumVars(var.allowableValues)) { |
| 2379 | + if (defaultValue.equals(enumVars.getEnumName())) { |
| 2380 | + // update default to use the string directly instead of enum string |
| 2381 | + var.defaultValue = (String) enumVars.getEnumValue(); |
| 2382 | + } |
| 2383 | + } |
| 2384 | + } |
| 2385 | + |
| 2386 | + // add import for Set, HashSet |
| 2387 | + cm.imports.add("Set"); |
| 2388 | + Map<String, String> importsSet = new HashMap<>(); |
| 2389 | + importsSet.put("import", "java.util.Set"); |
| 2390 | + imports.add(importsSet); |
| 2391 | + Map<String, String> importsHashSet = new HashMap<>(); |
| 2392 | + importsHashSet.put("import", "java.util.HashSet"); |
| 2393 | + imports.add(importsHashSet); |
| 2394 | + } |
| 2395 | + |
2359 | 2396 | /** |
2360 | 2397 | * Normalizes a model property vendor extension across all property collections. |
2361 | 2398 | * In this context, normalization means converting a missing value, a single string, or a list value |
@@ -2931,9 +2968,9 @@ public boolean isAddNullableImports(CodegenModel cm, boolean addImports, Codegen |
2931 | 2968 | boolean isOptionalNullable = Boolean.FALSE.equals(var.required) && Boolean.TRUE.equals(var.isNullable); |
2932 | 2969 | // only add JsonNullable and related imports to optional and nullable values |
2933 | 2970 | addImports |= isOptionalNullable; |
2934 | | - var.getVendorExtensions().put("x-is-jackson-optional-nullable", isOptionalNullable); |
| 2971 | + var.getVendorExtensions().put(X_IS_JACKSON_OPTIONAL_NULLABLE, isOptionalNullable); |
2935 | 2972 | findByName(var.name, cm.readOnlyVars) |
2936 | | - .ifPresent(p -> p.getVendorExtensions().put("x-is-jackson-optional-nullable", isOptionalNullable)); |
| 2973 | + .ifPresent(p -> p.getVendorExtensions().put(X_IS_JACKSON_OPTIONAL_NULLABLE, isOptionalNullable)); |
2937 | 2974 | } |
2938 | 2975 | return addImports; |
2939 | 2976 | } |
|
0 commit comments