Skip to content

Commit 4d5e327

Browse files
sjaakdgsmet
sjaakd
authored andcommitted
HV-1657 Make the “propertyPath” available via the HibernateMessageInterpolatorContext
1 parent 8e82fda commit 4d5e327

File tree

6 files changed

+255
-39
lines changed

6 files changed

+255
-39
lines changed

engine/src/main/java/org/hibernate/validator/internal/engine/MessageInterpolatorContext.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.lang.invoke.MethodHandles;
1212
import java.util.Map;
1313

14+
import javax.validation.Path;
1415
import javax.validation.metadata.ConstraintDescriptor;
1516

1617
import org.hibernate.validator.internal.util.logging.Log;
@@ -33,19 +34,22 @@ public class MessageInterpolatorContext implements HibernateMessageInterpolatorC
3334
private final ConstraintDescriptor<?> constraintDescriptor;
3435
private final Object validatedValue;
3536
private final Class<?> rootBeanType;
37+
private final Path propertyPath;
3638
@Immutable
3739
private final Map<String, Object> messageParameters;
3840
@Immutable
3941
private final Map<String, Object> expressionVariables;
4042

4143
public MessageInterpolatorContext(ConstraintDescriptor<?> constraintDescriptor,
42-
Object validatedValue,
43-
Class<?> rootBeanType,
44-
Map<String, Object> messageParameters,
45-
Map<String, Object> expressionVariables) {
44+
Object validatedValue,
45+
Class<?> rootBeanType,
46+
Path propertyPath,
47+
Map<String, Object> messageParameters,
48+
Map<String, Object> expressionVariables) {
4649
this.constraintDescriptor = constraintDescriptor;
4750
this.validatedValue = validatedValue;
4851
this.rootBeanType = rootBeanType;
52+
this.propertyPath = propertyPath;
4953
this.messageParameters = toImmutableMap( messageParameters );
5054
this.expressionVariables = toImmutableMap( expressionVariables );
5155
}
@@ -75,6 +79,11 @@ public Map<String, Object> getExpressionVariables() {
7579
return expressionVariables;
7680
}
7781

82+
@Override
83+
public Path getPropertyPath() {
84+
return propertyPath;
85+
}
86+
7887
@Override
7988
public <T> T unwrap(Class<T> type) {
8089
//allow unwrapping into public super types
@@ -122,6 +131,8 @@ public String toString() {
122131
sb.append( "MessageInterpolatorContext" );
123132
sb.append( "{constraintDescriptor=" ).append( constraintDescriptor );
124133
sb.append( ", validatedValue=" ).append( validatedValue );
134+
sb.append( ", rootBeanType=" ).append( rootBeanType.getName() );
135+
sb.append( ", propertyPath=" ).append( propertyPath );
125136
sb.append( ", messageParameters=" ).append( messageParameters );
126137
sb.append( ", expressionVariables=" ).append( expressionVariables );
127138
sb.append( '}' );

engine/src/main/java/org/hibernate/validator/internal/engine/validationcontext/AbstractValidationContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ public void addConstraintFailure(
228228
messageTemplate,
229229
valueContext.getCurrentValidatedValue(),
230230
descriptor,
231+
constraintViolationCreationContext.getPath(),
231232
constraintViolationCreationContext.getMessageParameters(),
232233
constraintViolationCreationContext.getExpressionVariables()
233234
);
@@ -293,12 +294,14 @@ private String interpolate(
293294
String messageTemplate,
294295
Object validatedValue,
295296
ConstraintDescriptor<?> descriptor,
297+
Path path,
296298
Map<String, Object> messageParameters,
297299
Map<String, Object> expressionVariables) {
298300
MessageInterpolatorContext context = new MessageInterpolatorContext(
299301
descriptor,
300302
validatedValue,
301303
getRootBeanClass(),
304+
path,
302305
messageParameters,
303306
expressionVariables
304307
);

engine/src/main/java/org/hibernate/validator/messageinterpolation/HibernateMessageInterpolatorContext.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010

1111
import javax.validation.MessageInterpolator;
12+
import javax.validation.Path;
1213

1314
/**
1415
* Extension to {@code MessageInterpolator.Context} which provides functionality
@@ -40,4 +41,11 @@ public interface HibernateMessageInterpolatorContext extends MessageInterpolator
4041
* @since 5.4.1
4142
*/
4243
Map<String, Object> getExpressionVariables();
44+
45+
/**
46+
* @return the path to the validated constraint starting from the root bean
47+
*
48+
* @since 6.1
49+
*/
50+
Path getPropertyPath();
4351
}

engine/src/test/java/org/hibernate/validator/test/internal/engine/messageinterpolation/ExpressionLanguageMessageInterpolationTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public void testExpressionLanguageGraphNavigation() {
6767
notNullDescriptor,
6868
user,
6969
null,
70+
null,
7071
Collections.<String, Object>emptyMap(),
71-
Collections.<String, Object>emptyMap()
72-
);
72+
Collections.<String, Object>emptyMap() );
7373

7474
String expected = "18";
7575
String actual = interpolatorUnderTest.interpolate( "${validatedValue.age}", context );
@@ -82,9 +82,9 @@ public void testUnknownPropertyInExpressionLanguageGraphNavigation() {
8282
notNullDescriptor,
8383
new User(),
8484
null,
85+
null,
8586
Collections.<String, Object>emptyMap(),
86-
Collections.<String, Object>emptyMap()
87-
);
87+
Collections.<String, Object>emptyMap() );
8888

8989
String expected = "${validatedValue.foo}";
9090
String actual = interpolatorUnderTest.interpolate( "${validatedValue.foo}", context );
@@ -172,9 +172,9 @@ public void testLocaleBasedFormatting() {
172172
notNullDescriptor,
173173
42.00000d,
174174
null,
175+
null,
175176
Collections.<String, Object>emptyMap(),
176-
Collections.<String, Object>emptyMap()
177-
);
177+
Collections.<String, Object>emptyMap() );
178178

179179
// german locale
180180
String expected = "42,00";
@@ -232,9 +232,9 @@ public void testCallingWrongFormatterMethod() {
232232
notNullDescriptor,
233233
42.00000d,
234234
null,
235+
null,
235236
Collections.<String, Object>emptyMap(),
236-
Collections.<String, Object>emptyMap()
237-
);
237+
Collections.<String, Object>emptyMap() );
238238

239239
String expected = "${formatter.foo('%1$.2f', validatedValue)}";
240240
String actual = interpolatorUnderTest.interpolate(
@@ -308,8 +308,8 @@ private MessageInterpolatorContext createMessageInterpolatorContext(ConstraintDe
308308
descriptor,
309309
null,
310310
null,
311+
null,
311312
Collections.<String, Object>emptyMap(),
312-
Collections.<String, Object>emptyMap()
313-
);
313+
Collections.<String, Object>emptyMap() );
314314
}
315315
}

0 commit comments

Comments
 (0)