Browse Source

DATACMNS-840 - ReturnedClass now aborts parameter name detection for types with multiple constructors.

In case a DTO type exposes multiple no-argument constructors we now leniently abort the parameter name detection and let downstream users decide what to do in this particular case.

The problematic case can't be outright rejected here already as there's valid scenarios for that, e.g. in JPA the type could be used for multiple query methods, using explict constructor expressions listing arguments explicitly and thereby select one of the multiple constructors.
pull/172/head
Oliver Gierke 10 years ago
parent
commit
6b4b52391b
  1. 5
      src/main/java/org/springframework/data/repository/query/ReturnedType.java
  2. 20
      src/test/java/org/springframework/data/repository/query/ReturnedTypeUnitTests.java

5
src/main/java/org/springframework/data/repository/query/ReturnedType.java

@ -283,6 +283,11 @@ public abstract class ReturnedType { @@ -283,6 +283,11 @@ public abstract class ReturnedType {
PreferredConstructorDiscoverer<?, ?> discoverer = new PreferredConstructorDiscoverer(type);
PreferredConstructor<?, ?> constructor = discoverer.getConstructor();
if (constructor == null) {
return Collections.emptyList();
}
List<String> properties = new ArrayList<String>();
for (PreferredConstructor.Parameter<Object, ?> parameter : constructor.getParameters()) {

20
src/test/java/org/springframework/data/repository/query/ReturnedTypeUnitTests.java

@ -130,6 +130,17 @@ public class ReturnedTypeUnitTests { @@ -130,6 +130,17 @@ public class ReturnedTypeUnitTests {
assertThat(type.getTypeToRead(), is(typeCompatibleWith(BigInteger.class)));
}
/**
* @see DATACMNS-840
*/
@Test
public void detectsSampleDtoWithDefaultConstructor() throws Exception {
ReturnedType type = getReturnedType("dtoWithMultipleConstructors");
assertThat(type.getInputProperties(), is(empty()));
}
private static ReturnedType getReturnedType(String methodName, Class<?>... parameters) throws Exception {
return getQueryMethod(methodName, parameters).getResultProcessor().getReturnedType();
}
@ -162,6 +173,8 @@ public class ReturnedTypeUnitTests { @@ -162,6 +173,8 @@ public class ReturnedTypeUnitTests {
Page<SampleProjection> findPageProjection(Pageable pageable);
BigInteger countQuery();
SampleDtoWithMultipleConstructors dtoWithMultipleConstructors();
}
static class Sample {
@ -180,6 +193,13 @@ public class ReturnedTypeUnitTests { @@ -180,6 +193,13 @@ public class ReturnedTypeUnitTests {
}
}
static class SampleDtoWithMultipleConstructors {
SampleDtoWithMultipleConstructors(String firstname) {}
SampleDtoWithMultipleConstructors(int age) {}
}
interface SampleProjection {
String getLastname();

Loading…
Cancel
Save