Skip to content

Commit 9bb5db3

Browse files
rsvasylievgsmet
authored andcommitted
HV-1853 Fix inconsistency of getter method detection in GetterPropertySelectionStrategy
1 parent 588744c commit 9bb5db3

File tree

8 files changed

+95
-15
lines changed

8 files changed

+95
-15
lines changed

documentation/src/test/java/org/hibernate/validator/referenceguide/chapter12/getterselectionstrategy/FluentGetterPropertySelectionStrategy.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Collections;
88
import java.util.Optional;
99
import java.util.Set;
10+
import java.util.List;
1011
import java.util.stream.Collectors;
1112

1213
import org.hibernate.validator.spi.properties.ConstrainableExecutable;
@@ -36,9 +37,9 @@ public Optional<String> getProperty(ConstrainableExecutable executable) {
3637
}
3738

3839
@Override
39-
public Set<String> getGetterMethodNameCandidates(String propertyName) {
40+
public List<String> getGetterMethodNameCandidates(String propertyName) {
4041
// As method name == property name, there always is just one possible name for a method
41-
return Collections.singleton( propertyName );
42+
return Collections.singletonList( propertyName );
4243
}
4344
}
4445
//end::include[]

engine/src/main/java/org/hibernate/validator/internal/properties/DefaultGetterPropertySelectionStrategy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
package org.hibernate.validator.internal.properties;
88

99
import java.lang.invoke.MethodHandles;
10+
import java.util.List;
1011
import java.util.Optional;
11-
import java.util.Set;
1212

1313
import org.hibernate.validator.internal.util.CollectionHelper;
1414
import org.hibernate.validator.internal.util.Contracts;
@@ -54,10 +54,10 @@ public Optional<String> getProperty(ConstrainableExecutable executable) {
5454
}
5555

5656
@Override
57-
public Set<String> getGetterMethodNameCandidates(String propertyName) {
57+
public List<String> getGetterMethodNameCandidates(String propertyName) {
5858
Contracts.assertNotEmpty( propertyName, "Name of a property must not be empty" );
5959

60-
Set<String> nameCandidates = CollectionHelper.newHashSet( GETTER_PREFIXES.length );
60+
List<String> nameCandidates = CollectionHelper.newArrayList( GETTER_PREFIXES.length );
6161
for ( String prefix : GETTER_PREFIXES ) {
6262
nameCandidates.add( prefix + Character.toUpperCase( propertyName.charAt( 0 ) ) + propertyName.substring( 1 ) );
6363
}

engine/src/main/java/org/hibernate/validator/internal/util/privilegedactions/GetMethodFromGetterNameCandidates.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import java.lang.reflect.Method;
1010
import java.security.PrivilegedAction;
11-
import java.util.Set;
11+
import java.util.List;
1212

1313
/**
1414
* Returns the method with the specified property name or {@code null} if it does not exist. This action will
@@ -23,20 +23,20 @@
2323
public final class GetMethodFromGetterNameCandidates implements PrivilegedAction<Method> {
2424

2525
private final Class<?> clazz;
26-
private final Set<String> getterNameCandidates;
26+
private final List<String> getterNameCandidates;
2727
private final boolean lookForMethodsInHierarchy;
2828

29-
private GetMethodFromGetterNameCandidates(Class<?> clazz, Set<String> getterNameCandidates, boolean lookForMethodsInHierarchy) {
29+
private GetMethodFromGetterNameCandidates(Class<?> clazz, List<String> getterNameCandidates, boolean lookForMethodsInHierarchy) {
3030
this.clazz = clazz;
3131
this.getterNameCandidates = getterNameCandidates;
3232
this.lookForMethodsInHierarchy = lookForMethodsInHierarchy;
3333
}
3434

35-
public static GetMethodFromGetterNameCandidates action(Class<?> clazz, Set<String> getterNameCandidates) {
35+
public static GetMethodFromGetterNameCandidates action(Class<?> clazz, List<String> getterNameCandidates) {
3636
return new GetMethodFromGetterNameCandidates( clazz, getterNameCandidates, false );
3737
}
3838

39-
public static GetMethodFromGetterNameCandidates action(Class<?> clazz, Set<String> possibleMethodNames, boolean lookForMethodsInHierarchy) {
39+
public static GetMethodFromGetterNameCandidates action(Class<?> clazz, List<String> possibleMethodNames, boolean lookForMethodsInHierarchy) {
4040
return new GetMethodFromGetterNameCandidates( clazz, possibleMethodNames, lookForMethodsInHierarchy );
4141
}
4242

engine/src/main/java/org/hibernate/validator/spi/properties/GetterPropertySelectionStrategy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.hibernate.validator.spi.properties;
88

9+
import java.util.List;
910
import java.util.Optional;
1011
import java.util.Set;
1112

@@ -42,6 +43,6 @@ public interface GetterPropertySelectionStrategy {
4243
*
4344
* @return the {@link Set} of possible getter names
4445
*/
45-
Set<String> getGetterMethodNameCandidates(String propertyName);
46+
List<String> getGetterMethodNameCandidates(String propertyName);
4647

4748
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.test.internal.xml;
8+
9+
/**
10+
* Test class for HV-1853.
11+
*
12+
* @author Roman Vasilyev
13+
*/
14+
public class MultipleGetterCandidates {
15+
private Integer property1 = 1;
16+
private Integer property2 = 1;
17+
18+
public boolean hasProperty1() {
19+
return property1 != null;
20+
}
21+
22+
public Integer getProperty1() {
23+
return property1;
24+
}
25+
26+
public boolean hasProperty2() {
27+
return property2 != null;
28+
}
29+
30+
public Integer getProperty2() {
31+
return property2;
32+
}
33+
}

engine/src/test/java/org/hibernate/validator/test/internal/xml/XmlMappingTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import jakarta.validation.ValidationException;
2525
import jakarta.validation.Validator;
2626
import jakarta.validation.ValidatorFactory;
27+
import jakarta.validation.constraints.Min;
2728
import jakarta.validation.constraints.NotNull;
2829
import jakarta.validation.constraints.Size;
2930
import jakarta.validation.executable.ExecutableType;
@@ -431,4 +432,19 @@ public void test_constraint_is_applied_to_type_argument_of_inherited_getter() {
431432
);
432433
}
433434

435+
@Test
436+
public void test_right_getter_used_when_there_are_multiple_getter_candidates() {
437+
final Configuration<?> configuration = ValidatorUtil.getConfiguration();
438+
configuration.addMapping( XmlMappingTest.class.getResourceAsStream( "hv-1853-mapping.xml" ) );
439+
440+
final ValidatorFactory validatorFactory = configuration.buildValidatorFactory();
441+
final Validator validator = validatorFactory.getValidator();
442+
443+
Set<ConstraintViolation<MultipleGetterCandidates>> violations = validator.validate( new MultipleGetterCandidates() );
444+
assertThat( violations ).containsOnlyViolations(
445+
violationOf( Min.class ).withProperty( "property1" ),
446+
violationOf( Min.class ).withProperty( "property2" )
447+
);
448+
}
449+
434450
}

engine/src/test/java/org/hibernate/validator/test/properties/GetterPropertySelectionStrategyTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ public Optional<String> getProperty(ConstrainableExecutable executable) {
232232
}
233233

234234
@Override
235-
public Set<String> getGetterMethodNameCandidates(String propertyName) {
236-
return Collections.singleton( propertyName );
235+
public List<String> getGetterMethodNameCandidates(String propertyName) {
236+
return Collections.singletonList( propertyName );
237237
}
238238
}
239239

@@ -252,8 +252,8 @@ public Optional<String> getProperty(ConstrainableExecutable executable) {
252252
}
253253

254254
@Override
255-
public Set<String> getGetterMethodNameCandidates(String propertyName) {
256-
return Collections.singleton( "foo" + Character.toUpperCase( propertyName.charAt( 0 ) ) + propertyName.substring( 1 ) );
255+
public List<String> getGetterMethodNameCandidates(String propertyName) {
256+
return Collections.singletonList( "foo" + Character.toUpperCase( propertyName.charAt( 0 ) ) + propertyName.substring( 1 ) );
257257
}
258258
}
259259

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Hibernate Validator, declare and validate application constraints
4+
~
5+
~ License: Apache License, Version 2.0
6+
~ See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
7+
-->
8+
<constraint-mappings
9+
xmlns="https://jakarta.ee/xml/ns/validation/mapping"
10+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
11+
xsi:schemaLocation="https://jakarta.ee/xml/ns/validation/mapping
12+
https://jakarta.ee/xml/ns/validation/validation-mapping-3.0.xsd"
13+
version="3.0">
14+
15+
<default-package>org.hibernate.validator.internal.xml</default-package>
16+
17+
<bean class="org.hibernate.validator.test.internal.xml.MultipleGetterCandidates">
18+
<getter name="property1">
19+
<constraint annotation="jakarta.validation.constraints.Min">
20+
<element name="value">2</element>
21+
</constraint>
22+
</getter>
23+
<getter name="property2">
24+
<constraint annotation="jakarta.validation.constraints.Min">
25+
<element name="value">2</element>
26+
</constraint>
27+
</getter>
28+
</bean>
29+
</constraint-mappings>

0 commit comments

Comments
 (0)