C
C#3mo ago
Annabelle

BsonDocument exceeds the JsonSerializerOptions.MaxDepth setting

Type
[BsonCollection("product_categories")]
public class ProductCategory
{
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; } = null!;

[BsonElement("name")]
[BsonRequired]
public required string Name { get; set; }

[BsonElement("icon")]
public ImageAsset? Icon { get; set; }

[BsonElement("schema")]
[BsonExtraElements]
public required BsonDocument Schema { get; set; }

[BsonElement("is_active")]
public bool IsActive { get; set; }

[BsonElement("created_at")]
[BsonRequired]
public DateTime CreatedAt { get; set; }

[BsonElement("updated_at")]
[BsonRequired]
public DateTime UpdatedAt { get; set; }
}
[BsonCollection("product_categories")]
public class ProductCategory
{
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; } = null!;

[BsonElement("name")]
[BsonRequired]
public required string Name { get; set; }

[BsonElement("icon")]
public ImageAsset? Icon { get; set; }

[BsonElement("schema")]
[BsonExtraElements]
public required BsonDocument Schema { get; set; }

[BsonElement("is_active")]
public bool IsActive { get; set; }

[BsonElement("created_at")]
[BsonRequired]
public DateTime CreatedAt { get; set; }

[BsonElement("updated_at")]
[BsonRequired]
public DateTime UpdatedAt { get; set; }
}
running the project returns System.InvalidOperationException: The depth of the generated JSON schema exceeds the JsonSerializerOptions.MaxDepth setting. I'm using Scalar.AspNetCore for this with default MapOpenApi I tried doing as well but nothing helps.
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.MaxDepth = 256;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.MaxDepth = 256;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
3 Replies
Annabelle
AnnabelleOP3mo ago
This is related to openapi when trying to access it:
[11:11:33 ERR] HTTP GET /openapi/v1.json responded 500 in 269.2645 ms <s:Serilog.AspNetCore.RequestLoggingMiddleware>
[11:11:33 ERR] HTTP GET /openapi/v1.json responded 500 in 269.2645 ms <s:Serilog.AspNetCore.RequestLoggingMiddleware>
Ok - so I found a solution that works now. Create new class BsonJsonConverter
public class BsonJsonConverter : JsonConverter<BsonDocument>
{
public override BsonDocument? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return BsonDocument.Parse(reader.GetString());
}

public override void Write(Utf8JsonWriter writer, BsonDocument value, JsonSerializerOptions options)
{
writer.WriteRawValue(value.ToJson());
}
}
public class BsonJsonConverter : JsonConverter<BsonDocument>
{
public override BsonDocument? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return BsonDocument.Parse(reader.GetString());
}

public override void Write(Utf8JsonWriter writer, BsonDocument value, JsonSerializerOptions options)
{
writer.WriteRawValue(value.ToJson());
}
}
change where BsonDocument is to this
[BsonElement("schema")]
[JsonConverter(typeof(BsonJsonConverter))]
public required BsonDocument Schema { get; set; }
[BsonElement("schema")]
[JsonConverter(typeof(BsonJsonConverter))]
public required BsonDocument Schema { get; set; }
and in your Program.cs of your API add this
builder.Services.Configure<JsonOptions>(o =>
{
o.SerializerOptions.MaxDepth = 256;
o.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
o.SerializerOptions.Converters.Add(new BsonJsonConverter());
});
builder.Services.Configure<JsonOptions>(o =>
{
o.SerializerOptions.MaxDepth = 256;
o.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
o.SerializerOptions.Converters.Add(new BsonJsonConverter());
});
Annabelle
AnnabelleOP3mo ago
BsonDocument becomes kind of empty not really any docs for it generates, but at least it does not cause errors there is probably better way to do this
No description
Annabelle
AnnabelleOP3mo ago
Returning/saving works fine though
No description

Did you find this page helpful?