Skip to content

JsonPropertyName inheritance perserve the parent member #51165

Open

Description

@goyzhang

When JsonPropertyName is applied to a sub-class override member, the original name in the parent class is preserved in the output of JsonSerializer.Serialize, as a result, that's two JSON field. I don't think this is by design?

Description

abstract class Person
{
    public abstract string Name { get; set; }
}
class Student : Person
{
    [JsonPropertyName("StudentName")]
    public override string Name { get; set; }
}

var json = JsonSerializer.Serialize(new Student() { Name = "Alice" });
  • Expected:
    {"StudentName":"Alice"}
  • Actual:
    {"StudentName":"Alice","Name":"Alice"}

Configuration

  • Which version of .NET is the code running on?
    .NET 5.0.2
  • What OS and version, and what distro if applicable?
    Windows 10 2004
  • What is the architecture (x64, x86, ARM, ARM64)?
    X64

Activity

  1. ghost added
    untriaged
    on Apr 13, 2021
  2. added and removed
    untriaged
    on Apr 13, 2021
  3. added this to the 6.0.0 milestone on Apr 13, 2021
  4. eiriktsarpalis commented on Apr 13, 2021

    @eiriktsarpalis
    Member

    That seems like a bug to me. FWIW I can reproduce the same behaviour when I create this combination:

    public abstract class Person
    {
        [JsonPropertyName("StudentName")]
        public virtual string Name { get; }
    }
    public class Student : Person
    {
        public override string Name => "Bob";
    }

    but the issue goes away when I remove the JsonPropertyName attribute altogether. cc @layomia

  5. devsko commented on Apr 24, 2021

    @devsko
    Contributor

    @eiriktsarpalis I can have a look. Even if it's a bug - fixing this would be a breaking change.

  6. ghost added
    in-pr
    on Apr 29, 2021
  7. layomia commented on Jul 23, 2021

    @layomia
    Contributor

    Triage - this isn't a regression from .NET 5.0 and I think it's a bit too late in the 6.0 wave to introduce a breaking change like this. We should consider in .NET 7.0.

  8. modified the milestones: 6.0.0, 7.0.0 on Jul 23, 2021
  9. ghost removed
    in-pr
    on Jul 23, 2021
  10. 2 remaining items

  11. simon10says commented on Jan 11, 2022

    @simon10says

    Is there any workaround?

  12. rogerfar commented on Jun 29, 2022

    @rogerfar

    Another bug related to this issue.

    	abstract class Person
    	{
    		[JsonPropertyName("StudentName")]
    		public abstract string Name { get; set; }
    	}
    	class Student : Person
    	{
    		public override string Name { get; set; }
    	}
    
    var json = JsonSerializer.Serialize(new Student() { Name = "Alice" });
    

    Results in 2 properties:

    {
        Name: 'Alice',
        StudentName: 'Alice'
    }
    
  13. krwq commented on Jul 7, 2022

    @krwq
    Member

    Unfortunately we won't have time to address this in 7.0 but contract customization can be used to workaround this. Moving to 8.0 in the meantime

  14. modified the milestones: 7.0.0, 8.0.0 on Jul 7, 2022
  15. modified the milestones: 8.0.0, Future on Sep 28, 2022
  16. kgbaum commented on Feb 28, 2023

    @kgbaum

    For those who may be looking for a work around until this can be addressed. You can make use of a dummy property:

    abstract class Person
    {
    	public abstract string Name { get; set; }
    }
    class Student : Person
    {
    	[JsonIgnore]
    	public override string Name { get => base.Name; set => base.Name = value; }
    	
    	public string StudentName { get => base.Name; set => base.Name = value; }
    }
    
  17. pinkfloydx33 commented on Mar 13, 2023

    @pinkfloydx33

    Just ran into this as well. I was hoping to avoid contract customization as it feels a bit heavy for this; might try the workaround posted above instead. Would be nice if it worked out of the box though

  18. removed
    Priority:1
    on Mar 13, 2023
  19. tanveerbadar commented on Feb 18, 2024

    @tanveerbadar

    This is causing source generation to generate invalid code when it interacts with JsonIgnoreCondition.WhenWritingDefault or JsonIgnoreCondition.WhenWritingNull.

    #98634

  20. keystroke commented on Feb 5, 2025

    @keystroke

    This bug still exists in .net8... produces completely invalid json.

  21. doug62 commented on Sep 16, 2025

    @doug62

    This bug still exists in .net9... produces completely invalid json.

    Jeepers - this is serious

  22. tkononowiczsolwit commented on May 29, 2026

    @tkononowiczsolwit

    Wait a minute. It has been fixed in #52036 but it wasn't merged. Why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions