spring-projects / spring-data-relational

Spring Data Relational. Home of Spring Data JDBC and Spring Data R2DBC.

Home Page:https://spring.io/projects/spring-data-jdbc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reading converter executed for generated 'countBy' method (r2dbc)

kdebski85 opened this issue · comments

After upgrade to org.springframework.data:spring-data-r2dbc:3.2.2, reading converter is executed for generated countBy methods.
In 3.2.1 and before, the converter was not executed as expected
Maybe the fix for #1710 is related.

Sample repository with countBy method:

public interface FooRepository extends BaseRepository<Foo, String> extends org.springframework.data.repository.reactive.ReactiveCrudRepository {

   Mono<Long> countByBar(String bar); 
}

Sample reading converter for the entity:

@Component
@ReadingConverter
class ReadConverter implements org.springframework.core.convert.converter.Converter<Row, Foo> {

    public Foo convert(Row source) {
            Foo.FooBuilder entity = Foo.builder();
            entity.name(source.get("name", String.class));
            return entity.build();
    }
}

As a result, the converter fails because it expects to retrieve name column, but only count column is returned:

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [io.r2dbc.postgresql.PostgresqlRow] to type [foo.Foo] for value [io.r2dbc.postgresql.PostgresqlRow@1e52c3ea]
	at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Assembly trace from producer [reactor.core.publisher.FluxHandleFuseable] :
	reactor.core.publisher.Flux.handle
	io.r2dbc.postgresql.PostgresqlResult.map(PostgresqlResult.java:113)
Error has been observed at the following site(s):
	*___________Flux.handle ⇢ at io.r2dbc.postgresql.PostgresqlResult.map(PostgresqlResult.java:113)
	*__________Flux.flatMap ⇢ at org.springframework.r2dbc.core.DefaultFetchSpec.lambda$all$1(DefaultFetchSpec.java:78)
	*________Flux.usingWhen ⇢ at org.springframework.r2dbc.core.DefaultDatabaseClient.inConnectionMany(DefaultDatabaseClient.java:143)
	|_      Flux.onErrorMap ⇢ at org.springframework.r2dbc.core.DefaultDatabaseClient.inConnectionMany(DefaultDatabaseClient.java:155)
	|_                      ⇢ at org.springframework.r2dbc.core.DefaultFetchSpec.all(DefaultFetchSpec.java:76)
	|_            Flux.next ⇢ at org.springframework.r2dbc.core.DefaultFetchSpec.first(DefaultFetchSpec.java:71)
	|_          Mono.handle ⇢ at org.springframework.data.r2dbc.core.R2dbcEntityTemplate$UnwrapOptionalFetchSpecAdapter.first(R2dbcEntityTemplate.java:872)
	|_         Mono.flatMap ⇢ at org.springframework.data.r2dbc.core.R2dbcEntityTemplate$EntityCallbackAdapter.first
(...)
Caused by: java.util.NoSuchElementException: Column name 'name' does not exist in column names [count]
	at io.r2dbc.postgresql.PostgresqlRow.getColumn(PostgresqlRow.java:176)
	at io.r2dbc.postgresql.PostgresqlRow.get(PostgresqlRow.java:85)
	at foo.FooConverter$ReadConverter.convert(FooConverter.java:44)

The converter should not be called at all for countBy method.

This should be fixed via #1723. Care to retest against the latest snapshots?

I checked that the issue is resolved in spring-data-r2dbc 3.2.3-SNAPSHOT. Thank you!
Should I close this ticket?

Thanks for your feedback. I'm closing this one as duplicate.