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
This commit is contained in:
Mark Paluch
2025-01-15 12:21:19 +01:00
parent 19af483959
commit b60feb71e6
3 changed files with 67 additions and 2 deletions

View File

@@ -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;
*/
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,10 +299,21 @@ 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()) {
properties.add(parameter.getName());
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);

View File

@@ -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();
}

View File

@@ -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"
)