Browse Source

Consider projections without input properties open ones.

Closes #3164
3.2.x
Mark Paluch 1 year ago
parent
commit
52e10da72e
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 2
      src/main/java/org/springframework/data/projection/DefaultProjectionInformation.java
  2. 13
      src/main/java/org/springframework/data/projection/ProjectionInformation.java
  3. 13
      src/main/java/org/springframework/data/repository/query/ReturnedType.java
  4. 20
      src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java

2
src/main/java/org/springframework/data/projection/DefaultProjectionInformation.java

@ -82,7 +82,7 @@ class DefaultProjectionInformation implements ProjectionInformation {
@Override @Override
public boolean isClosed() { public boolean isClosed() {
return this.properties.equals(getInputProperties()); return hasInputProperties() && this.properties.equals(getInputProperties());
} }
/** /**

13
src/main/java/org/springframework/data/projection/ProjectionInformation.java

@ -18,6 +18,8 @@ package org.springframework.data.projection;
import java.beans.PropertyDescriptor; import java.beans.PropertyDescriptor;
import java.util.List; import java.util.List;
import org.springframework.util.CollectionUtils;
/** /**
* Information about a projection type. * Information about a projection type.
* *
@ -40,6 +42,17 @@ public interface ProjectionInformation {
*/ */
List<PropertyDescriptor> getInputProperties(); List<PropertyDescriptor> getInputProperties();
/**
* Returns whether the projection has input properties. Projections without input types are typically open projections
* that do not follow Java's property accessor naming.
*
* @return
* @since 3.2.11
*/
default boolean hasInputProperties() {
return !CollectionUtils.isEmpty(getInputProperties());
}
/** /**
* Returns whether supplying values for the properties returned via {@link #getInputProperties()} is sufficient to * Returns whether supplying values for the properties returned via {@link #getInputProperties()} is sufficient to
* create a working proxy instance. This will usually be used to determine whether the projection uses any dynamically * create a working proxy instance. This will usually be used to determine whether the projection uses any dynamically

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

@ -33,6 +33,7 @@ import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
@ -129,9 +130,21 @@ public abstract class ReturnedType {
* Returns the properties required to be used to populate the result. * Returns the properties required to be used to populate the result.
* *
* @return * @return
* @see ProjectionInformation#getInputProperties()
*/ */
public abstract List<String> getInputProperties(); public abstract List<String> getInputProperties();
/**
* Returns whether the returned type has input properties.
*
* @return
* @since 3.2.11
* @see ProjectionInformation#hasInputProperties()
*/
public boolean hasInputProperties() {
return !CollectionUtils.isEmpty(getInputProperties());
}
/** /**
* A {@link ReturnedType} that's backed by an interface. * A {@link ReturnedType} that's backed by an interface.
* *

20
src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java

@ -31,6 +31,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.aop.Advisor; import org.springframework.aop.Advisor;
import org.springframework.aop.TargetClassAware; import org.springframework.aop.TargetClassAware;
import org.springframework.aop.framework.Advised; import org.springframework.aop.framework.Advised;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
/** /**
@ -135,6 +136,19 @@ class ProxyProjectionFactoryUnitTests {
var result = projectionInformation.getInputProperties(); var result = projectionInformation.getInputProperties();
assertThat(result).hasSize(6); assertThat(result).hasSize(6);
assertThat(projectionInformation.hasInputProperties()).isTrue();
assertThat(projectionInformation.isClosed()).isTrue();
}
@Test // DATACMNS-630
void identifiersOpenProjectionCorrectly() {
var projectionInformation = factory.getProjectionInformation(OpenProjection.class);
var result = projectionInformation.getInputProperties();
assertThat(result).isEmpty();
assertThat(projectionInformation.hasInputProperties()).isFalse();
assertThat(projectionInformation.isClosed()).isFalse();
} }
@Test // DATACMNS-655, GH-2831 @Test // DATACMNS-655, GH-2831
@ -357,6 +371,12 @@ class ProxyProjectionFactoryUnitTests {
Map<String, Object> getData(); Map<String, Object> getData();
} }
interface OpenProjection {
@Value("#{@greetingsFrom.groot(target.firstname)}")
String hello();
}
interface CustomerExcerptWithDefaultMethod extends CustomerExcerpt { interface CustomerExcerptWithDefaultMethod extends CustomerExcerpt {
default String getFirstnameAndId() { default String getFirstnameAndId() {

Loading…
Cancel
Save