[CSHARP-708] BsonClassMap.SetIdMember fails for lhamda expressions that contain interface members Created: 20/Mar/13  Updated: 20/Mar/14  Resolved: 21/Mar/13

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

Type: Bug Priority: Blocker - P1
Reporter: Blake Niemyjski Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: driver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 8, MongoDB 2.4


Issue Links:
Related
is related to CSHARP-637 Fluent Lambda Expressions Don't Resol... Closed
Backwards Compatibility: Fully Compatible

 Description   

We upgrade to the 1.8 driver via NuGet this morning and instantly brought down our dev environment.

Here is part of the stack trace:

at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
at MongoDB.Bson.Serialization.BsonClassMap`1.ResolveExplicitProperty(MemberInfo interfaceMemberInfo, Type targetType)
at MongoDB.Bson.Serialization.BsonClassMap`1.GetMemberInfoFromLambda[TMember](Expression`1 memberLambda)
at MongoDB.Bson.Serialization.BsonClassMap`1.GetMemberNameFromLambda[TMember](Expression`1 memberLambda)

Here is some snippets that will show how to reproduce:

protected override void ConfigureClassMap(BsonClassMap<T> cm) {
cm.SetIdMember(cm.GetMemberMap(c => c.Id).SetRepresentation(BsonType.ObjectId));
}

..
where T : class, IIdentity, new() {
..

public interface IIdentity {
string Id

{ get; }

}

..

public class JobHistory : IIdentity {
public string Id

{ get; set; }

}
..



 Comments   
Comment by Blake Niemyjski [ 21/Mar/13 ]

You guys are awesome!

Comment by auto [ 20/Mar/13 ]

Author:

{u'date': u'2013-03-20T16:28:11Z', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-708: fixed bug where interface maps were too strict with regards to mapping a member back to it's interface declaration.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/97bb29236f5fc11f7b60de653012e2721e8c23a9

Comment by auto [ 20/Mar/13 ]

Author:

{u'date': u'2013-03-20T16:01:49Z', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-708: added breaking test.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/9c969995219b0942d634682c54956cdc8c844a00

Comment by Blake Niemyjski [ 20/Mar/13 ]

Hello,

Thanks for the clarification. I think we may have one other potentially blocking issue. I'll let you know when I get into the next sprint (later today/this week).

Comment by Craig Wilson [ 20/Mar/13 ]

Just to note... This problem only exists for lambdas applied to a class with a constraint where the implementation contains a different number of accessors than the interface. That was a mouthful, so as to your example... Because IIdentity.Id only has a getter, but your implementation JobHistory.Id has a getter and a setter, we are having an issue. Hence, if this is the only location that type of thing occurs, you are probably ok (and we'd like to know if you hit any other regressions; this was a major release).

In previous versions of the driver, this worked by accident, so we were a little surprised it worked at all when the report came in.

Comment by Blake Niemyjski [ 20/Mar/13 ]

Thanks for the work around, I may wait this one out for a 1.8.1 as we have a ton of code uses lhamdas for setting representation and member names.

Comment by Craig Wilson [ 20/Mar/13 ]

Ok, crappy workaround, but I think very safe in this case is to use a string instead of the lambda expression. It's unlikely you'll change the Id member name in your IIdentity interface.

        void ConfigureClassMap<T>(BsonClassMap<T> cm)
            where T : class, IIdentity, new()
        {
            cm.SetIdMember(cm.GetMemberMap("Id").SetRepresentation(BsonType.ObjectId));
        }

Let me know if this is satisfactory in the mean time and we'll get this fixed for the next release.

Comment by Craig Wilson [ 20/Mar/13 ]

I can confirm this is a regression. I'll work to figure out a simple workaround and post back.

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