[CSHARP-432] Linq doesn't work with derived types when needs to query by string value field Created: 04/Apr/12  Updated: 02/Apr/15  Resolved: 04/Apr/12

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

Type: Bug Priority: Critical - P2
Reporter: Brian Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File Program.cs    
Issue Links:
Duplicate
duplicates CSHARP-418 NullReferenceException in LINQ query ... Closed

 Description   

Got abstract BaseClass and DerivedClass in "collection1"
Mapping:

            //BaseClass
            BsonClassMap.RegisterClassMap<BaseClass>(cm =>
            {
                cm.AutoMap();
                cm.MapIdProperty(x => x.Id);
                cm.SetIsRootClass(true);
                cm.GetMemberMap(c => c.Properties)
                    .SetSerializationOptions(DictionarySerializationOptions.ArrayOfDocuments);
            });
            BsonClassMap.RegisterClassMap<DerivedClass>();

Query:

var s1 = db.GetCollection("collection1").AsQueryable<DerivedClass>().Where(x => x.Id == "some").Single(); - don't work
var s2 = db.GetCollection("collection1").AsQueryable<BaseClass>().Where(x => x.Id == "some").Single(); - work
var s3 = db.GetCollection("collection1").AsQueryable<DerivedClass>().Where(x => x.SomeLogicalField).Single(); - work
var s4 = db.GetCollection("collection1").AsQueryable<DerivedClass>().Where(x => x.SomeIntegerField > 666).Single(); - work

Exception: NullReferenceException "Object reference not set to an instance of an object"
Stack:
at MongoDB.Bson.Serialization.BsonClassMapSerializer.GetMemberSerializationInfo(String memberName) in C:\work\10gen\mongodb\mongo-csharp-driver\Bson\Serialization\BsonClassMapSerializer.cs:line 253
at MongoDB.Driver.Linq.SelectQuery.GetSerializationInfoMember(IBsonSerializer serializer, MemberExpression memberExpression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 962
at MongoDB.Driver.Linq.SelectQuery.GetSerializationInfo(IBsonSerializer serializer, Expression expression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 888
at MongoDB.Driver.Linq.SelectQuery.GetSerializationInfo(Expression expression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 880
at MongoDB.Driver.Linq.SelectQuery.BuildComparisonQuery(BinaryExpression binaryExpression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 433
at MongoDB.Driver.Linq.SelectQuery.BuildQuery(Expression expression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 768
at MongoDB.Driver.Linq.SelectQuery.BuildQuery() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 113
at MongoDB.Driver.Linq.SelectQuery.Execute() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 122
at MongoDB.Driver.Linq.MongoQueryProvider.Execute(Expression expression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\MongoQueryProvider.cs:line 147
at MongoDB.Driver.Linq.MongoQueryable`1.GetEnumerator() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\MongoQueryable.cs:line 81
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at DatabaseMappingTest.Program.Main() in D:\Projects\Support Projects\TestDBSchema\DatabaseMappingTest\Program.cs:line 3380
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()



 Comments   
Comment by Robert Stam [ 04/Apr/12 ]

Thanks for taking another look. I'll go ahead and mark this as resolved then. This is fixed in the master branch in github so if you're willing to work with the very latest you will see it fixed.

Comment by Brian [ 04/Apr/12 ]

Yep, now I see that this is the same bug - driver doesn't handle base class properties in derived classes.

Comment by Robert Stam [ 04/Apr/12 ]

I don't get a NullReferenceException running your program with the latest version of the driver (master in github).

The only query you comment as failing is the last. But that query is using the DerivedClass as the type argument to AsQueryable and then querying on an inherited property (x.Id).

That's precisely what CSHARP-418 was about (doesn't matter if it's integer or string, it's the fact that it was inherited that was triggering the bug).

This is still looking like a duplicate to me. Let me know what you think. Thanks.

Comment by Robert Stam [ 04/Apr/12 ]

Thanks. I'll download your program and try it.

Comment by Brian [ 04/Apr/12 ]

Done

Comment by Robert Stam [ 04/Apr/12 ]

OK. Can you provide the class declarations you used in this test? I'll work on reproducing then. Thanks.

Comment by Brian [ 04/Apr/12 ]

Not the same: there query via interger field - and it's work properly (s4 in my tests).
The bug is that driver can't handle query with string fields

They are "attached" bugs

Comment by Robert Stam [ 04/Apr/12 ]

Is this a duplicate of CSHARP-418? In which case it has already been fixed.

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