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