You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JsonIgnore attribute is not inherited in overridden properties #50078
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
usingSystem;usingSystem.Text.Json;usingSystem.Text.Json.Serialization;usingXunit;namespaceTest.TallyIt.JsonLibTest{internalabstractclassAnimal{publicintId{get;set;}publicstringFirstName{get;set;}publicDateTimeDob{get;set;}[JsonIgnore]publicabstractboolIsIgnored{get;set;}}internalclassCat:Animal{publicoverrideboolIsIgnored{get;set;}}publicclassJsonTests{[Fact]publicvoidTestSerialiseIntoCamelCase(){varcat=newCat(){Id=123782,Dob=newDateTime(2000,7,14),FirstName="Fluffy",IsIgnored=true};varjson=JsonSerializer.Serialize(cat);// This assert fails, I expected the JsonIgnore attribute to be derivedAssert.Equal(@"{""id"":123782,""firstName"":""Fluffy"",""dob"":""2000-07-14T00:00:00""}",json);}}}
Reacted by Ahmet Kuru, Andrew D. Bond, Jacob McDowell, Hazel K, Esther Chung, PingTKP, Mantas Klimašauskas, Maciej Walczak, Adilet S., Ivan Demchuk and 6 more
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?
@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)?
.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.
Not a solution, but it is possible to use customization as a workaround.
Something like:
voidMain(){varjsonSerializerOptions=newJsonSerializerOptions{TypeInfoResolver=newDefaultJsonTypeInfoResolver{Modifiers={InheritJsonIgnore}}};varcat=newCat(){Id=123782,Dob=newDateTime(2000,7,14),FirstName="Fluffy",IsIgnored=true};varjson=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-contractsstaticvoidInheritJsonIgnore(JsonTypeInfojsonTypeInfo){if(jsonTypeInfo.Kindis not JsonTypeInfoKind.Object)return;for(inti=0;i<jsonTypeInfo.Properties.Count;i++){if(jsonTypeInfo.Properties[i].AttributeProvideris not PropertyInfopropertyInfo)continue;if(propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>()isnull)continue;jsonTypeInfo.Properties.RemoveAt(i--);}}internalabstractclassAnimal{publicintId{get;set;}publicstringFirstName{get;set;}publicDateTimeDob{get;set;}[JsonIgnore]publicabstractboolIsIgnored{get;set;}}internalclassCat:Animal{publicoverrideboolIsIgnored{get;set;}}
Reacted by Liang Cian, Bernhard Häussermann, Jurij Bilas, AlexeyTliss, lukedukeus and Ivan Demchuk
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.
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.
Activity
eiriktsarpalis commentedon Mar 24, 2021
I can reproduce locally in .NET 5. Given that virtual properties already inherit attributes like
JsonPropertyNameit stands to reason that the same should be the case withJsonIgnore. @layomia thoughts?eiriktsarpalis commentedon Mar 24, 2021
Triage: assigning to future since not a regression and can be worked around by applying the same attribute in the derived classes.
Maximys commentedon Aug 30, 2021
@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)?
Jetski5822 commentedon May 28, 2024
Just ran in to this - any idea when in 8.X this will be worked on?
eiriktsarpalis commentedon May 28, 2024
.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.
oddbear commentedon Jun 26, 2024
Not a solution, but it is possible to use customization as a workaround.
Something like:
rmannibucau commentedon Nov 9, 2024
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.
lukedukeus commentedon May 27, 2025
After switching from Newtonsoft.Json, I was supprized to find that this also doesn't work for interfaces:
Hoping this can get fixed for .NET 10
Jusas commentedon Jun 24, 2025
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.