Git Product home page Git Product logo

Comments (5)

mp911de avatar mp911de commented on June 14, 2024 1

R2dbcCustomConversions keeps track of read and write targets. When registering a converter, we determine the custom read or write target. If that is set, then we register the converter in Spring's ConversionService and use the conversion service to perform the actual conversion.

Spring's ConversionService comes already with a converter (StringToEnumConverterFactory) that matches Enum<T> as target type. Because of type erasure, the T from EmptyStringToEnumReadingConverter<T extends Enum<T>> is being flattened down to Enum and therefore, the conversion service now has two converters. Spring's ConversionService checks the raw type for your converter and since the raw type is Enum and not Debt.DebtSource, the custom converter isn't used.

Instead, StringToEnum is provided by the StringToEnumConverterFactory that is a ConverterFactory and it creates an instance for string to enum conversion with an exact type match.

Let me know whether this helps.

from spring-data-r2dbc.

mp911de avatar mp911de commented on June 14, 2024

Enum conversion works as designed. null values in the column map to null values in the domain model. Anything else is being looked up through Enum.valueOf(…). Translating empty string values to null is a rule specific to your domain and not something that applies broadly.

As this is a rather specific issue, please configure a custom conversions bean for your application.

from spring-data-r2dbc.

bskorka avatar bskorka commented on June 14, 2024

I don't know if you get my point here.
When I create the custom conversion bean it is not used. The org.springframework.core.convert.support.StringToEnumConverterFactory.StringToEnum is used instead and it deals with empty strings.
The question is why the Spring StringToEnum is used only if R2dbcCustomConversions are declared (not empty), and why my custom bean is not used.

from spring-data-r2dbc.

bskorka avatar bskorka commented on June 14, 2024

@mp911de - I still think it's a bug or a inconsistent behavior, do you disagree?

from spring-data-r2dbc.

bskorka avatar bskorka commented on June 14, 2024

After the explanation, I got the point of why my converter is not used.

What I didn't understand was why the Spring ConversionService and StringToEnumConverterFactory were not used without the registration of the R2dbcCustomConversions bean. I thought that spring and spring-data-r2dbc would treat the data equally. Your answer covers that a little.

It is also a little bit unclear that one day we will throw an error on an empty string, and on the next day if you register the custom converter for literally anything, your enums will be converted without any error.

from spring-data-r2dbc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.