Serialization/Deserialization broken in 5.6.1

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: 5.6.1
    • Component/s: BSON
    • None
    • None
    • Java Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Summary

      Since version 5.6.x of org.mongodb:bson there's a bug in scanning classes for serialization/deserialization with generic types. Although the concrete type is completely statically defined, bson does not accept the class any more. Same code did work in versions prior to this, i.e. 5.5.1.

      Application throws exception before accessing any database.

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      DBMS: Mongo DB (ver. 7.0.16) Case sensitivity: plain=mixed, delimited=mixed

      How to Reproduce

      GenericContainer.java
      //abstract generic class for accessing collection
      @BsonDiscriminator
      public abstract class GenericContainer<T> {
        private ObjectId id;
        private InnerModel<T> inner;
      
        public ObjectId getId() {
          return id;
        }
        public void setId(final ObjectId id) {
          this.id = id;
        }
        public InnerModel<T> getInner() {
          return inner;
        }
        void setInner(final InnerModel<T> inner) {
          this.inner = inner;
        }
      }
      InnerModel.java
      // class for property in GenericContainer
      public class InnerModel<T> {
        private T field;
      
        public T getField() {
          return field;
        }
        public void setField(final T field) {
          this.field = field;
        }
      }
      IntContainer.java
      public class IntContainer extends GenericContainer<Integer> {}
      StringContainer.java
      public class StringContainer extends GenericContainer<String> {}
      Testmethod
      public void test(final MongoDatabase database) {
        @SuppressWarnings("unchecked")
        final MongoCollection<GenericContainer<?>>  collection =
          (MongoCollection<GenericContainer<?>>) (MongoCollection<?>)
          database.getCollection("genericCollection", GenericContainer.class);
      
        final IntContainer intContainer = new IntContainer();
        intContainer.setInner(new InnerModel<>());
        intContainer.getInner().setField(3);
      
        final InsertOneResult insertResult = collection.insertOne(intContainer);
        System.out.println(insertResult.getInsertedId());
      }

      Additional Background

      Code works in versions 5.5.x

            Assignee:
            Slav Babanin
            Reporter:
            Sven Lamberti-Meng
            None
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: