-
Notifications
You must be signed in to change notification settings - Fork 581
HV-1816 Disable Expression Language by default for custom constraint violations #1138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,780
−111
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...ion/src/test/java/org/hibernate/validator/referenceguide/chapter12/el/ElFeaturesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.hibernate.validator.referenceguide.chapter12.el; | ||
|
||
import javax.validation.Validation; | ||
import javax.validation.ValidatorFactory; | ||
|
||
import org.hibernate.validator.HibernateValidator; | ||
import org.hibernate.validator.messageinterpolation.ExpressionLanguageFeatureLevel; | ||
import org.junit.Test; | ||
|
||
public class ElFeaturesTest { | ||
|
||
@SuppressWarnings("unused") | ||
@Test | ||
public void testConstraints() throws Exception { | ||
//tag::constraints[] | ||
ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class ) | ||
.configure() | ||
.constraintExpressionLanguageFeatureLevel( ExpressionLanguageFeatureLevel.VARIABLES ) | ||
.buildValidatorFactory(); | ||
//end::constraints[] | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
@Test | ||
public void testCustomViolations() throws Exception { | ||
//tag::customViolations[] | ||
ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class ) | ||
.configure() | ||
.customViolationExpressionLanguageFeatureLevel( ExpressionLanguageFeatureLevel.VARIABLES ) | ||
.buildValidatorFactory(); | ||
//end::customViolations[] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
import java.util.Set; | ||
|
||
import javax.validation.Configuration; | ||
import javax.validation.ConstraintValidatorContext; | ||
import javax.validation.ConstraintViolation; | ||
import javax.validation.TraversableResolver; | ||
import javax.validation.constraints.Future; | ||
|
@@ -24,6 +25,7 @@ | |
import org.hibernate.validator.cfg.ConstraintMapping; | ||
import org.hibernate.validator.constraints.ParameterScriptAssert; | ||
import org.hibernate.validator.constraints.ScriptAssert; | ||
import org.hibernate.validator.messageinterpolation.ExpressionLanguageFeatureLevel; | ||
import org.hibernate.validator.spi.messageinterpolation.LocaleResolver; | ||
import org.hibernate.validator.metadata.BeanMetaDataClassNormalizer; | ||
import org.hibernate.validator.spi.nodenameprovider.PropertyNodeNameProvider; | ||
|
@@ -144,6 +146,28 @@ public interface BaseHibernateValidatorConfiguration<S extends BaseHibernateVali | |
@Incubating | ||
String LOCALE_RESOLVER_CLASSNAME = "hibernate.validator.locale_resolver"; | ||
|
||
/** | ||
* Property for configuring the Expression Language feature level for constraints, allowing to define which | ||
* Expression Language features are available for message interpolation. | ||
* <p> | ||
* This property only affects the EL feature level of "static" constraint violation messages. In particular, it | ||
* doesn't affect the default EL feature level for custom violations. Refer to | ||
* {@link #CUSTOM_VIOLATION_EXPRESSION_LANGUAGE_FEATURE_LEVEL} to configure that. | ||
* | ||
* @since 6.2 | ||
*/ | ||
@Incubating | ||
String CONSTRAINT_EXPRESSION_LANGUAGE_FEATURE_LEVEL = "hibernate.validator.constraint_expression_language_feature_level"; | ||
gsmet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Property for configuring the Expression Language feature level for custom violations, allowing to define which | ||
* Expression Language features are available for message interpolation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a link to the method that allows customizing this setting for custom violations? |
||
* | ||
* @since 6.2 | ||
*/ | ||
@Incubating | ||
String CUSTOM_VIOLATION_EXPRESSION_LANGUAGE_FEATURE_LEVEL = "hibernate.validator.custom_violation_expression_language_feature_level"; | ||
|
||
/** | ||
* <p> | ||
* Returns the {@link ResourceBundleLocator} used by the | ||
|
@@ -427,4 +451,33 @@ default S locales(Locale... locales) { | |
|
||
@Incubating | ||
S beanMetaDataClassNormalizer(BeanMetaDataClassNormalizer beanMetaDataClassNormalizer); | ||
|
||
/** | ||
* Allows setting the Expression Language feature level for message interpolation of constraint messages. | ||
* <p> | ||
* This is the feature level used for messages hardcoded inside the constraint declaration. | ||
* <p> | ||
* If you are creating custom constraint violations, Expression Language support needs to be explicitly enabled and | ||
gsmet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* use the safest feature level by default if enabled. | ||
* | ||
* @param expressionLanguageFeatureLevel the {@link ExpressionLanguageFeatureLevel} to be used | ||
* @return {@code this} following the chaining method pattern | ||
* | ||
* @since 6.2 | ||
*/ | ||
@Incubating | ||
S constraintExpressionLanguageFeatureLevel(ExpressionLanguageFeatureLevel expressionLanguageFeatureLevel); | ||
|
||
/** | ||
* Allows setting the Expression Language feature level for message interpolation of custom violation messages. | ||
* <p> | ||
* This is the feature level used for messages of custom violations created by the {@link ConstraintValidatorContext}. | ||
* | ||
* @param expressionLanguageFeatureLevel the {@link ExpressionLanguageFeatureLevel} to be used | ||
* @return {@code this} following the chaining method pattern | ||
* | ||
* @since 6.2 | ||
*/ | ||
@Incubating | ||
S customViolationExpressionLanguageFeatureLevel(ExpressionLanguageFeatureLevel expressionLanguageFeatureLevel); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...ava/org/hibernate/validator/constraintvalidation/HibernateConstraintViolationBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
|
||
package org.hibernate.validator.constraintvalidation; | ||
|
||
import javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder; | ||
|
||
import org.hibernate.validator.Incubating; | ||
import org.hibernate.validator.messageinterpolation.ExpressionLanguageFeatureLevel; | ||
|
||
public interface HibernateConstraintViolationBuilder extends ConstraintViolationBuilder { | ||
|
||
/** | ||
* Enable Expression Language with the default Expression Language feature level for the constraint violation | ||
* created by this builder if the chosen {@code MessageInterpolator} supports it. | ||
* <p> | ||
* If you enable this, you need to make sure your message template does not contain any unescaped user input (such as | ||
* the validated value): use {@code addExpressionVariable()} to inject properly escaped variables into the template. | ||
* | ||
* @since 6.2 | ||
*/ | ||
@Incubating | ||
default HibernateConstraintViolationBuilder enableExpressionLanguage() { | ||
return enableExpressionLanguage( ExpressionLanguageFeatureLevel.DEFAULT ); | ||
}; | ||
|
||
/** | ||
* Enable Expression Language for the constraint violation created by this builder if the chosen | ||
* {@code MessageInterpolator} supports it. | ||
* <p> | ||
* If you enable this, you need to make sure your message template does not contain any unescaped user input (such as | ||
* the validated value): use {@code addExpressionVariable()} to inject properly escaped variables into the template. | ||
* | ||
* @param level The Expression Language features level supported. | ||
* @since 6.2 | ||
*/ | ||
@Incubating | ||
HibernateConstraintViolationBuilder enableExpressionLanguage(ExpressionLanguageFeatureLevel level); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.