-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
System.Text.Json source generator fails for covariant properties when [JsonIgnore] is specified #63443
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescriptionI have a class hierarchy where the derived class overrides one of the properties using covariant return type. I want to omit this property from the JSON, so I added [JsonIgnore] attributes to both properties: public class A
{
[JsonIgnore]
public virtual object Id { get; }
}
public class GenericB<T> : A
where T : class
{
[JsonIgnore]
public override T Id { get; }
} When I try to use the Json source generator for the derived type, it fails. I tried without generics, but it also fails. Reproduction StepsAdd the following classes to a .NET 6 project and try to build it: using System.Text.Json.Serialization;
public class A
{
[JsonIgnore]
public virtual object Id { get; }
}
public class B : A
{
[JsonIgnore]
public override string Id { get; }
}
public class GenericB<T> : A
where T : class
{
[JsonIgnore]
public override T Id { get; }
}
[JsonSerializable(typeof(B))]
[JsonSerializable(typeof(GenericB<string>))]
public partial class Context : JsonSerializerContext
{
} Expected behaviorThe project should build without errors. Actual behaviorThe source generator fails with an error:
Regression?No response Known WorkaroundsNo response Configuration.NET 6.0.101 Other informationNo response
|
Agreed this looks incorrect, any chance you'd be interested in contributing a fix? |
Yes, I can take a look. |
The exception is thrown at this line: runtime/src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs Lines 223 to 226 in 5626066
Based on this, I tested a similar case and found that the generator runs into the same problem when there is no covariance involved, only property hiding without overriding: public class IgnoredBase
{
[JsonIgnore]
public string Id { get; set; }
}
public class IgnoredBase_IgnoredDerived : IgnoredBase
{
[JsonIgnore]
public new string Id { get; set; }
} Modifying the Add to TryAdd would solve this issue, but what about other cases when only one of the properties has the [JsonIgnore] attribute? public class IgnoredBase
{
[JsonIgnore]
public string Id { get; set; }
}
public class IgnoredBase_NotIgnoredDerived : IgnoredBase
{
public new string Id { get; set; }
}
public class NotIgnoredBase
{
public string Id { get; set; }
}
public class NotIgnoredBase_IgnoredDerived : NotIgnoredBase
{
[JsonIgnore]
public new string Id { get; set; }
}
NotIgnoredBase a = new NotIgnoredBase_IgnoredDerived();
a.Id = "Test"; // Should be serialized?
var b = new NotIgnoredBase_IgnoredDerived();
b.Id = "Test"; // Should be ignored?
IgnoredBase c = new IgnoredBase_NotIgnoredDerived();
c.Id = "Test"; // Should be ignored?
var d = new IgnoredBase_NotIgnoredDerived();
d.Id = "Test"; // Should be serialized? |
Slightly related to #59364 which also tracks |
More relevant, #50078 |
As per #63823 (comment) we need to look at this problem holistically and write down all scenarios and their expected behavior before we start making any fixes |
Fixed by #84756 |
Description
I have a class hierarchy where the derived class overrides one of the properties using covariant return type. I want to omit this property from the JSON, so I added [JsonIgnore] attributes to both properties:
When I try to use the Json source generator for the derived type, it fails. I tried without generics, but it also fails.
Reproduction Steps
Add the following classes to a .NET 6 project and try to build it:
Expected behavior
The project should build without errors.
Actual behavior
The source generator fails with an error:
Regression?
No response
Known Workarounds
No response
Configuration
.NET 6.0.101
Microsoft Windows [Version 10.0.19044.1415]
x64
Other information
No response
The text was updated successfully, but these errors were encountered: