[CSHARP-2877] Default BsonSerializationArgs sometimes has null NominalType Created: 16/Dec/19  Updated: 28/Oct/23  Resolved: 18/Dec/19

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

Type: Bug Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-2860 custom serializer not being called Closed
is related to CSHARP-3889 System.InvalidOperationException: 'An... Closed

 Description   

When BsonSerializationArgs is not provided the default value will sometimes have a null value for the NominalType.

This can result in an undesired `_t` discriminator value.

Reproduction:

namespace ConsoleApplication
{
    public class C
    {
        public int X { get; set; }
    }
 
    public class Program
    {
        public static void Main()
        {
            var c = new C { X = 1 };
 
            using (var stringWriter = new StringWriter())
            using (var writer = new JsonWriter(stringWriter))
            {
                BsonSerializer.Serialize(writer, c);
                var json = stringWriter.ToString();
            }
        }
    }
}

Results in:

"{ \"_t\" : \"C\", \"X\" : 1 }"

But the `_t` is not supposed to be there.

 



 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 Githook User [ 18/Dec/19 ]

Author:

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

Message: CSHARP-2877: Default BsonSerializationArgs sometimes has null NominalType.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/4a636c69045ee7a4c098f6125bb8d1712d8833ee

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