Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2648

POJOs with Collections not deserialized without a setter

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.6.0
    • Affects Version/s: 3.5.0
    • Component/s: POJO
    • None
    • Environment:
      JDK 8, Linux

      When using the new POJO codecs, a POJO with a collection that has no setter is serialised to Mongo successfully but when deserialised, the POJO does not have the collection set.

      This is contrary to the behavior of most codecs such as GSON, Jackson, Johnzon etc etc which by default use the getter to get access to the collection and then write the entries. This issue causes POJOs created with JAXB from XSD not work with the Mongo BSON POJO Codec which is very inconvenient as all other frameworks work fine with them.

      E.g a Person like this can be written and read from Mongo using the POJO codec provider without any data loss.

      public class Person {
          private String name;
          private List<Pet> pets;
          public String getName() {
              return name;
          }
          public void setName(String name) {
              this.name = name;
          }
          public List<Pet> getPets() {
              return pets;
          }
          public void setPets(List<Pet> pets) {
              this.pets = pets;
          }
      }
      
      public class Pet {
          private String type;
          public String getType() {
              return type;
          }
          public void setType(String type) {
              this.type = type;
          }
      }
      

      But like this it writes and reads without an exception but the Person read does not have any pets in it:

      public class Person {
          private String name;
          private List<Pet> pets;
          public String getName() {
              return name;
          }
          public void setName(String name) {
              this.name = name;
          }
          public List<Pet> getPets() {
              if (pets == null) {
                  pets = new ArrayList<>();
              }
              return pets;
          }
      }
      
      public class Pet {
          private String type;
          public String getType() {
              return type;
          }
          public void setType(String type) {
              this.type = type;
          }
      }
      

      It would be great if the Codec could handle this by using the getter and populating the collection accordingly.

      Thanks
      Paul

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            paulcb Paul Carter-Brown
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: