Read domain type when returning an implemented interface from repository query methods.

We now read the domain type instead of trying to materialize an interface if the type returned from the query method is implemented by the domain object handled by the repository.

Closes #519
This commit is contained in:
Mark Paluch
2021-01-08 11:03:55 +01:00
parent 908f17bd64
commit a418fd2f92
3 changed files with 21 additions and 1 deletions

View File

@@ -122,6 +122,10 @@ public abstract class AbstractR2dbcQuery implements RepositoryQuery {
ReturnedType returnedType = resultProcessor.getReturnedType();
if (returnedType.getReturnedType().isAssignableFrom(returnedType.getDomainType())) {
return returnedType.getDomainType();
}
return returnedType.isProjecting() ? returnedType.getDomainType() : returnedType.getReturnedType();
}

View File

@@ -347,11 +347,16 @@ public abstract class AbstractR2dbcRepositoryIntegrationTests extends R2dbcInteg
Mono<Integer> countByNameContains(String namePart);
}
public interface Buildable {
String getName();
}
@Getter
@Setter
@Table("legoset")
@NoArgsConstructor
static class LegoSet extends Lego {
static class LegoSet extends Lego implements Buildable {
String name;
Integer manual;

View File

@@ -137,8 +137,19 @@ public class H2R2dbcRepositoryIntegrationTests extends AbstractR2dbcRepositoryIn
}).verifyComplete();
}
@Test // gh-519
public void shouldReturnEntityThroughInterface() {
shouldInsertNewItems();
repository.findByName("SCHAUFELRADBAGGER").map(Buildable::getName).as(StepVerifier::create)
.expectNext("SCHAUFELRADBAGGER").verifyComplete();
}
interface H2LegoSetRepository extends LegoSetRepository {
Mono<Buildable> findByName(String name);
@Override
@Query("SELECT name FROM legoset")
Flux<Named> findAsProjection();