Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.lang.invoke.MethodHandles;
import java.util.Map;

import javax.validation.Path;
import javax.validation.metadata.ConstraintDescriptor;

import org.hibernate.validator.internal.util.logging.Log;
Expand All @@ -33,19 +34,22 @@ public class MessageInterpolatorContext implements HibernateMessageInterpolatorC
private final ConstraintDescriptor<?> constraintDescriptor;
private final Object validatedValue;
private final Class<?> rootBeanType;
private final Path propertyPath;
@Immutable
private final Map<String, Object> messageParameters;
@Immutable
private final Map<String, Object> expressionVariables;

public MessageInterpolatorContext(ConstraintDescriptor<?> constraintDescriptor,
Object validatedValue,
Class<?> rootBeanType,
Map<String, Object> messageParameters,
Map<String, Object> expressionVariables) {
Object validatedValue,
Class<?> rootBeanType,
Path propertyPath,
Map<String, Object> messageParameters,
Map<String, Object> expressionVariables) {
this.constraintDescriptor = constraintDescriptor;
this.validatedValue = validatedValue;
this.rootBeanType = rootBeanType;
this.propertyPath = propertyPath;
this.messageParameters = toImmutableMap( messageParameters );
this.expressionVariables = toImmutableMap( expressionVariables );
}
Expand Down Expand Up @@ -75,6 +79,11 @@ public Map<String, Object> getExpressionVariables() {
return expressionVariables;
}

@Override
public Path getPropertyPath() {
return propertyPath;
}

@Override
public <T> T unwrap(Class<T> type) {
//allow unwrapping into public super types
Expand Down Expand Up @@ -122,6 +131,8 @@ public String toString() {
sb.append( "MessageInterpolatorContext" );
sb.append( "{constraintDescriptor=" ).append( constraintDescriptor );
sb.append( ", validatedValue=" ).append( validatedValue );
sb.append( ", rootBeanType=" ).append( rootBeanType.getName() );
sb.append( ", propertyPath=" ).append( propertyPath );
sb.append( ", messageParameters=" ).append( messageParameters );
sb.append( ", expressionVariables=" ).append( expressionVariables );
sb.append( '}' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public ConstraintViolation<T> createConstraintViolation(ValueContext<?, ?> local
messageTemplate,
localContext.getCurrentValidatedValue(),
descriptor,
constraintViolationCreationContext.getPath(),
constraintViolationCreationContext.getMessageParameters(),
constraintViolationCreationContext.getExpressionVariables()
);
Expand Down Expand Up @@ -451,12 +452,14 @@ private static boolean buildDisableAlreadyValidatedBeanTracking(ValidationOperat
private String interpolate(String messageTemplate,
Object validatedValue,
ConstraintDescriptor<?> descriptor,
Path path,
Map<String, Object> messageParameters,
Map<String, Object> expressionVariables) {
MessageInterpolatorContext context = new MessageInterpolatorContext(
descriptor,
validatedValue,
getRootBeanClass(),
path,
messageParameters,
expressionVariables
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;

import javax.validation.MessageInterpolator;
import javax.validation.Path;

/**
* Extension to {@code MessageInterpolator.Context} which provides functionality
Expand Down Expand Up @@ -40,4 +41,11 @@ public interface HibernateMessageInterpolatorContext extends MessageInterpolator
* @since 5.4.1
*/
Map<String, Object> getExpressionVariables();

/**
* @return the path to the validated constraint starting from the root bean
*
* @since 6.0.21
*/
Path getPropertyPath();
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public void testExpressionLanguageGraphNavigation() {
notNullDescriptor,
user,
null,
null,
Collections.<String, Object>emptyMap(),
Collections.<String, Object>emptyMap()
);
Collections.<String, Object>emptyMap() );

String expected = "18";
String actual = interpolatorUnderTest.interpolate( "${validatedValue.age}", context );
Expand All @@ -81,9 +81,9 @@ public void testUnknownPropertyInExpressionLanguageGraphNavigation() {
notNullDescriptor,
new User(),
null,
null,
Collections.<String, Object>emptyMap(),
Collections.<String, Object>emptyMap()
);
Collections.<String, Object>emptyMap() );

String expected = "${validatedValue.foo}";
String actual = interpolatorUnderTest.interpolate( "${validatedValue.foo}", context );
Expand Down Expand Up @@ -171,9 +171,9 @@ public void testLocaleBasedFormatting() {
notNullDescriptor,
42.00000d,
null,
null,
Collections.<String, Object>emptyMap(),
Collections.<String, Object>emptyMap()
);
Collections.<String, Object>emptyMap() );

// german locale
String expected = "42,00";
Expand Down Expand Up @@ -231,9 +231,9 @@ public void testCallingWrongFormatterMethod() {
notNullDescriptor,
42.00000d,
null,
null,
Collections.<String, Object>emptyMap(),
Collections.<String, Object>emptyMap()
);
Collections.<String, Object>emptyMap() );

String expected = "${formatter.foo('%1$.2f', validatedValue)}";
String actual = interpolatorUnderTest.interpolate(
Expand Down Expand Up @@ -307,8 +307,8 @@ private MessageInterpolatorContext createMessageInterpolatorContext(ConstraintDe
descriptor,
null,
null,
null,
Collections.<String, Object>emptyMap(),
Collections.<String, Object>emptyMap()
);
Collections.<String, Object>emptyMap() );
}
}
Loading