forked from loafle/openapi-generator-original
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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"))
|
||||
|
||||
Reference in New Issue
Block a user