We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicate fields occur when serialized using JSON Polymorphic. The type field was duplicated in the reproduction step.
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")] [JsonDerivedType(typeof(Teatcher), 0)] [JsonDerivedType(typeof(Student), 1)] public interface IPerson { int Type { get; } string Name { get; } } public class Teatcher : IPerson { public int Type => 0; public string Name => "Teatcher"; } public class Student : IPerson { public int Type => 1; public string Name => "Student"; } public class Model { public IPerson? Person { get; set; } } [TestClass] public class UnitTestClass { [TestMethod] public void MyTestMethod() { try { var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var t = new Teatcher(); var model = new Model { Person = t }; var json = JsonSerializer.Serialize(model, options); //{"person":{"type":0,"type":0,"name":"Teatcher"}} Assert.IsNotNull(json); } catch (Exception ex) { throw; } } }
var json = JsonSerializer.Serialize(model, options); //{"person":{"type":0,"name":"Teatcher"}} //Instead of duplicated fields
Has duplicated fields {"person":{"type":0,"type":0,"name":"Teatcher"}}
No response
dotnet --info .NET SDK: Version: 9.0.100 Commit: 59db016f11 Workload version: 9.0.100-manifests.c6f19616 MSBuild version: 17.12.7+5b8665660 Runtime Environment: OS Name: Windows OS Version: 10.0.19044 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\9.0.100\ Host: Version: 9.0.0 Architecture: x64 Commit: 9d5a6a9aa4 .NET SDKs installed: 8.0.100 [C:\Program Files\dotnet\sdk] 9.0.100 [C:\Program Files\dotnet\sdk] .NET runtimes installed: Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation] Environment variables: Not set global.json file: Not found Learn more: https://aka.ms/dotnet/info Download .NET: https://aka.ms/dotnet/download
project.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> </Project>
The text was updated successfully, but these errors were encountered:
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.
Sorry, something went wrong.
Duplicate of #72170.
No branches or pull requests
Description
Duplicate fields occur when serialized using JSON Polymorphic.
The type field was duplicated in the reproduction step.
Reproduction Steps
Expected behavior
Actual behavior
Has duplicated fields
{"person":{"type":0,"type":0,"name":"Teatcher"}}
Regression?
No response
Known Workarounds
No response
Configuration
project.csproj
Other information
No response
The text was updated successfully, but these errors were encountered: