[CSHARP-3818] Bool? on .Net Core 2.1 it is not being bound on .Net Core 3.1 - Unable to cast object of type 'MongoDB.Bson.BsonBoolean' to type 'MongoDB.Bson.BsonArray'. Created: 26/Aug/21  Updated: 27/Oct/23  Resolved: 28/Aug/21

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

Type: Bug Priority: Unknown
Reporter: Patrick Ferraro Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: triage
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

macOS Catalina 10.15.7, Mongo Atlas 4.4.8


Issue Links:
Duplicate
duplicates CSHARP-3817 Bool? on .Net Core 2.1 it is not bein... Closed

 Description   

I had a project in .Net Core 2.1 that was working fine. One of my classes has a prop like:

public bool? Terms { get; set; }

Was working fine. After I upgraded to .Net Core 3.1, the old items of the collection started to throw  errors when biding to this prop.

The wierd part is that the new regs created from 3.1, dont throw any error on the bind of this prop.

The 2.1 reg and the the 3.1 has the same value on MongoDB:

"Terms" : null

For the old ones, the error thrown is:

Unable to cast object of type 'MongoDB.Bson.BsonBoolean' to type 'MongoDB.Bson.BsonArray'.



 Comments   
Comment by Boris Dogadov [ 27/Aug/21 ]

Thanks for the update patrick@boutique-pool.com, closing this ticket then.
In this case there is no need for the sample project.

Comment by Patrick Ferraro [ 27/Aug/21 ]

Hi Boris, I think it would be better to close this item I´ve opened. I'm sorry, I thought the problem would be the driver, but it's not. The driver brings the correct information normally, what is causing the problem is when the API project tries to render the object to the controller's response. I see it's a serialization issue related to System.Text.Json which is the default library of .Net Core 3.1. I switched to the old NewtonSoft and it worked perfectly.

Not being a Mongo problem or a driver, do you think it's necessary for me to send the project that can show this problem?

Comment by Boris Dogadov [ 26/Aug/21 ]

Hi patrick@boutique-pool.com
We have tried to reproduce this behaviour by serializing an object with bool? field in netcoreapp2.1 and reading back in netcoreapp3.1. That worked as expected.

Could you please provide a simple app reproducing this bug, and a full stack trace?

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