0

I have a c# class as following:

public class Product
{
    public string ProductName { get; set; }
    public int ProductCount { get; set; }
    public BsonDocument ProductMetadata { get; set; }
}

The BsonDocument is coming from the MongoDB.Driver

My api code is as following:

// GET: api/<ProductController>
[HttpGet]
public Product Get()
{
    Product prod = new Product();
    prod.ProductName = "Test";
    prod.ProductCount = 20;
    var doc = new BsonDocument
    {
        { "metadata1", "val1" }
    };
    prod.ProductMetadata = doc;
    return prod;
}

When I call the Get api, I have the following error:

An unhandled exception occurred while processing the request.
InvalidCastException: Unable to cast object of type 'MongoDB.Bson.BsonString' to type 'MongoDB.Bson.BsonBoolean'.
System.Text.Json.Serialization.Metadata.JsonPropertyInfo<T>.GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
System.Text.Json.Serialization.Converters.ObjectDefaultConverter<T>.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.TryWrite(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.Metadata.JsonPropertyInfo<T>.GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
System.Text.Json.Serialization.Converters.ObjectDefaultConverter<T>.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
....

It looks like the serializer is trying to cast the metadata1 to boolean! I don't know why... any idea? Thank you!

simoha
  • 35
  • 6
  • I think you missed providing some configuration. I doubt what you posted can generate this error. I would suggest create a simple repro in a different solution and check there – dododo Jun 02 '23 at 13:02
  • This example is from a small project that I created especially to post my question! I created a web api project, added MongoDB.Driver from nuget.org, added the Product class and the ProductController, that's all! – simoha Jun 02 '23 at 13:09
  • I pushed my project to github, you can check here: https://github.com/simoha76/TestBsonDocument – simoha Jun 02 '23 at 13:24
  • interesting, looks like a bug to me here `System.Text.Json.Serialization.JsonConverter`. – dododo Jun 02 '23 at 13:38
  • Maybe! but I wonder how the others are doing stuff like this... it's a basic serialization! – simoha Jun 02 '23 at 14:55
  • 1
    I vote that returning BsonDocument from a Get method is ***not*** too common behavior. It will work if instead, you will convert BsonDocument to json on your side before returning it to the client – dododo Jun 02 '23 at 16:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253939/discussion-between-dododo-and-simoha). – dododo Jun 03 '23 at 12:03
  • This what I did to solve that, converting it to json before sending the response... – simoha Jun 03 '23 at 14:10

0 Answers0