spring-projects / spring-data-cassandra

Provides support to increase developer productivity in Java when using Apache Cassandra. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-cassandra/
Apache License 2.0
376 stars 309 forks source link

User Defined ConversionService is ignored #1343

Closed neshkeev closed 1 year ago

neshkeev commented 1 year ago

A user can define their own ConversionService bean and spring reuses it (example 1, example 2) if the bean name is conversionService and it's of the org.springframework.core.convert.ConversionService type.

Unfortunately spring-data-cassandra always creates a new org.springframework.core.convert.support.DefaultConversionService in MappingCassandraConverter#newConversionService which prevents users from tweaking the conversionService the way they need.

mp911de commented 1 year ago

Let's take a step back before we apply changes and discuss what you want to achieve. The design is by intent encapsulating the conversion service as customizations in the conversion service require additional metadata for proper type-based conversions.

neshkeev commented 1 year ago

@mp911de , you can configure conversions with a custom CassandraCustomConversions bean, in fact I found it out when spring-data-cassandra ignored my custom ConversionService bean which I am used to defining in my code. In order to make this discovery I had to spend a couple of hours, so I figured the project might benefit from providing users with smooth spring experience in regards to defining custom ConversionService beans for spring-data-cassandra-based projects. Also I think if a user in addition to spring-data-cassandra also includes a different spring module, which supports user defined ConversionService beans, they might be lost when the same bean is used in one part of their application and ignored at the same time when it comes to Cassandra

What I like about user defined ConversionService is that it has a constructor, where I can define an initialization part if necessary (sending a HTTP request, for example). There are no problems with defining the same initialization code when it comes to Java-Annotation-based configuration, but it might be difficult when users go with xml-based configuration, since you can only list all the custom converters and that is it.

On the other hand we can dedicate a section about conversions in the Spring Data Cassandra documentation, which explains design decisions and guides users on defining users' own converters

mp911de commented 1 year ago

The reference documentation explains custom converters and shows some of the limitations of a bare metal ConversionService. Mixing conversion services can easily lead to all sorts of unwanted problems as in some cases we remove converters to overcome defaults, or unwanted converters can kick in. The list of potential issues is endless.

Let us know what's missing from the documentation so others do not run into similar issues.

spring-projects-issues commented 1 year 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.

neshkeev commented 1 year ago

@mp911de , thank you for the hint. I don't understand why I didn't find it right away. Although I think it might be worth explicitly mentioning that defining a user's own org.springframework.core.convert.ConversionService bean is meaningless, because spring-data-cassandra applies custom conversions if a CassandraCustomConversions bean is defined. I was probably looking for ConversionService on the web page, I found nothing.