Skip to content

Commit

Permalink
Adding property to disable hibernate validation
Browse files Browse the repository at this point in the history
Co-Authored-By: George Gastaldi <[email protected]>
  • Loading branch information
2 people authored and gsmet committed Jan 26, 2023
1 parent 1ac3b96 commit e93a720
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .../src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfigPersistenceUnit.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ public class HibernateOrmConfigPersistenceUnit {
@ConfigItem(defaultValue = "true")
public boolean secondLevelCachingEnabled;

/**
* Bean Validation configuration.
*/
@ConfigItem
public HibernateOrmConfigPersistenceValidation 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
Expand Down Expand Up @@ -496,4 +502,15 @@ public enum IdentifierQuotingStrategy {
ALL_EXCEPT_COLUMN_DEFINITIONS,
ONLY_KEYWORDS
}

@ConfigGroup
public static class HibernateOrmConfigPersistenceValidation {

/**
* Enables the Bean Validation integration.
*/
@ConfigItem(defaultValue = "true")
public boolean enabled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.hibernate.orm.validation;

import static org.hamcrest.Matchers.is;

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 {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(MyEntity.class, JPATestValidationResource.class)
.addAsResource("application-validation-disabled.properties", "application.properties"));

@Test
@Transactional
public void testValidEntity() {
String entityName = "Post method should not persist an entity having a Size constraint of 50 on the name column if validation was enabled.";
RestAssured.given().body(entityName).when().post("/validation").then()
.body(is("OK"));
}

}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e93a720

Please sign in to comment.