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

Adding JsonIgnoreAttribute to new property fails with ArgumentException #76268

Closed
Tracked by #73255
liuliang-wt opened this issue Sep 25, 2022 · 2 comments · Fixed by #84756
Closed
Tracked by #73255

Adding JsonIgnoreAttribute to new property fails with ArgumentException #76268

liuliang-wt opened this issue Sep 25, 2022 · 2 comments · Fixed by #84756

Comments

@liuliang-wt
Copy link

liuliang-wt commented Sep 25, 2022

Description

using System.Text.Json;
using System.Text.Json.Serialization;

ClassB b = new ClassB();
var test = JsonSerializer.Serialize(b);
Console.ReadLine();

public class ClassA
{
    [JsonIgnore]
    public string test { get; set; }
}

public class ClassB : ClassA
{
    [JsonIgnore]
    public new string test { get; set; }
}

The above code will throw an exception: System.ArgumentException:“An item with the same key has already been added. Key: test”

When both base class and derived class has a property with the same name (derived class using new to override property in base class), and both of this two properties marked as [JsonIgnore], Serialize function throw this exception.

@vitek-karas vitek-karas transferred this issue from dotnet/core Sep 27, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Sep 27, 2022
@ghost
Copy link

ghost commented Sep 27, 2022

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

Issue Details

Description

using System.Text.Json;
using System.Text.Json.Serialization;

ClassB b = new ClassB();
var test = JsonSerializer.Serialize(b);
Console.ReadLine();

public class ClassA
{
[JsonIgnore]
public string test { get; set; }
}

public class ClassB : ClassA
{
[JsonIgnore]
public new string test { get; set; }
}

The above code will throw an exception: System.ArgumentException:“An item with the same key has already been added. Key: test”

When both base class and derived class has a property with the same name (derived class using new to override property in base class), and both of this two properties marked as [JsonIgnore], Serialize function throw this exception.

Author: liuliang-wt
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@eiriktsarpalis eiriktsarpalis added bug and removed untriaged New issue has not been triaged by the area owner labels Sep 28, 2022
@eiriktsarpalis eiriktsarpalis added this to the 8.0.0 milestone Sep 28, 2022
@eiriktsarpalis
Copy link
Member

I can reproduce -- this belongs in the same class of bugs as #73255. This one is more interesting as it's actually failing with an exception during contract resolution.

@eiriktsarpalis eiriktsarpalis changed the title System.Text.Json.JsonSerializer.Serialize bug adding JsonIgnoreAttribute to new property fails with ArgumentException Sep 28, 2022
@eiriktsarpalis eiriktsarpalis changed the title adding JsonIgnoreAttribute to new property fails with ArgumentException Adding JsonIgnoreAttribute to new property fails with ArgumentException Sep 28, 2022
@krwq krwq modified the milestones: 8.0.0, Future Sep 28, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Apr 13, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jun 5, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants