Skip to content

Commit 045e406

Browse files
committed
HV-2128 Add HV-specific Path and expose the leaf node through it
Signed-off-by: marko-bekhta <[email protected]>
1 parent 09c7ebb commit 045e406

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

documentation/src/test/java/org/hibernate/validator/referenceguide/chapter12/propertypath/PropertyPathTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.hibernate.validator.HibernateValidator;
2020
import org.hibernate.validator.path.PropertyNode;
2121

22+
import org.junit.Assert;
2223
import org.junit.Test;
2324

2425
/**
@@ -60,6 +61,13 @@ public void testPropertyNodeGetValueForSet() {
6061
assertEquals( node.getName(), "name" );
6162
assertEquals( node.as( PropertyNode.class ).getValue(), "Bob" );
6263
//end::include[]
64+
if ( path instanceof org.hibernate.validator.path.Path hvPath ) {
65+
assertEquals( hvPath.getLeafNode().getName(), "name" );
66+
assertEquals( hvPath.getLeafNode().as( PropertyNode.class ).getValue(), "Bob" );
67+
}
68+
else {
69+
Assert.fail( "Unexpected path node type: " + path.getClass().getName() );
70+
}
6371

6472
assertFalse( nodeIterator.hasNext() );
6573
}

engine/src/main/java/org/hibernate/validator/internal/engine/path/MaterializedPath.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import java.io.Serializable;
99
import java.util.Iterator;
1010

11-
import jakarta.validation.Path;
11+
import org.hibernate.validator.path.Path;
12+
1213

1314
final class MaterializedPath implements Path, Serializable {
1415

@@ -71,4 +72,9 @@ static String asString(MaterializedNode currentLeafNode) {
7172
}
7273
return builder.toString();
7374
}
75+
76+
@Override
77+
public Node getLeafNode() {
78+
return leafNode;
79+
}
7480
}

engine/src/main/java/org/hibernate/validator/internal/engine/path/MutablePath.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
import java.util.regex.Pattern;
1616

1717
import jakarta.validation.ElementKind;
18-
import jakarta.validation.Path;
1918

2019
import org.hibernate.validator.internal.metadata.aggregated.ExecutableMetaData;
2120
import org.hibernate.validator.internal.util.Contracts;
2221
import org.hibernate.validator.internal.util.logging.Log;
2322
import org.hibernate.validator.internal.util.logging.LoggerFactory;
23+
import org.hibernate.validator.path.Path;
2424

2525
/**
2626
* Internal, mutable implementation of {@code jakarta.validation.Path}.
@@ -182,6 +182,7 @@ public void removeLeafNode() {
182182
}
183183
}
184184

185+
@Override
185186
public MutableNode getLeafNode() {
186187
return currentLeafNode;
187188
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.validator.path;
6+
7+
import org.hibernate.validator.Incubating;
8+
9+
/**
10+
* An extended representation of the validation path, provides Hibernate Validator specific functionality.
11+
*/
12+
@Incubating
13+
public interface Path extends jakarta.validation.Path {
14+
15+
/**
16+
* @return The leaf node of the current path.
17+
* This is a shortcut method to iterating over the path till the last node.
18+
*/
19+
default jakarta.validation.Path.Node getLeafNode() {
20+
Path.Node leafNode = null;
21+
for ( Node node : this ) {
22+
leafNode = node;
23+
}
24+
return leafNode;
25+
}
26+
27+
}

0 commit comments

Comments
 (0)