[CSHARP-1485] NullReferenceException when saving an ExpandoObject property Created: 23/Nov/15  Updated: 16/Nov/21  Resolved: 07/Jun/21

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.1.1
Fix Version/s: 2.13.0

Type: Bug Priority: Minor - P4
Reporter: Davide Icardi Assignee: James Kovacs
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

I have a class like:

	public class TagRecord
	{
		public Guid Id { get; set; }
		....
		public ExpandoObject ExtraData { get; set; }
	}

When I trying to insert it in MongoDb without setting ExtraData property (so leaving it to null) I get a NullReferenceException exception.
If I simply change the type to "Object" all is working fine, also when I actually save it with an ExpandoObject instance.



 Comments   
Comment by Githook User [ 07/Jun/21 ]

Author:

{'name': 'James Kovacs', 'email': 'jkovacs@post.harvard.edu', 'username': 'JamesKovacs'}

Message: CSHARP-1485: Fix ExpandoObject serialization to permit nulls. (#542)
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/d3f75f880e5e67fc268a56681acc58a07ce497e2

Comment by James Kovacs [ 03/Jun/21 ]

Hi, Brecht,

Thank you for bringing this back to our attention. I have confirmed that it is still an issue in the latest version of the driver and I have implemented a fix, which will be available in an upcoming release.

Sincerely,
James

Comment by Brecht Vanhaesebrouck [ 31/May/21 ]

I just had the same problem (with the most recent version of the driver)

Comment by Davide Icardi [ 20/Sep/16 ]

Here a very easy example to reproduce the error:

public class MyClass
{
public ExpandoObject MyProperty1

{ get; set; }

}

var myClass = new MyClass

{MyProperty1 = null}

;
var bson = myClass.ToBson(); // throw NullReferenceException

var myClass = new MyClass

{MyProperty1 = new ExpandoObject()}

;
var bson = myClass.ToBson(); // OK

Comment by Davide Icardi [ 10/Feb/16 ]

Sorry for the late response ....
It will throw NullReferenceException on DynamicDocumentBaseSerializer.Serialize. Looking at the code I cannot see any null checking before calling:

var metaObject = value.GetMetaObject(Expression.Constant(value));

A similar problem is during deserialize on the DynamicDocumentBaseSerializer.Deserialize because It doesn't handle BsonType.Null.

There is a reason for this? I have tried creating a NullableExpandoObjectSerializer and checking for null on both Serialize and Deserialize and seems to work as expected.

thanks

Comment by Craig Wilson [ 23/Nov/15 ]

Thanks for the report Davide. Could you provide a stack trace?

Craig

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