[JAVA-3925] DocumentCodec ignores registry when writing Iterable or Map instances Created: 10/Jan/21 Updated: 28/Oct/23 Resolved: 01/Nov/21 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | Codecs |
| Affects Version/s: | None |
| Fix Version/s: | 4.4.0 |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Conchi Bueno | Assignee: | Jeffrey Yemin |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Case: | (copied to CRM) | ||||||||
| Backwards Compatibility: | Minor Change | ||||||||
| Documentation Changes: | Needed | ||||||||
| Description |
|
Using POJO serialization, updates seem to be problematic. Considering this POJO:
Note, the "value" is Jackson. To support this, it was registered a custom Codec for ObjectNode. Updates will not invoke the codec. For example:
The ObjectNode is replaced with [] in the resulting BSON, which makes subsequent POJO reads fail. After some investigation, it was identified the root cause is [this line](https://github.com/mongodb/mongo-java-driver/blob/r3.12.5/bson/src/main/org/bson/codecs/DocumentCodec.java#L192) in the DocumentCodec. [This line](https://github.com/mongodb/mongo-java-driver/blob/r3.12.5/bson/src/main/org/bson/codecs/DocumentCodec.java#L192) special-cases encoding instances of Iterable with its own encoder. Normally this is not an issue. However in this case it is an issue because ObjectNode extends JsonNode which implements Iterable<JsonNode>. This prevents the process from falling through to [this line](https://github.com/mongodb/mongo-java-driver/blob/r3.12.5/bson/src/main/org/bson/codecs/DocumentCodec.java#L197). As a workaround, the ObjectNode can be wrapped in some other class that does not implement Iterable, and provide a codec for that class as well. |
| Comments |
| Comment by Githook User [ 01/Nov/21 ] |
|
Author: {'name': 'Jeff Yemin', 'email': 'jeff.yemin@mongodb.com', 'username': 'jyemin'}Message: Use CodecRegistry in DocumentCodec for Iterable and Map instances (#811)
|
| Comment by John Lilley [ 11/Jan/21 ] |
|
This is the workaround we implemented for the issue. Unfortunately, it cannot be added to existing code, you must retrofit POJOs with the wrapper type.
See attached codec_example.txt
|