Search code examples
c#asp.net-coreasp.net-core-mvc

JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than


In my web API when I run project to get data from the database got this error .net core 3.1

JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.

These are my codes:

My Model

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ProductText { get; set; }
    public int ProductCategoryId { get; set; }
    [JsonIgnore]
    public virtual ProductCategory ProductCategory { get; set; }
}

My productCategory class is:

public class ProductCategory
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string CatText { get; set; }
    public string ImagePath { get; set; }
    public int Priority { get; set; }
    public int Viewd { get; set; }
    public string Description { get; set; }
    public bool Active { get; set; }
    public DateTime CreateDate { get; set; }
    public DateTime ModifyDate { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}

My repo is

public async Task<IList<Product>> GetAllProductAsync()
    {
        return await  _context.Products.Include(p => p.ProductCategory).ToListAsync(); 
    }

My interface

public interface IProductRepository
{
   ...
   Task<IList<Product>> GetAllProductAsync();
   ...
}

and this is my controller in API project

[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase
{
    private readonly IProductRepository _productRepository;

    public ProductsController(IProductRepository productRepository)
    {
        _productRepository = productRepository;
    }
    [HttpGet]
    public ActionResult Get()
    {
        return Ok(_productRepository.GetAllProduct());
    }
}

When I run API project and put this URL: https://localhost:44397/api/products I got that error, I can't resolve it.


Solution

  • this is happening because your data have a reference loop.

    e.g

    // this example creates a reference loop
    var p = new Product()
         { 
            ProductCategory = new ProductCategory() 
               { products = new List<Product>() }
         };
        p.ProductCategory.products.Add(p); // <- this create the loop
        var x = JsonSerializer.Serialize(p); // A possible object cycle was detected ...
    

    You can not handle the reference loop situation in the new System.Text.Json yet (netcore 3.1.1) unless you completely ignore a reference and its not a good idea always. (using [JsonIgnore] attribute)

    but you have two options to fix this.

    1. you can use Newtonsoft.Json in your project instead of System.Text.Json (i linked an article for you)

    2. Download the System.Text.Json preview package version 5.0.0-alpha.1.20071.1 from dotnet5 gallery (through Visual Studio's NuGet client):

    option 1 usage:

    services.AddMvc()
         .AddNewtonsoftJson(
              options => {
               options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; 
          });
    // if you not using .AddMvc use these methods instead 
    //services.AddControllers().AddNewtonsoftJson(...);
    //services.AddControllersWithViews().AddNewtonsoftJson(...);
    //services.AddRazorPages().AddNewtonsoftJson(...);
    

    option 2 usage:

    // for manual serializer
    var options = new JsonSerializerOptions
    {
        ReferenceHandling = ReferenceHandling.Preserve
    };
    
    string json = JsonSerializer.Serialize(objectWithLoops, options);
    
    // -----------------------------------------
    // for asp.net core 3.1 (globaly)
     services.AddMvc()
      .AddJsonOptions(o => {
         o.JsonSerializerOptions
           .ReferenceHandling = ReferenceHandling.Preserve  
                });
    

    these serializers have ReferenceLoopHandling feature.

    • Edit : ReferenceHandling changed to ReferenceHandler in DotNet 5

    but if you decide to just ignore one reference use [JsonIgnore] on one of these properties. but it causes null result on your API response for that field even when you don't have a reference loop.

    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ProductText { get; set; }
        
        public int ProductCategoryId { get; set; }
        // [JsonIgnore] HERE or
        public virtual ProductCategory ProductCategory { get; set; }
    }
    
    public class ProductCategory
    {
        public int Id { get; set; }
        // [JsonIgnore] or HERE
        public ICollection<Product> products {get;set;}
    }