[JAVA-3665] Polymorphic POJO mapping does not work with package registration only Created: 19/Mar/20 Updated: 27/Oct/23 Resolved: 20/Apr/20 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | POJO |
| Affects Version/s: | 3.12.2 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | John Lilley | Assignee: | Ross Lawley |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Java 8, Mongodb server 3.6, mongodb-driver-sync 3.12.2 |
||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Description |
|
When polymorphic classes are used with @BsonDiscriminator, attempts to read/deserialize POJOs that return polymorhpic results (any of the derived classes may be returned) will fail, unless you do one of two things:
The suggestion initially given by mongodb support (reading from Collection<DerivedClass> instead of Collection<BaseClass> is not workable, because you cannot read a list of mixed types e.g. The issue seems to be that simply registering the package as prescribed is not enough. Attempting to read from the database results in the following error:
When you run this, you'll see that the write command works as expected, returning a polymorphic list, but the read command fails if that is the first command run in a process. The only difference is that the write command first inserts Derived1 and Dervied2 before reading them back. Clearly, it must somehow convince the POJO codec to believe that the derived classes all exist before it can read them. Changing the codec registration to CodecProvider pojoCodecProvider = PojoCodecProvider.builder().register(Base.class, Derived1.class, Derived2.class).build(); CodecProvider pojoCodecProvider = PojoCodecProvider.builder().register(Base.class, Derived1.class, Derived2.class).build(); Works as expected. |
| Comments |
| Comment by Ross Lawley [ 21/Apr/20 ] |
|
Hi john.lilley@redpointglobal.com, I've added JAVA-3707 to look at scanning the class path for BsonDiscriminators. All the best, Ross |
| Comment by John Lilley [ 20/Apr/20 ] |
|
Even if this works "as designed", I have described a valid and useful case which IMHO should be supported. Can you reclassify this as a Story and re-open it? A more appropriate title would be: "Support polymorphic POJO mapping with package registration and custom BsonDiscriminator". In my workplace, we would never allow the default classname discriminator, as doing so prohibits refactoring and makes the schema fragile. Thanks john |
| Comment by Ross Lawley [ 20/Apr/20 ] |
|
Hi john.lilley@redpointglobal.com, Thanks for the ticket, I was wondering why I couldn't reproduce the issue and thought the library already did package based lookups for classes. Having looked at your code I noticed that the discriminators are all lowercase. If the discriminator is the name of the class, then the registered package based lookups will work. However, if it is different to the class name, then it would have to be explicitly declarated. All the best, Ross |