[CSHARP-3889] System.InvalidOperationException: 'An Array value cannot be written to the root level of a BSON document.' when using MongoDB.Bson version 2.11.0 or higher Created: 05/Oct/21  Updated: 27/Oct/23  Resolved: 14/Oct/21

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

Type: Task Priority: Unknown
Reporter: John James Smith Assignee: James Kovacs
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Zip Archive BsonTest.zip    
Issue Links:
Related
related to CSHARP-2877 Default BsonSerializationArgs sometim... Closed

 Description   

Running the code in the attached project results in an exception:

System.InvalidOperationException: 'An Array value cannot be written to the root level of a BSON document.'

when using MongoDB.Bson version 2.11.0 or higher.

The issue does not reproduce when using 2.10.4 or lower.

It seems to be related to CSHARP-2877.



 Comments   
Comment by Githook User [ 14/Oct/21 ]

Author:

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

Message: CSHARP-2877 / CSHARP-3889: Added upgrade warning for 2.11.0+ as BsonSerializer.Serialize no longer allows arrays at the root of a BSON document. (#645)
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/80c7c8e10b81ae942d73ebf38317244706de124b

Comment by John James Smith [ 06/Oct/21 ]

Thank you for the response. I understand this is the expected behavior and this Jira can be closed, however, maybe this should be noted as a breaking change when upgrading from an older version, as this can result in an unexpected runtime exception.

Comment by James Kovacs [ 05/Oct/21 ]

Hi, johnjames.smith75@protonmail.com,

Thank you for reporting this issue and providing a reproduction. We have confirmed that it was introduced in 2.11.0 by CSHARP-2877. It is however correct behaviour according to the BSON Spec.

A BSON document consists of a list of field name / field value pairs. Arrays are serialized as "\x04" (array type identifier), the name of the field, and then integer keys (as strings) with array values. For example, consider the following document:

{ "someArray" : [ "red", "blue" ] } 

This would be represented in BSON as:

{ (type \x04) "someArray" : { "0" : "red", "1" : "blue" } }

The serialized value of the array on its own is ambiguous because it doesn't have a BSON type associated with it. The root of a BSON document must be a list of name/value pairs and cannot be a freestanding array.

Hopefully this explains the change in behaviour introduced by CSHARP-2877 in 2.11.0. A common practice is to wrap the value as follows:

{ "_v" : ARRAY }

We use this technique internally in the driver when the return value is an array, integer, or other simple data type.

Please let us know if you have any additional questions. If not, we will close this as "works as designed".

Sincerely,
James

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