Browse Source

Skip unnamed DTO projection properties.

We now skip unnamed DTO projection properties and issue a warning log to raise awareness.

Skipping unnamed (null) properties avoids identification as DTO and only selects properties stemming from named constructor arguments.

Add tests for Kotlin data classes using value classes for verification.

Closes #3225
pull/3235/head
Mark Paluch 11 months ago
parent
commit
ae6fa747a1
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 18
      src/main/java/org/springframework/data/repository/query/ReturnedType.java
  2. 19
      src/test/java/org/springframework/data/repository/query/ReturnedTypeUnitTests.java
  3. 30
      src/test/kotlin/org/springframework/data/repository/query/SomeDataClass.kt

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

@ -24,6 +24,9 @@ import java.util.List; @@ -24,6 +24,9 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.mapping.Parameter;
import org.springframework.data.mapping.PreferredConstructor;
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer;
@ -47,6 +50,8 @@ import org.springframework.util.ObjectUtils; @@ -47,6 +50,8 @@ import org.springframework.util.ObjectUtils;
*/
public abstract class ReturnedType {
private static final Log logger = LogFactory.getLog(ReturnedType.class);
private static final Map<CacheKey, ReturnedType> cache = new ConcurrentReferenceHashMap<>(32);
private final Class<?> domainType;
@ -294,11 +299,22 @@ public abstract class ReturnedType { @@ -294,11 +299,22 @@ public abstract class ReturnedType {
return Collections.emptyList();
}
List<String> properties = new ArrayList<>(constructor.getConstructor().getParameterCount());
int parameterCount = constructor.getConstructor().getParameterCount();
List<String> properties = new ArrayList<>(parameterCount);
for (Parameter<Object, ?> parameter : constructor.getParameters()) {
if (parameter.getName() != null) {
properties.add(parameter.getName());
}
}
if (properties.isEmpty() && parameterCount > 0) {
if (logger.isWarnEnabled()) {
logger.warn(("No constructor parameter names discovered. "
+ "Compile the affected code with '-parameters' instead or avoid its introspection: %s")
.formatted(type.getName()));
}
}
return Collections.unmodifiableList(properties);
}

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

@ -167,6 +167,25 @@ class ReturnedTypeUnitTests { @@ -167,6 +167,25 @@ class ReturnedTypeUnitTests {
assertThat(left).isSameAs(right);
}
@Test // GH-3225
void detectsKotlinInputProperties() {
var factory = new SpelAwareProxyProjectionFactory();
var returnedType = ReturnedType.of(SomeDataClass.class, Sample.class, factory);
assertThat(returnedType.getInputProperties()).containsExactly("firstname", "lastname");
}
@Test // GH-3225
void detectsKotlinValueClassInputProperties() {
var factory = new SpelAwareProxyProjectionFactory();
var returnedType = ReturnedType.of(SomeDataClassWithValues.class, Sample.class, factory);
assertThat(returnedType.getInputProperties()).containsExactly("email", "firstname", "lastname");
}
private static ReturnedType getReturnedType(String methodName, Class<?>... parameters) throws Exception {
return getQueryMethod(methodName, parameters).getResultProcessor().getReturnedType();
}

30
src/test/kotlin/org/springframework/data/repository/query/SomeDataClass.kt

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.query
/**
* @author Mark Paluch
*/
data class SomeDataClass(val firstname: String, val lastname: String = "Doe")
@JvmInline
value class Email(val value: String)
data class SomeDataClassWithValues(
val email: Email,
val firstname: String,
val lastname: String = "Doe"
)
Loading…
Cancel
Save