-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
JsonIgnore attribute is not inherited in overridden properties #50078
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsDescriptionMarking a property in an abstract class as [JsonIgnore] does not get inherited when the property is overridden Configuration
Other informationExample test code to show the problem
|
I can reproduce locally in .NET 5. Given that virtual properties already inherit attributes like |
Triage: assigning to future since not a regression and can be worked around by applying the same attribute in the derived classes. |
Just ran in to this - any idea when in 8.X this will be worked on? |
.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. 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; }
} |
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. |
Description
Marking a property in an abstract class as [JsonIgnore] does not get inherited when the property is overridden
Configuration
Other information
Example test code to show the problem
The text was updated successfully, but these errors were encountered: