Skip to content
New issue

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

The JsonSerializer.Serialize result has duplicate fields. #110260

Closed
yuxingz opened this issue Nov 29, 2024 · 2 comments
Closed

The JsonSerializer.Serialize result has duplicate fields. #110260

yuxingz opened this issue Nov 29, 2024 · 2 comments

Comments

@yuxingz
Copy link

yuxingz commented Nov 29, 2024

Description

Duplicate fields occur when serialized using JSON Polymorphic.
The type field was duplicated in the reproduction step.

Reproduction Steps

[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;
        }
    }
}

Expected behavior

var json = JsonSerializer.Serialize(model, options);
//{"person":{"type":0,"name":"Teatcher"}}
//Instead of duplicated fields

Actual behavior

Has duplicated fields
{"person":{"type":0,"type":0,"name":"Teatcher"}}

Regression?

No response

Known Workarounds

No response

Configuration

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>

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 29, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@eiriktsarpalis
Copy link
Member

Duplicate of #72170.

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants