Skip to content

JsonIgnore attribute is not inherited in overridden properties #50078

Open

Description

@jmcginty

Description

Marking a property in an abstract class as [JsonIgnore] does not get inherited when the property is overridden

Configuration

  • Which version of .NET is the code running on? .net Core 3.1

Other information

Example test code to show the problem

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

namespace Test.TallyIt.JsonLibTest
{
    internal abstract class Animal
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public DateTime Dob { get; set; }
        [JsonIgnore]
        public abstract bool IsIgnored { get; set; }
    }

    internal class Cat : Animal
    {
        public override bool IsIgnored { get; set; }
    }

    public class JsonTests
    {
        [Fact]
        public void TestSerialiseIntoCamelCase()
        {
            var cat = new Cat()
            {
                Id = 123782,
                Dob = new DateTime(2000, 7, 14),
                FirstName = "Fluffy",
                IsIgnored = true
            };

            var json = JsonSerializer.Serialize(cat);

            // This assert fails, I expected the JsonIgnore attribute to be derived
            Assert.Equal(@"{""id"":123782,""firstName"":""Fluffy"",""dob"":""2000-07-14T00:00:00""}", json);
        }
    }
}

Activity

  1. ghost added
    untriaged
    on Mar 23, 2021
  2. eiriktsarpalis commented on Mar 24, 2021

    @eiriktsarpalis
    Member

    I can reproduce locally in .NET 5. Given that virtual properties already inherit attributes like JsonPropertyName it stands to reason that the same should be the case with JsonIgnore. @layomia thoughts?

  3. added and removed
    untriaged
    on Mar 24, 2021
  4. added this to the Future milestone on Mar 24, 2021
  5. eiriktsarpalis commented on Mar 24, 2021

    @eiriktsarpalis
    Member

    Triage: assigning to future since not a regression and can be worked around by applying the same attribute in the derived classes.

  6. Maximys commented on Aug 30, 2021

    @Maximys
    Contributor

    @layomia , I think this bug is linked with 51165. Can you get me possibility to fix they on my free time (I hope, on Saturday or Sunday of current week)?

  7. modified the milestones: Future, 7.0.0 on Sep 1, 2021
  8. modified the milestones: 7.0.0, Future on Jul 10, 2022
  9. modified the milestones: Future, 8.0.0 on Aug 1, 2022
  10. modified the milestones: 8.0.0, Future on Sep 28, 2022
  11. Jetski5822 commented on May 28, 2024

    @Jetski5822

    Just ran in to this - any idea when in 8.X this will be worked on?

  12. eiriktsarpalis commented on May 28, 2024

    @eiriktsarpalis
    Member

    .NET 8 has already shipped - we don't backport bugfixes to shipped versions of .NET unless they are regressions or security related bugs. In terms when this will get fixed in general, I can say with certainty that a fix is not in the cards for .NET 9.

  13. oddbear commented on Jun 26, 2024

    @oddbear

    Not a solution, but it is possible to use customization as a workaround.
    Something like:

    void Main()
    {
        var jsonSerializerOptions = new JsonSerializerOptions
        {
            TypeInfoResolver = new DefaultJsonTypeInfoResolver
            {
                Modifiers = { InheritJsonIgnore }
            }
        };
    
        var cat = new Cat()
        {
            Id = 123782,
            Dob = new DateTime(2000, 7, 14),
            FirstName = "Fluffy",
            IsIgnored = true
        };
    
        var json = JsonSerializer.Serialize(cat, jsonSerializerOptions);
        json.Dump(); // {"Id":123782,"FirstName":"Fluffy","Dob":"2000-07-14T00:00:00"}
    }
    
    // Modified example from: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/custom-contracts
    static void InheritJsonIgnore(JsonTypeInfo jsonTypeInfo)
    {
        if (jsonTypeInfo.Kind is not JsonTypeInfoKind.Object)
            return;
        
        for (int i = 0; i < jsonTypeInfo.Properties.Count; i++)
        {
            if (jsonTypeInfo.Properties[i].AttributeProvider is not PropertyInfo propertyInfo)
                continue;
    
            if (propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>() is null)
                continue;
    
            jsonTypeInfo.Properties.RemoveAt(i--);
        }
    }
    
    internal abstract class Animal
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public DateTime Dob { get; set; }
        [JsonIgnore]
        public abstract bool IsIgnored { get; set; }
    }
    
    internal class Cat : Animal
    {
        public override bool IsIgnored { get; set; }
    }
  14. rmannibucau commented on Nov 9, 2024

    @rmannibucau

    Same there, the API is flagged as inherited but the impl is not which is bothering cause it means you can leak a parent state whereas the child doesn't always have to care about it so hope .NET 9 gets it fixed since it doesn't look crazy as fix.

  15. lukedukeus commented on May 27, 2025

    @lukedukeus

    After switching from Newtonsoft.Json, I was supprized to find that this also doesn't work for interfaces:

    internal interface Animal
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public DateTime Dob { get; set; }
        [JsonIgnore]
        public abstract bool IsIgnored { get; set; }
    }
    
    internal class Cat : Animal
    {
        public override bool IsIgnored { get; set; }
    }
    
    Cat cat = new Cat() {Id = 0; FirstName = "Garfield", Dob = DateTime.Parse("Jun 19, 1978"), IsIgnored = false};
    string json = JsonSerializer.Serialize(cat); // json still includes the IsIgnored prop!
    

    Hoping this can get fixed for .NET 10

  16. Jusas commented on Jun 24, 2025

    @Jusas

    Amazing, how is this still open?
    Coming from Newtonsoft.Json the amount of walls I seem to have to climb over just to get rid of one dependency is getting ridiculous.

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