spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. 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-mongodb/
Apache License 2.0
1.59k stars 1.07k forks source link

Null-Values from custom converters are stored in document #4710

Open CybAtax opened 1 month ago

CybAtax commented 1 month ago

With the release of spring-boot 3.2.0 a change to the deserialization was noted, which was addressed in a previous issue: https://github.com/spring-projects/spring-data-mongodb/issues/4571

The change itself makes sense, as the serialized null value should be respected. The analysis surprisingly led to a different issue as null-values are being serialized despite the defaulting setting of not writing null-values as specified here: https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/mapping/Field.html#write()

However, that check appears to only apply to unconverted values, as seen in: https://github.com/spring-projects/spring-data-mongodb/blob/286404d9681cf0748511cb6c37290a113c7a58a2/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java#L906 while the value retrieved from a custom converter is not checked for nullity. The Javadoc specifically allows returning null from a Converter (https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/convert/converter/Converter.html#convert(S))

One specific use case may be reducing data in the database and thus converting certain values to null - with the intention of them not being written. With the provided examples above however, this does not appear to be the case.

Additionally it is interesting, that the Converter Javadoc specifies that the source may never be null (https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/convert/converter/Converter.html#convert(S)) - even though this can occur when converting a value to null during serialization and then deserializing said value with the corresponding ReadingConverter.

An example project verifying the issue can be found at https://github.com/CybAtax/null-serialization-demo/tree/main

Any assistance in how to resolve this will be greatly appreciated.

christophstrobl commented 1 month ago

Thank you @CybAtax for raising this ticket. Let me take it to the team.