Skip to content

Commit 97f8505

Browse files
committed
HV-2029 Allow same group convert rules on the methods in class hierarchies
1 parent 3aa8de7 commit 97f8505

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

engine/src/main/java/org/hibernate/validator/internal/metadata/aggregated/CascadingMetaDataBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,12 @@ private static Map<Class<?>, Class<?>> mergeGroupConversion(Map<Class<?>, Class<
351351
Map<Class<?>, Class<?>> mergedGroupConversions = new HashMap<>( groupConversions.size() + otherGroupConversions.size() );
352352

353353
for ( Entry<Class<?>, Class<?>> otherGroupConversionEntry : otherGroupConversions.entrySet() ) {
354-
if ( groupConversions.containsKey( otherGroupConversionEntry.getKey() ) ) {
354+
Class<?> convertTo = groupConversions.get( otherGroupConversionEntry.getKey() );
355+
if ( convertTo != null && !convertTo.equals( otherGroupConversionEntry.getValue() ) ) {
355356
throw LOG.getMultipleGroupConversionsForSameSourceException(
356357
otherGroupConversionEntry.getKey(),
357358
CollectionHelper.<Class<?>>asSet(
358-
groupConversions.get( otherGroupConversionEntry.getKey() ),
359+
convertTo,
359360
otherGroupConversionEntry.getValue() ) );
360361
}
361362
}

engine/src/test/java/org/hibernate/validator/test/internal/engine/groups/conversion/AbstractGroupConversionTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ public void groupConversionOnParameter() throws Exception {
100100
);
101101
}
102102

103+
@Test
104+
public void groupConversionOnParameterInHierarchy() throws Exception {
105+
Set<ConstraintViolation<User6>> violations = getValidator().forExecutables().validateParameters(
106+
new User6Extended(),
107+
User6Extended.class.getMethod( "setAddresses", List.class ),
108+
new List<?>[] { Arrays.asList( new Address() ) }
109+
);
110+
111+
assertThat( violations ).containsOnlyViolations(
112+
violationOf( NotNull.class ).withPropertyPath( pathWith()
113+
.method( "setAddresses" )
114+
.parameter( "addresses", 0 )
115+
.property( "street1", true, null, 0, List.class, 0 )
116+
),
117+
violationOf( Size.class ).withPropertyPath( pathWith()
118+
.method( "setAddresses" )
119+
.parameter( "addresses", 0 )
120+
.property( "zipCode", true, null, 0, List.class, 0 )
121+
)
122+
);
123+
}
124+
103125
@Test
104126
public void groupConversionOnReturnValue() throws Exception {
105127
Set<ConstraintViolation<User7>> violations = getValidator().forExecutables().validateReturnValue(
@@ -333,7 +355,19 @@ public PhoneNumber getPhoneNumber() {
333355

334356
private static class User6 {
335357

336-
public void setAddresses(@Valid @ConvertGroup(from = Default.class, to = BasicPostal.class) List<Address> addresses) {
358+
public void setAddresses(
359+
@Valid @ConvertGroup(from = Default.class, to = BasicPostal.class) List<Address> addresses
360+
) {
361+
}
362+
}
363+
364+
private static class User6Extended extends User6 {
365+
366+
@Override
367+
public void setAddresses(
368+
@Valid @ConvertGroup(from = Default.class, to = BasicPostal.class) List<Address> addresses
369+
) {
370+
// do nothing as well
337371
}
338372
}
339373

0 commit comments

Comments
 (0)