Skip to content

Commit 4a872a8

Browse files
authored
Prevent generating "pattern" and "size" to ENUM (#18658)
1 parent 2a15270 commit 4a872a8

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,16 @@ public static boolean isURISchema(Schema schema) {
722722
&& URI_FORMAT.equals(schema.getFormat());
723723
}
724724

725+
public static boolean isEnumSchema(final Schema<?> schema) {
726+
// MyEnum:
727+
// type: string
728+
// enum:
729+
// - ENUM_1
730+
// - ENUM_2
731+
return schema.getEnum() != null
732+
&& !schema.getEnum().isEmpty();
733+
}
734+
725735
public static boolean isEmailSchema(Schema schema) {
726736
return (schema instanceof EmailSchema) ||
727737
// format: email
@@ -756,7 +766,8 @@ public static boolean shouldIgnoreBeanValidation(Schema schema) {
756766
return ModelUtils.isByteArraySchema(schema) ||
757767
ModelUtils.isBinarySchema(schema) ||
758768
ModelUtils.isUUIDSchema(schema) ||
759-
ModelUtils.isURISchema(schema);
769+
ModelUtils.isURISchema(schema) ||
770+
ModelUtils.isEnumSchema(schema);
760771

761772
}
762773

@@ -1351,7 +1362,7 @@ public static Schema unaliasSchema(OpenAPI openAPI,
13511362
once(LOGGER).warn("{} is not defined", schema.get$ref());
13521363
}
13531364
return schema;
1354-
} else if (ref.getEnum() != null && !ref.getEnum().isEmpty()) {
1365+
} else if (isEnumSchema(ref)) {
13551366
// top-level enum class
13561367
return schema;
13571368
} else if (isArraySchema(ref)) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,10 @@ public void ignoreBeanValidationAnnotationsTest() {
923923
schema = new Schema<>().type("string").format("binary").pattern("^[a-z]$").maxLength(36);
924924
defaultValue = codegen.getTypeDeclaration(schema);
925925
Assert.assertEquals(defaultValue, "File");
926+
927+
schema = new Schema<>().type("string")._enum(List.of("A","B")).pattern("^[a-z]$").maxLength(36);
928+
defaultValue = codegen.getTypeDeclaration(schema);
929+
Assert.assertEquals(defaultValue, "String");
926930
}
927931

928932
@Test
@@ -945,6 +949,10 @@ public void ignoreBeanValidationAnnotationsContainerTest() {
945949
schema = new ArraySchema().items(new Schema<>().type("string").format("binary").pattern("^[a-z]$").maxLength(36));
946950
defaultValue = codegen.getTypeDeclaration(schema);
947951
Assert.assertEquals(defaultValue, "List<File>");
952+
953+
schema = new ArraySchema().items(new Schema<>().type("string")._enum(List.of("A","B")).pattern("^[a-z]$").maxLength(36));
954+
defaultValue = codegen.getTypeDeclaration(schema);
955+
Assert.assertEquals(defaultValue, "List<String>");
948956
}
949957

950958
@Test

0 commit comments

Comments
 (0)