From e5d57e2dd359b659d51c0390d0823e632f154b20 Mon Sep 17 00:00:00 2001 From: Marcell Cruz Date: Fri, 20 Jan 2023 16:19:40 -0300 Subject: [PATCH] Added hibernate-orm property: quarkus.hibernate-orm.validation.mode that controls how the hibernate validator behaves Co-Authored-By: George Gastaldi --- .../HibernateOrmConfigPersistenceUnit.java | 21 +++++++++++ .../orm/deployment/HibernateOrmProcessor.java | 6 ++- .../JPAValidationDisabledTestCase.java | 37 +++++++++++++++++++ ...application-validation-disabled.properties | 6 +++ 4 files changed, 69 insertions(+), 1 deletion(-) mode change 100644 => 100755 extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfigPersistenceUnit.java mode change 100644 => 100755 extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java create mode 100755 extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/validation/JPAValidationDisabledTestCase.java create mode 100755 extensions/hibernate-orm/deployment/src/test/resources/application-validation-disabled.properties diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfigPersistenceUnit.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfigPersistenceUnit.java old mode 100644 new mode 100755 index 8b451d302230a0..5364128608d074 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfigPersistenceUnit.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfigPersistenceUnit.java @@ -219,6 +219,13 @@ public class HibernateOrmConfigPersistenceUnit { @ConfigItem(defaultValue = "true") public boolean secondLevelCachingEnabled; + /** + * This flag controls hibernate validation of entities + */ + + @ConfigItem + public HibernateOrmConfigPersistenceValidator validation; + /** * Defines the method for multi-tenancy (DATABASE, NONE, SCHEMA). The complete list of allowed values is available in the * https://javadoc.io/doc/org.hibernate/hibernate-core/5.6.10.Final/org/hibernate/MultiTenancyStrategy.html[Hibernate ORM @@ -496,4 +503,18 @@ public enum IdentifierQuotingStrategy { ALL_EXCEPT_COLUMN_DEFINITIONS, ONLY_KEYWORDS } + + @ConfigGroup + public static class HibernateOrmConfigPersistenceValidator { + + + /** + * This flag enable/disable hibernate validation. + */ + + @ConfigItem(defaultValue = "true") + public boolean enabled; + + } + } diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java old mode 100644 new mode 100755 index cbcf48eefe3714..cd98bf5ae3d65a --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java @@ -1183,7 +1183,11 @@ private static void producePersistenceUnitDescriptorFromConfig( // Hibernate Validator integration: we force the callback mode to have bootstrap errors reported rather than validation ignored // if there is any issue when bootstrapping Hibernate Validator. if (capabilities.isPresent(Capability.HIBERNATE_VALIDATOR)) { - descriptor.getProperties().setProperty(AvailableSettings.JPA_VALIDATION_MODE, ValidationMode.CALLBACK.name()); + if (persistenceUnitConfig.validation.enabled) { + descriptor.getProperties().setProperty(AvailableSettings.JPA_VALIDATION_MODE, ValidationMode.CALLBACK.name()); + } else { + descriptor.getProperties().setProperty(AvailableSettings.JPA_VALIDATION_MODE, ValidationMode.NONE.name()); + } } // Collect the storage engines if MySQL or MariaDB diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/validation/JPAValidationDisabledTestCase.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/validation/JPAValidationDisabledTestCase.java new file mode 100755 index 00000000000000..517a18f10e1b8e --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/validation/JPAValidationDisabledTestCase.java @@ -0,0 +1,37 @@ +package io.quarkus.hibernate.orm.validation; + +import static org.hamcrest.Matchers.is; + +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.transaction.Transactional; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.hibernate.orm.MyEntity; +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class JPAValidationDisabledTestCase { + + @Inject + EntityManager em; + + @RegisterExtension + static QuarkusUnitTest runner = new QuarkusUnitTest() + .withApplicationRoot((jar) -> jar + .addClasses(MyEntity.class, JPATestValidationResource.class) + .addAsResource("application-validation-disabled.properties", "application.properties")); + + //define an empty import.sql file + + @Test + @Transactional + public void testValidEntity() { + String entityName = MyEntity.ENTITY_NAME_TOO_LONG; + RestAssured.given().body(entityName).when().post("/validation").then() + .body(is("OK")); + } + +} diff --git a/extensions/hibernate-orm/deployment/src/test/resources/application-validation-disabled.properties b/extensions/hibernate-orm/deployment/src/test/resources/application-validation-disabled.properties new file mode 100755 index 00000000000000..bf76287adc5daf --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/test/resources/application-validation-disabled.properties @@ -0,0 +1,6 @@ +quarkus.datasource.db-kind=h2 +quarkus.datasource.jdbc.url=jdbc:h2:mem:test + +#quarkus.hibernate-orm.log.sql=true +quarkus.hibernate-orm.validation.enabled=false +quarkus.hibernate-orm.database.generation=drop-and-create