From 859ffa862cd6213802931b7c8a589b04eea68c6b Mon Sep 17 00:00:00 2001 From: Faron Dutton Date: Fri, 22 Nov 2024 10:18:19 -0500 Subject: [PATCH] Adds failing unit-test for issue #4839 NPE when projecting to an interface and using StringBasedAggregation --- .../repository/AbstractPersonRepositoryIntegrationTests.java | 5 +++++ .../data/mongodb/repository/PersonRepository.java | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java index 33720d2557..20e333e348 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java @@ -1275,6 +1275,11 @@ void readsClosedProjection() { assertThat(repository.findClosedProjectionBy()).isNotEmpty(); } + @Test // https://github.com/spring-projects/spring-data-mongodb/issues/4839 + void findAggregatedClosedProjectionBy() { + assertThat(repository.findAggregatedClosedProjectionBy()).isNotEmpty(); + } + @Test // DATAMONGO-1865 void findFirstEntityReturnsFirstResultEvenForNonUniqueMatches() { assertThat(repository.findFirstBy()).isNotNull(); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java index c2036cabf9..09ead80617 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java @@ -386,6 +386,10 @@ Page findByCustomQueryLastnameAndAddressStreetInList(String lastname, Li // DATAMONGO-1752 Iterable findClosedProjectionBy(); + // https://github.com/spring-projects/spring-data-mongodb/issues/4839 + @Aggregation("{ '$project': { _id : 0, firstName : 1, lastname : 1 } }") + Iterable findAggregatedClosedProjectionBy(); + @Query(sort = "{ age : -1 }") List findByAgeGreaterThan(int age);