[JAVA-555] Incorrect object type received in BSONObject encoding hook (Transformer) when multiple transformers found Created: 20/Apr/12  Updated: 08/Nov/17  Resolved: 08/Nov/17

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Jordi Pradel Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Minor Change

 Description   

When multiple encoding hooks are found for a class in BSONObject, they are applied linearly:

BSON.java line 202 to 205:
List<Transformer> l = _encodingHooks.get( o.getClass() );
if ( l != null )
  for ( Transformer t : l )
    o = t.transform( o );

If the first transformer found transforms from C to T and the second from C to R (both were registered as encoding hooks for class C or one of its superclasses):
1. The first transformer transforms o (of type C) to o' of type T
2. The second transformer, programmed to transform things of type C receives an object of type T, which is potentially unrelated to C. So, we receive an object of an unexpected type.

Solution would be to apply the first transformer (getting o' of type T) and then looking for encoding hooks for class T and so on, until no encoding hooks are found.

Apart from that, if I have a class C1 that extends C2 and I have encoding hooks for both (EH1 and EH2), when given an object of type C2 to DBObject, EH2 should be applied, not EH1, since C2 is more specific than C1. Currently, it applies both (and this is how I found what I explained earlier).



 Comments   
Comment by Jeffrey Yemin [ 08/Nov/17 ]

We are no longer investing in the Transformer API, so closing as Won't Fix.

As an alternative, consider creating configuating a DocumentCodec as needed and add it to the CodecRegistry on the MongoClient.

Generated at Thu Feb 08 08:52:33 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.