I have a webapi project that written using C#
and built on the top of ASP.NET Core/5 framework.
The API return JSON data.
Here is an example of one endpoint
[HttpGet]
public async Task<ActionResult<TModel[]>> QueryAsync(
[FromQuery(Name = "filter")] string filter = null,
[FromQuery(Name = "expand")] string expand = null)
{
IQueryable<TModel> query = GetQuery(filter, expand);
var models = await query.ToArrayAsync();
return Ok(models);
}
In the above request, the expand
will tell the server which navigation properties to expand. When the user request to expand a property, I get the following error
A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles
Following the instruction from the error, I added the following to the Startup class
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
// added this
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
The above change did fix the problem! However, it introduced another problem. The new problem is that it changed the structure of the output.
The output went from something like this
{
// properties
"relations": [
{
// relation 1
"parent": null
},
{
// relation 2
"parent": null
}
]
}
to something like this
{
"$id": "1",
// properties
"relations": {
"$id": "2",
"$values": [
{
// relation 1 properties
"$id": "3",
"parent": {
"$ref": 1
}
},
{
// relation 2 properties
"$id": "3",
"parent": {
"$ref": 1
}
}
]
}
}
Is there a way to not change the structure of the output and instead ignore the circular reference?