[CSHARP-4658] NullReferenceException when using certain projections Created: 18/May/23  Updated: 28/Oct/23  Resolved: 23/May/23

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

Type: Bug Priority: Unknown
Reporter: Dan Marinescu Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

I found a case in which using AsQueryable() throws NRE. Driver version: 2.19.1.

How to Reproduce

I have this model:

class Model
{
    public Guid NotId { get; set; }
    public string Name { get; set; }
} 

The query is:

var collection = database.GetCollection<Model>("models");
var query = await collection.AsQueryable()
    .GroupBy(x => x.Name)
    .Select(x => new Model
    {
        NotId = Guid.Empty,
        Name = x.Key
    })
    .ToListAsync(); // throws NRE

Additional Background

I am using V3 guid serialization:

BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));

Workarounds

I found two workarounds:

  1. Modify the projection to return anonymous type instead of Model
  2. Change the return type of Model.NotId to Guid?


 Comments   
Comment by Githook User [ 24/May/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4658: NullReferenceException when using certain projections.
Branch: v2.19.x
https://github.com/mongodb/mongo-csharp-driver/commit/7746b0fab545580d5a7ad6d44e569e8ccc2f474e

Comment by Githook User [ 23/May/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4658: NullReferenceException when using certain projections.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/89e5d988e7db38e049a918fc4cbce4b5a47aa451

Comment by Robert Stam [ 22/May/23 ]

Note: I've reproduced the issues reported using `string.Empty` instead of `Guid.Empty` because working with Guids is harder. The root cause didn't have anything to do with `Guid` so the tests are equivalent.

Comment by Javier LaRota [ 18/May/23 ]

Same issue with the collection.Aggregate() function

var result = await collection.Aggregate()
	.Group(x => "",
		g => new 
		{
			Count = g.Count()
		})
	.Project(x => new ModelAggregated
	{
		NotId = Guid.Empty,
		Count = x.Count
	})
	.ToListAsync(); // This throws NullReferenceException 

Comment by PM Bot [ 18/May/23 ]

Hi suiden@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

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