Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.mongodb.repository.aot;

import java.util.Set;
import java.util.regex.Pattern;

import org.bson.Document;
Expand Down Expand Up @@ -48,6 +49,7 @@
* {@link CodeBlock} generator for common tasks.
*
* @author Christoph Strobl
* @author maryantocinn
* @since 5.0
*/
class MongoCodeBlocks {
Expand Down Expand Up @@ -237,8 +239,8 @@ static void appendReadPreference(AotQueryMethodGenerationContext context, Builde
}

/**
* Wraps the given {@link CodeBlock} representing an {@link Iterable} into a {@link Streamable} if the
* {@link MethodReturn} indicates so.
* Adapts the given {@link CodeBlock} representing an {@link Iterable} to the declared collection return type if
* necessary.
*/
public static CodeBlock potentiallyWrapStreamable(MethodReturn methodReturn, CodeBlock returningIterable) {

Expand All @@ -255,6 +257,11 @@ public static CodeBlock potentiallyWrapStreamable(MethodReturn methodReturn, Cod
returnType, DefaultConversionService.class, Streamable.class, returningIterable, TypeDescriptor.class);
}

if (ClassUtils.isAssignable(Set.class, returnType)) {
return CodeBlock.of("$2T.getSharedInstance().convert($3L, $1T.class)", returnType,
DefaultConversionService.class, returningIterable);
}

return returningIterable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@

/**
* @author Christoph Strobl
* @author maryantocinn
*/
public interface UserRepository extends CrudRepository<User, String> {

/* Derived Queries */

List<User> findUserNoArgumentsBy();

Set<User> findUserSetBy();

Streamable<User> streamUserNoArgumentsBy();

User findOneByUsername(String username);
Expand Down Expand Up @@ -234,6 +237,8 @@ public interface UserRepository extends CrudRepository<User, String> {

List<UserProjection> findUserProjectionByLastnameStartingWith(String lastname);

Set<UserProjection> findUserProjectionSetByLastnameStartingWith(String lastname);

Page<UserProjection> findUserProjectionByLastnameStartingWith(String lastname, Pageable page);

<T> Page<T> findUserProjectionByLastnameStartingWith(String lastname, Pageable page, Class<T> projectionType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;

import org.bson.BsonString;
Expand Down Expand Up @@ -79,6 +80,7 @@
*
* @author Christoph Strobl
* @author Mark Paluch
* @author maryantocinn
*/
@Testcontainers(disabledWithoutDocker = true)
@SpringJUnitConfig(classes = MongoRepositoryContributorTests.MongoRepositoryContributorConfiguration.class)
Expand Down Expand Up @@ -200,6 +202,15 @@ void testDerivedFinderWithoutArguments() {
assertThat(users).hasSize(7).hasOnlyElementsOfType(User.class);
}

@Test // GH-5225
void testDerivedQueryAsSet() {

Set<User> users = fragment.findUserSetBy();

assertThat(users).isInstanceOf(Set.class).extracting(User::getUsername).containsExactlyInAnyOrder("luke", "leia",
"han", "chewbacca", "yoda", "vader", "kylo");
}

@Test
void testCountWorksAsExpected() {

Expand Down Expand Up @@ -563,6 +574,14 @@ void testDerivedFinderReturningListOfProjections() {
assertThat(users).extracting(UserProjection::getUsername).containsExactlyInAnyOrder("han", "kylo", "luke", "vader");
}

@Test // GH-5225
void testDerivedQueryReturningSetOfProjections() {

Set<UserProjection> users = fragment.findUserProjectionSetByLastnameStartingWith("S");
assertThat(users).isInstanceOf(Set.class).extracting(UserProjection::getUsername)
.containsExactlyInAnyOrder("han", "kylo", "luke", "vader");
}

@Test
void testDerivedFinderReturningPageOfProjections() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author Tomasz Forys
* @author maryantocinn
*/
class QueryMethodContributionUnitTests {

Expand Down Expand Up @@ -399,6 +400,17 @@ void rendersStreamableReturnType() throws NoSuchMethodException {
.containsSubsequence("return", "Streamable.of(", "all())");
}

@Test // GH-5225
void rendersSetReturnType() throws NoSuchMethodException {

MethodSpec methodSpec = codeOf(UserRepository.class, "findUserSetBy");

assertThat(methodSpec.toString()) //
.contains("DefaultConversionService.getSharedInstance().convert(") //
.containsSubsequence("return", "all()", "Set.class") //
.doesNotContain("return finder.matching(filterQuery).all()");
}

@Test // GH-5089
void rendersStreamableReturnTypeForAggregation() throws NoSuchMethodException {

Expand Down
Loading