Skip to content

Commit

Permalink
Prevent generating "pattern" and "size" to ENUM (OpenAPITools#18658)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerod authored May 13, 2024
1 parent 2a15270 commit 4a872a8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,16 @@ public static boolean isURISchema(Schema schema) {
&& URI_FORMAT.equals(schema.getFormat());
}

public static boolean isEnumSchema(final Schema<?> schema) {
// MyEnum:
// type: string
// enum:
// - ENUM_1
// - ENUM_2
return schema.getEnum() != null
&& !schema.getEnum().isEmpty();
}

public static boolean isEmailSchema(Schema schema) {
return (schema instanceof EmailSchema) ||
// format: email
Expand Down Expand Up @@ -756,7 +766,8 @@ public static boolean shouldIgnoreBeanValidation(Schema schema) {
return ModelUtils.isByteArraySchema(schema) ||
ModelUtils.isBinarySchema(schema) ||
ModelUtils.isUUIDSchema(schema) ||
ModelUtils.isURISchema(schema);
ModelUtils.isURISchema(schema) ||
ModelUtils.isEnumSchema(schema);

}

Expand Down Expand Up @@ -1351,7 +1362,7 @@ public static Schema unaliasSchema(OpenAPI openAPI,
once(LOGGER).warn("{} is not defined", schema.get$ref());
}
return schema;
} else if (ref.getEnum() != null && !ref.getEnum().isEmpty()) {
} else if (isEnumSchema(ref)) {
// top-level enum class
return schema;
} else if (isArraySchema(ref)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,10 @@ public void ignoreBeanValidationAnnotationsTest() {
schema = new Schema<>().type("string").format("binary").pattern("^[a-z]$").maxLength(36);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "File");

schema = new Schema<>().type("string")._enum(List.of("A","B")).pattern("^[a-z]$").maxLength(36);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "String");
}

@Test
Expand All @@ -945,6 +949,10 @@ public void ignoreBeanValidationAnnotationsContainerTest() {
schema = new ArraySchema().items(new Schema<>().type("string").format("binary").pattern("^[a-z]$").maxLength(36));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<File>");

schema = new ArraySchema().items(new Schema<>().type("string")._enum(List.of("A","B")).pattern("^[a-z]$").maxLength(36));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<String>");
}

@Test
Expand Down

0 comments on commit 4a872a8

Please sign in to comment.