As of Hibernate Validator 6.2, EL expressions are disabled by default #690
Closed
sleberknight
started this conversation in
General
Replies: 3 comments
-
Also see #689 which has some discussion regarding |
Beta Was this translation helpful? Give feedback.
0 replies
-
For kiwi version 2.1.0:
|
Beta Was this translation helpful? Give feedback.
0 replies
-
With #722 there is no action that needs to be taken on this anymore, so we can close this discussion. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Some of the kiwi validation annotations will stop producing the expected messages as of Hibernate Validator 6.2 and beyond, because as of that version, "The Expression Language is now disabled by default for custom violations." (1)
This is a problem that we need to address by either (somehow) fixing it, or documenting how to fix it (i.e. kiwi users need to explicitly enable EL expressions at the "bean-methods" level), or changing some of our annotations so they don't use EL expressions.
At least one of our annotations (
@Range
) would need to change in a breaking way by removing theminLabel
andmaxLabel
attributes, since we use EL to determine what the message should be based on whether the label attributes are empty or not, i.e.org.kiwiproject.validation.Range.between.message=must be between ${minLabel.length() > 0 ? minLabel : min} and ${maxLabel.length() > 0 ? maxLabel : max}
Dropwizard 2.0.x (we're on 2.0.28 and will soon update to 2.0.29 which was just released on 4/5/2022) is still using 6.1.7 but version 2.1.0 upgrades to 6.2.x, at which point a bunch of our validation annotations will have broken messages.
You can customize a
Validator
instance to permit EL expressions at the constraint expression level and/or custom constraint violations. See the Javadocs inBaseHibernateValidatorConfiguration
for theconstraintExpressionLanguageFeatureLevel
andcustomViolationExpressionLanguageFeatureLevel
methods for more details.Here's an example of building a
Validator
that permits EL expressions such that the kiwi validation annotations like@Range
will have their messages interpolated as before 6.2.x:You can also configure things on the
ValidatorFactory
returned bybuildValidatorFactory()
, e.g.We need to consider whether we'd want to allow callers to easily create
Validator
instances viaKiwiValidations
and enable EL interpolation. e.g. currently you can:Maybe we'd want to overload these and add parameters when creating new instances (since the singleton instance would not change), e.g.
With the last two, we start encroaching and duplicating parts of Hibernate Validator itself, so maybe we should not bother at all. We do provide the static
setValidator
method as a way to configure in unit tests and (once) at application startup.Another thing we could do is use a callback, e.g.
While flexible, the
Consumer
approach doesn't look all that nice in the code (in my opinion anyway).Another option is to change our validator implementations to use the labels (e.g.
minLabel
) or the values (e.g.min
). It's not the most pretty solution, but it works and more importantly does not require EL to be enabled. Basically, just split each template into two separate templates, one with just the values and one with the labels. If we simply say that we'll use the labels if one is defined (e.g. if there is aminLabel
or amaxLabel
), then we'll use labels.So, for kiwi we'd take:
org.kiwiproject.validation.Range.between.message=must be between ${minLabel.length() > 0 ? minLabel : min} and ${maxLabel.length() > 0 ? maxLabel : max}
and split it into two templates:
Inside the validator implementation class, in the
initialize
method we do this:And then in the
isValid
when adding errors, we select the template based on the value of theuseLabels
field, for example:So if
useLabels
istrue
, thentemplate
will be{org.kiwiproject.validation.Range.between.message.minMaxLabels}
which does not require EL. And ifuseLabels
isfalse
, the template used will be{org.kiwiproject.validation.Range.between.message.minMaxValues}
.Doing it this way, all our validation annotations will work at the default EL feature level in 6.2 and beyond (i.e. we won't need EL).
But since we also have a bunch of very custom validation annotations in various services (not generic, very specific to the problem domain), that also use EL, we ought to provide the capability to enable EL if desired.
My proposed solution is to provide a way to customize the
Validator
instances using one or more of the ways discussed above, and to also change the existing kiwi validator implementations so that they do not require EL to be enabled. If we find there is some case where this is "too difficult" we can either document the requirements, or perhaps just change the validation error message even if that ends up providing less information.References:
Beta Was this translation helpful? Give feedback.
All reactions