-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Description
Describe the bug
The System.Text.Json.Serialization.JsonIgnoreAttribute
not working as expected in partial class's
ModeMetaDataType
class at ASP․NET Core 3.1.
To Reproduce
- Setup demo project
git clone https://github.com/rocfatcat/JsonIgnoreTest.git
cd JsonIgnoreTest
dotnet run
- Send HTTP GET request to the demo action
curl http://localhost:5000/api/Department
It return 500 status code and web application throw follow log message.
I expected api will return department data without instructor and course json data.
System.Text.Json.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.
For compare System.Text.Json.Serialization.JsonIgnoreAttribute
and Newtonsoft.Json.JsonIgnoreAttribue
result, i put Newtonsoft.Json example in Newtonsoft.Json_Test
branch in the same git repository.
Here is the code snippet of the demo code:
Department.cs Content
public partial class Department
{
public Department()
{
Course = new HashSet<Course>();
}
public int DepartmentId { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
public DateTime StartDate { get; set; }
public int? InstructorId { get; set; }
public byte[] RowVersion { get; set; }
public virtual Person Instructor { get; set; }
public virtual ICollection<Course> Course { get; set; }
}
Department.Partial.cs
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
...
[ModelMetadataType(typeof(DepartmentModelMetadataType))]
public partial class Department
{
...
}
public partial class DepartmentModelMetadataType
{
[JsonIgnore]
public virtual Person Instructor { get; set; }
[JsonIgnore]
public virtual ICollection<Course> Course { get; set; }
}
Further technical details
- ASP.NET Core version: 3.1
- Include the output of dotnet --info: See below
- The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version: VSCode 1.39.2
.NET Core SDK (reflecting any global.json):
Version: 3.0.101
Commit: bc5f8df0f5
Runtime Environment:
OS Name: Windows
OS Version: 10.0.17134
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.0.101\
Host (useful for support):
Version: 3.1.0
Commit: 65f04fb6db
.NET Core SDKs installed:
2.1.802 [C:\Program Files\dotnet\sdk]
3.0.101 [C:\Program Files\dotnet\sdk]
3.1.100 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download