[CSHARP-532] Cannot set subclass using strongly typed update builder Created: 19/Jul/12  Updated: 20/Mar/14  Resolved: 19/Jul/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.5
Fix Version/s: 1.6

Type: Bug Priority: Major - P3
Reporter: Joe Waller Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: C#
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

A BsonSerializationException is thrown when attempting to use the strongly typed update builder to set a subclass type. The following code reproduces the issue:

[BsonKnownTypes(typeof(B))]
public abstract class A
{
 
}
 
public class B : A
{
 
}
 
public class C
{
    [BsonElement("a")]
    public A A { get; set; }
}
 
class Program
{
    static void Main(string[] args)
    {
        var b = new B();
 
        var s = Update.SetWrapped("a", b);
        var t = Update<C>.Set(c => c.A, b); // throws BsonSerializationException
    }
}



 Comments   
Comment by Joe Waller [ 20/Jul/12 ]

Wow, that was quick. Thanks!

Comment by auto [ 19/Jul/12 ]

Author:

{u'date': u'2012-07-19T06:44:17-07:00', u'email': u'craiggwilson@gmail.com', u'name': u'Craig Wilson'}

Message: CSHARP-532: fixed bug with typed builders when using a value with a different type than the nominal type.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/42c17f2c636315f762d2a450e9b6d7cde6ca6e53

Comment by Craig Wilson [ 19/Jul/12 ]

Thanks for reporting. I can confirm this issue and we'll look into it. For now, you'll need to use the untyped builder for this particular property.

Generated at Wed Feb 07 21:37:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.