Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3907,9 +3907,7 @@ protected String getCommonSchemaType(List<Schema> schemas) {
}
if (ModelUtils.isEnumSchema(first)) {
// inline enum, use the least common denominator
String simpleType = typeMapping.get("enum");

return simpleType != null? simpleType: typeMapping.get("object");
return getCommonEnumType();
}
return typeMapping.get(getPrimitiveType(first));
} catch (Exception e) {
Expand Down Expand Up @@ -3947,10 +3945,7 @@ protected String getCommonTypeMapping(List<Schema> schemas) {
boolean allEnums = schemas.stream().allMatch(ModelUtils::isEnumSchema);
if (allEnums) {
// non matching enums. Use enum if if the langage can map it.
if (typeMapping.containsKey("enum")) {
simpleType = "enum";
}
return typeMapping.get(simpleType);
return getCommonEnumType();
}
if (schemas.stream().noneMatch(ModelUtils::isEnumSchema)) {
Set<String> types = schemas.stream().map(this::getPrimitiveType).collect(Collectors.toSet());
Expand All @@ -3965,6 +3960,18 @@ protected String getCommonTypeMapping(List<Schema> schemas) {
return typeMapping.get(simpleType);
}

/**
* Get the common type name for enum schemas when multiple or inline enums cannot be unified to a specific enum model.
*
* @return the common enum type name (e.g. "Enum" for Java, or mapped "object" if unsupported)
*/
protected String getCommonEnumType() {
if (typeMapping.containsKey("enum")) {
return typeMapping.get("enum");
}
return typeMapping.get("object");
}

/**
* Handle the model for the 'additionalProperties' keyword in the OAS schema.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ public AbstractJavaCodegen() {
typeMapping.put("file", "File");
typeMapping.put("AnyType", "Object");
typeMapping.put("null", "Object");
typeMapping.put("enum", "Enum");

importMapping.put("BigDecimal", "java.math.BigDecimal");
importMapping.put("UUID", "java.util.UUID");
Expand Down Expand Up @@ -3080,4 +3079,12 @@ public void keepAnnotation(String keep) {
this.keep = keep;
}
}

@Override
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
protected String getCommonEnumType() {
if (typeMapping.containsKey("enum")) {
return typeMapping.get("enum");
}
return "Enum";
}
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -2804,6 +2804,45 @@ public void testEnumDiscriminatorDefaultValueIsNotString() {
}
}

@Test
public void testEnumFormatStringSchema_issue24950() {
final OpenAPI openAPI = TestUtils.parseContent(
"openapi: 3.0.3\n" +
"info:\n" +
" title: Enum format test\n" +
" version: 1.0.0\n" +
"paths: {}\n" +
"components:\n" +
" schemas:\n" +
" Ruleset:\n" +
" type: object\n" +
" properties:\n" +
" bypassMode:\n" +
" type: string\n" +
" format: enum\n" +
" enum:\n" +
" - RULESET_BYPASS_MODE_UNSPECIFIED\n" +
" - RULESET_BYPASS_MODE_ALLOWED\n" +
" - RULESET_BYPASS_MODE_DISABLED\n" +
" plainEnumFormat:\n" +
" type: string\n" +
" format: enum\n"
);
final JavaClientCodegen codegen = new JavaClientCodegen();
codegen.setOpenAPI(openAPI);
codegen.setOutputDir(newTempFolder().toString());

Map<String, File> files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen))
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));

File rulesetFile = files.get("Ruleset.java");
assertNotNull(rulesetFile);
JavaFileAssert.assertThat(rulesetFile)
.hasNoImports("org.openapitools.client.model.Enum")
.fileContains("public enum BypassModeEnum {", "private String value;", "BypassModeEnum(String value)", "public String getValue()", "public static BypassModeEnum fromValue(String value)", "private String plainEnumFormat;")
.fileDoesNotContain("private Enum value;", "Enum.valueOf(", "private Enum plainEnumFormat;");
}

@Test
public void testRestTemplateHandleURIEnum() {
String[] expectedInnerEnumLines = new String[]{
Expand Down
Loading