Skip to content

Provide an optional way to convert from BsonUndefined to null [DATAMONGO-1439] #2350

@spring-projects-issues

Description

@spring-projects-issues

Sylvain LAURENT opened DATAMONGO-1439 and commented

Currently (as of 1.8.2) when reading an "undefined" value from mongo, we have this exception :

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.bson.BsonUndefined] to type [java.math.BigDecimal]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:313) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:176) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:822) ~[spring-data-mongodb-1.8.2.RELEASE.jar:na]

It would be nice to have a way of converting sunch BsonUndefined to null out of the box.

For my own usage, I created this converter factory :

@ReadingConverter
public class BsonUndefinedToNullObjectConverterFactory implements ConverterFactory<BsonUndefined, @Nullable Object> {
    @Override
    public <T extends @Nullable Object> Converter<BsonUndefined, T> getConverter(Class<T> targetType) {
        return o -> null;
    }

}

It works, with the small glitch of this warning which I think is not correct for this situation :

WARN o.s.d.m.c.convert.CustomConversions - - Registering converter from class org.bson.BsonUndefined to class java.lang.Object as reading converter although it doesn't convert from a Mongo supported type! You might wanna check you annotation setup at the converter implementation.


Affects: 1.8.4 (Gosling SR4)

1 votes, 2 watchers

Activity

jkjsanthosh

jkjsanthosh commented on Nov 6, 2022

@jkjsanthosh

@a-cordier why do you close this issue, this converter support has to be added in spring data mongodb by default right?
I have see your team wrote a custom converter and registered in mongomappingconverter. But that is temporary solution only right?

added a commit that references this issue on May 20, 2024
98795cb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

in: mappingMapping and conversion infrastructuretype: enhancementA general enhancement

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @odrotbohm@christophstrobl@spring-projects-issues@jkjsanthosh

      Issue actions

        Provide an optional way to convert from BsonUndefined to null [DATAMONGO-1439] · Issue #2350 · spring-projects/spring-data-mongodb