Code Monkey home page Code Monkey logo

Comments (2)

webermich avatar webermich commented on June 10, 2024

Hi Thomas,

thanks for using polymorphia and trying to improve it!

SimpleMapTypeCodec's intention is to encode/decode ordinary Java maps.
Most java map implementations allow to put Null-values into.
The specialized map BsonDocument extends BsonValue implements Map<String, BsonValue>, Cloneable, Bson, Serializable does not allow null. Instead a placeholder BsonNull.VALUE must be used.

If every Null-map-value would be decoded as org.bson.BsonNull.VALUE this would be quite unexpected by the consumer of that map especially since most maps to be decoded are not BsonDocument

SimpleMapTypeCodec is not the correct codec to use to decode a property of type BsonDocument. Since most codecs (also those provided by mongo driver project)
are incapable of decoding Null correctly the SimpleMapTypeCodec handles that and in most cases it works.

Now the org.bson.codecs.BsonDocumentCodec handles Null values when decoding and creates org.bson.BsonNull.VALUE for such.

If you can make sure to chain the org.bson.codecs.BsonDocumentCodec before the PojoCodecProvider in your CodecRegistry setup you should be done.

Try chaining the mongo default codecs first like:


    public static CodecRegistry getCodecRegistry() {
        return CodecRegistries.fromRegistries(
                MongoClient.getDefaultCodecRegistry(),
                CodecRegistries.fromProviders(
                        new EnumCodecProvider(),
                        PojoCodecProvider.builder().register("com.xyz.abc").build()
                )
        );
    }
 

Let me know, if that works for you.
If you need to keep your current Codec-order try adding CodecRegistries.fromCodecs(new BsonDocumentCodec()) to the top of your registry creation.

from polymorphia.

t-beckmann avatar t-beckmann commented on June 10, 2024

Your argument makes perfect sense!

Therefore I'd love to change the order getting the defaults into first place but some of my classes to be handled by Polymorphia implement Iterable. This causes org.bson.codecs.IterableCodec to be picked up for them, trying to start with an array for those and failing: org.bson.BsonInvalidOperationException: A StartArray value cannot be written to the root level of a BSON document. So Polymorphia must stay in first place and changing the order does not work unless altering my domain classes.

Actually I already worked around the issue by coding things differently, so that there is no test causing the error, still. Nevertheless I think transcoding BsonDocument should work for me. Therefore to avoid running into issues in the future, I follow your suggestion and add the standard BsonDocumentCodec to the top. This works perfectly fine.

from polymorphia.

Related Issues (7)

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.