[CSHARP-2660] Unexpected behaviour with a new BsonMemberMap instance for SetExtraElementsMember Created: 04/Jul/19 Updated: 31/Mar/22 |
|
| Status: | Backlog |
| Project: | C# Driver |
| Component/s: | Serialization |
| Affects Version/s: | 2.8.1 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | James Turner | Assignee: | Unassigned |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Backwards Compatibility: | Minor Change |
| Description |
|
There is a subtle bug with the handling of `BsonClassMap.SetExtraElementsMember()`. That method will take any instance of a `BsonMemberMap` but serialization breaks in an unexpected way if you do give it a new instance (and only when there is an extra field to utilize the extra elements member map). There is an internal property on `BsonClassMap` called `ExtraElementsMemberMapIndex` which is set from the `Freeze` method on the class map (line 593 of `BsonClassMap`). The comparison performed here is `==` which, as there is no overridden equals method on the class map, uses the default `object.Equals` which is a reference comparison. BsonClassMap.cs
That reference comparison obviously fails as its a new instance but we only get an error during serialization and the error during serialization is somewhat confusing. It states, when there is an extra field, that the extra field doesn't exist on the deserialization target. But because you've called `SetExtraElementsMember`, you're none-the-wiser as to why. (I only realised it when going through the source code of `BsonClassMap`.) BsonClassMapSerializer.cs
Basically I have a few thoughts about what could make it less annoying for anyone else who stumbles across it:
|
| Comments |
| Comment by Esha Bhargava [ 28/Sep/20 ] | ||||||||||||||||||||||||||||||
|
Turnerj Thanks for reporting this bug! We are moving it to the backlog for now, but we'll work on this when resources permit. | ||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 21/Sep/20 ] | ||||||||||||||||||||||||||||||
|
SetExtraElementsMember requires that the provided member map be an existing member map already configured for the class, but that is currently not verified. The best fix for this is for SetExtraElementsMember to throw an exception if the provided member map is not already part of the class map. | ||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 21/Sep/20 ] | ||||||||||||||||||||||||||||||
|
To reproduce:
|