spring-projects / spring-data-relational

Spring Data Relational. Home of Spring Data JDBC and Spring Data R2DBC.
https://spring.io/projects/spring-data-jdbc
Apache License 2.0
753 stars 345 forks source link

Enable to persist enumeration in postgresql r2dbc #1832

Closed charboubmustapha closed 1 month ago

charboubmustapha commented 2 months ago

Hello Guys, I'm using enumeration of postgresql with spring data r2dbc , i followed the documentation to register the custom converter, but the save is failing with an error of : java.lang.IllegalArgumentException: Cannot encode parameter of type com.example.r2dbc.Mode (AUTOMATIC) Here is the converter

@WritingConverter
public class ModeConverter implements Converter<Mode, Mode> {
    @Override
    public Mode convert(@NonNull Mode pricingMode) {
        return pricingMode;
    }
}

Here is the configuration class

@Configuration
@AllArgsConstructor
public class R2dbcConfiguration extends AbstractR2dbcConfiguration {
    private final ConnectionFactory connectionFactory;

    @Override
    @NonNull
    public ConnectionFactory connectionFactory() {
        return connectionFactory;
    }

    @Override
    @NonNull
    protected List<Object> getCustomConverters() {
        return List.of(new ModeConverter());
    }
}

I created an example of project with tests : r2dbc.zip

Thank you for your help

charboubmustapha commented 1 month ago

It seem that problem come from the spring boot auto configure integration especially the class R2dbcDataAutoConfiguration. The bean R2dbcCustomConversions is created with an empty list ignoring custom converters .


    @Bean
    @ConditionalOnMissingBean
    public R2dbcCustomConversions r2dbcCustomConversions() {
        List<Object> converters = new ArrayList<>(this.dialect.getConverters());
        converters.addAll(R2dbcCustomConversions.STORE_CONVERTERS);
        return new R2dbcCustomConversions(
                CustomConversions.StoreConversions.of(this.dialect.getSimpleTypeHolder(), converters),
                Collections.emptyList());
    }
schauder commented 1 month ago

Please provide a Minimimal Reproducable Example, preferable as a Github repository. Make sure to include the database, either as an in memory database or if that is not possible using Testcontainers.

charboubmustapha commented 1 month ago

Please provide a Minimimal Reproducable Example, preferable as a Github repository. Make sure to include the database, either as an in memory database or if that is not possible using Testcontainers.

Sorry the example was in the first comment but the formatting was wrong , here is the example using Testcontainers r2dbc.zip

schauder commented 1 month ago

Ah sorry, looks like I messed that up. Thanks for the reproducer

mp911de commented 1 month ago

IllegalArgumentException: Cannot encode parameter of type com.example.r2dbc.Mode (AUTOMATIC) comes from the driver. Did you register an EnumCodec in the driver to configure the driver what Postgres enum type to use for a specific Java enum type? The driver documentation explains how to register enum codecs. In a Spring Boot context, you can either register a driver extension so that the driver auto-detects the extension or, you can configure ConnectionFactory yourself.

spring-projects-issues commented 1 month ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues commented 1 month ago

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.