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
System.Text.Json source generator fails for covariant properties when [JsonIgnore] is specified #63443
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:
Build started...
1>------ Build started: Project: JsonTest, Configuration: Debug Any CPU ------
1>CSC : warning CS8785: Generator 'JsonSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'An item with the same key has already been added.'
1>C:\src\JsonTest\JsonTest\JsonClasses.cs(22,22,22,29): error CS0534: 'Context' does not implement inherited abstract member 'JsonSerializerContext.GeneratedSerializerOptions.get'
1>C:\src\JsonTest\JsonTest\JsonClasses.cs(22,22,22,29): error CS0534: 'Context' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'
1>Done building project "JsonTest.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Regression?
No response
Known Workarounds
No response
Configuration
.NET 6.0.101
Microsoft Windows [Version 10.0.19044.1415]
x64
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:
Modifying the Add to TryAdd would solve this issue, but what about other cases when only one of the properties has the [JsonIgnore] attribute?
publicclassIgnoredBase{[JsonIgnore]publicstringId{get;set;}}publicclassIgnoredBase_NotIgnoredDerived:IgnoredBase{publicnewstringId{get;set;}}publicclassNotIgnoredBase{publicstringId{get;set;}}publicclassNotIgnoredBase_IgnoredDerived:NotIgnoredBase{[JsonIgnore]publicnewstringId{get;set;}}NotIgnoredBasea=newNotIgnoredBase_IgnoredDerived();a.Id="Test";// Should be serialized?varb=newNotIgnoredBase_IgnoredDerived();b.Id="Test";// Should be ignored?IgnoredBasec=newIgnoredBase_NotIgnoredDerived();c.Id="Test";// Should be ignored?vard=newIgnoredBase_NotIgnoredDerived();d.Id="Test";// Should be serialized?
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
Activity
krwq commentedon Jan 14, 2022
Agreed this looks incorrect, any chance you'd be interested in contributing a fix?
Kloizdena commentedon Jan 14, 2022
Yes, I can take a look.
Kloizdena commentedon Jan 14, 2022
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:
Modifying the Add to TryAdd would solve this issue, but what about other cases when only one of the properties has the [JsonIgnore] attribute?
eiriktsarpalis commentedon Jan 24, 2022
Slightly related to #59364 which also tracks
JsonIgnoreAttributeconsistency issues.eiriktsarpalis commentedon Jan 24, 2022
More relevant, #50078
krwq commentedon May 31, 2022
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
eiriktsarpalis commentedon Jun 21, 2023
Fixed by #84756