Skip to content

Commit

Permalink
Skip unnamed DTO projection properties.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mp911de committed Jan 15, 2025
1 parent 6f0b7f6 commit ae6fa74
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
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;
Expand All @@ -47,6 +50,8 @@
*/
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;
Expand Down Expand Up @@ -294,10 +299,21 @@ private List<String> detectConstructorParameterNames(Class<?> type) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ void cachesInstancesBySourceTypes() {
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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit ae6fa74

Please sign in to comment.