Skip to content

Commit 145a99d

Browse files
committed
Polishing.
Use TypeName instead of Object actualReturnType for improved contextual representation of the type. Refine query type and return type usage. Add integration tests to verify query consistency. See #5178 Original pull request: #5179
1 parent f78e87f commit 145a99d

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/QueryBlocks.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.core.annotation.MergedAnnotation;
2525
import org.springframework.data.domain.ScrollPosition;
26+
import org.springframework.data.javapoet.TypeNames;
2627
import org.springframework.data.mongodb.core.ExecutableFindOperation.FindWithQuery;
2728
import org.springframework.data.mongodb.core.MongoOperations;
2829
import org.springframework.data.mongodb.core.annotation.Collation;
@@ -39,6 +40,7 @@
3940
import org.springframework.data.util.Lazy;
4041
import org.springframework.javapoet.CodeBlock;
4142
import org.springframework.javapoet.CodeBlock.Builder;
43+
import org.springframework.javapoet.TypeName;
4244
import org.springframework.util.ClassUtils;
4345
import org.springframework.util.NumberUtils;
4446
import org.springframework.util.StringUtils;
@@ -76,26 +78,25 @@ CodeBlock build() {
7678
MongoEntityMetadata<?> entityMetadata = queryMethod.getEntityInformation();
7779

7880
Builder builder = CodeBlock.builder();
79-
80-
boolean isProjecting = context.getReturnedType().isProjecting();
8181
boolean hasDynamicProjection = queryMethod.getParameters().hasDynamicProjection();
8282
Class<?> queryType = entityMetadata.getCollectionEntity().getType();
83-
Class<?> entityType = entityMetadata.getJavaType();
84-
Object actualReturnType = hasDynamicProjection || isProjecting
85-
? methodReturn.getActualTypeName()
86-
: entityType;
83+
boolean isDocumentReturn = methodReturn.getActualReturnClass().equals(Document.class);
84+
boolean isProjecting = context.getReturnedType().isProjecting()
85+
|| !queryType.equals(entityMetadata.getJavaType());
86+
TypeName actualReturnType = isDocumentReturn ? methodReturn.getActualClassName()
87+
: TypeNames.typeNameOrWrapper(methodReturn.getActualType());
8788

8889
builder.add("\n");
8990

9091
if (hasDynamicProjection) {
91-
builder.addStatement("$T<$T> $L = $L.query($T.class).as($L)", FindWithQuery.class, actualReturnType,
92+
builder.addStatement("$1T<$2T> $3L = $4L.query($5T.class).as($6L)", FindWithQuery.class, actualReturnType,
9293
context.localVariable("finder"), mongoOpsRef, queryType, context.getDynamicProjectionParameterName());
93-
} else if (isProjecting || !queryType.equals(entityType)) {
94-
builder.addStatement("$T<$T> $L = $L.query($T.class).as($T.class)", FindWithQuery.class, actualReturnType,
95-
context.localVariable("finder"), mongoOpsRef, queryType, actualReturnType);
96-
} else {
97-
builder.addStatement("$T<$T> $L = $L.query($T.class)", FindWithQuery.class, actualReturnType,
94+
} else if (isProjecting) {
95+
builder.addStatement("$1T<$2T> $3L = $4L.query($5T.class).as($2T.class)", FindWithQuery.class, actualReturnType,
9896
context.localVariable("finder"), mongoOpsRef, queryType);
97+
} else {
98+
builder.addStatement("$1T<$2T> $3L = $4L.query($2T.class)", FindWithQuery.class, queryType,
99+
context.localVariable("finder"), mongoOpsRef);
99100
}
100101

101102
String terminatingMethod;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
*/
1616
package org.springframework.data.mongodb.repository;
1717

18-
import static java.util.Arrays.asList;
19-
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
21-
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
22-
import static org.assertj.core.api.Assumptions.assumeThat;
23-
import static org.springframework.data.geo.Metrics.KILOMETERS;
18+
import static java.util.Arrays.*;
19+
import static org.assertj.core.api.Assertions.*;
20+
import static org.assertj.core.api.Assumptions.*;
21+
import static org.springframework.data.geo.Metrics.*;
2422

2523
import java.util.ArrayList;
2624
import java.util.Arrays;
@@ -40,22 +38,13 @@
4038
import org.junit.jupiter.api.Test;
4139
import org.junit.jupiter.api.TestInstance;
4240
import org.junit.jupiter.api.extension.ExtendWith;
41+
4342
import org.springframework.beans.factory.annotation.Autowired;
4443
import org.springframework.dao.DuplicateKeyException;
4544
import org.springframework.dao.IncorrectResultSizeDataAccessException;
46-
import org.springframework.data.domain.Example;
47-
import org.springframework.data.domain.ExampleMatcher;
45+
import org.springframework.data.domain.*;
4846
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatcher;
49-
import org.springframework.data.domain.Limit;
50-
import org.springframework.data.domain.Page;
51-
import org.springframework.data.domain.PageRequest;
52-
import org.springframework.data.domain.Pageable;
53-
import org.springframework.data.domain.Range;
54-
import org.springframework.data.domain.ScrollPosition;
55-
import org.springframework.data.domain.Slice;
56-
import org.springframework.data.domain.Sort;
5747
import org.springframework.data.domain.Sort.Direction;
58-
import org.springframework.data.domain.Window;
5948
import org.springframework.data.geo.Box;
6049
import org.springframework.data.geo.Circle;
6150
import org.springframework.data.geo.Distance;
@@ -185,6 +174,14 @@ void findsPersonsByLastname() {
185174
assertThat(result).hasSize(1).contains(carter);
186175
}
187176

177+
@Test // GH-5178
178+
@SuppressWarnings("rawtypes")
179+
void findsExtendedPersonsByLastname() {
180+
181+
List<ExtendedPerson> result = repository.findExtendedPersonByLastname("Beauford");
182+
assertThat(result).hasSize(1).extracting(ExtendedPerson::getClass).contains((Class) ExtendedPerson.class);
183+
}
184+
188185
@Test
189186
void findsPersonsByFirstname() {
190187

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.repository;
17+
18+
import org.springframework.data.mongodb.core.mapping.Document;
19+
20+
/**
21+
* @author Mark Paluch
22+
*/
23+
@Document("person")
24+
public class ExtendedPerson extends Person {}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
6767
*/
6868
List<Person> findByLastname(String lastname);
6969

70+
List<ExtendedPerson> findExtendedPersonByLastname(String lastname);
71+
7072
List<Person> findByLastnameStartsWith(String prefix);
7173

7274
List<Person> findByLastnameEndsWith(String postfix);

0 commit comments

Comments
 (0)