#18095 | Prevent generating "pattern" and "size" to special formats of string type (#18135)

This commit is contained in:
Axident
2024-03-20 07:07:29 +01:00
committed by GitHub
parent 971c3a6059
commit e2df0d6154
3 changed files with 70 additions and 1 deletions

View File

@@ -1038,7 +1038,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
private String getStringBeanValidation(Schema<?> items) {
String validations = "";
if (ModelUtils.isByteArraySchema(items) || ModelUtils.isBinarySchema(items)) {
if (ModelUtils.shouldIgnoreBeanValidation(items)) {
return validations;
}

View File

@@ -719,6 +719,31 @@ public class ModelUtils {
&& "number".equals(schema.getFormat());
}
/**
* Returns true if the class defined by the schema cannot be used with bean validation annotations
* E.g. The UUID is defined in the schema as follows:
* <pre>{@code
* type: string,
* format: uuid,
* pattern: "^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$"
* maxLength: 36
* }</pre>
* (`pattern` and `maxLength` are required when using security tools like 42Crunch)
* If we wrap it into a container (e.g. array), the generator would create something like this:
* <pre>{@code List<@Pattern(regexp = "^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$")@Size(max = 36)UUID>}</pre>
* This causes a compilation error because the @Pattern and @Size annotations cannot be used on UUID
*
* @param schema containing at least 'type' and optionally 'format'
* @return true if the class defined by the schema cannot be used with bean validation annotations
*/
public static boolean shouldIgnoreBeanValidation(Schema schema) {
return ModelUtils.isByteArraySchema(schema) ||
ModelUtils.isBinarySchema(schema) ||
ModelUtils.isUUIDSchema(schema) ||
ModelUtils.isURISchema(schema);
}
/**
* Check to see if the schema is a model
*

View File

@@ -897,6 +897,50 @@ public class AbstractJavaCodegenTest {
Assert.assertEquals(codegen.toDefaultParameterValue(schemas.get("inlineEnumWithDefaultEmpty")), "");
}
@Test
public void ignoreBeanValidationAnnotationsTest() {
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
codegen.additionalProperties().put("useBeanValidation", true);
Schema<?> schema = new Schema<>().type("string").format("uuid").pattern("^[a-z]$").maxLength(36);
String defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "UUID");
schema = new Schema<>().type("string").format("uri").pattern("^[a-z]$").maxLength(36);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "URI");
schema = new Schema<>().type("string").format("byte").pattern("^[a-z]$").maxLength(36);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "byte[]");
schema = new Schema<>().type("string").format("binary").pattern("^[a-z]$").maxLength(36);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "File");
}
@Test
public void ignoreBeanValidationAnnotationsContainerTest() {
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
codegen.additionalProperties().put("useBeanValidation", true);
Schema<?> schema = new ArraySchema().items(new Schema<>().type("string").format("uuid").pattern("^[a-z]$").maxLength(36));
String defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<UUID>");
schema = new ArraySchema().items(new Schema<>().type("string").format("uri").pattern("^[a-z]$").maxLength(36));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<URI>");
schema = new ArraySchema().items(new Schema<>().type("string").format("byte").pattern("^[a-z]$").maxLength(36));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<byte[]>");
schema = new ArraySchema().items(new Schema<>().type("string").format("binary").pattern("^[a-z]$").maxLength(36));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<File>");
}
private static Schema<?> createObjectSchemaWithMinItems() {
return new ObjectSchema()
.addProperties("id", new IntegerSchema().format("int32"))