-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
System.Text.Json code generation fails on recursive IReadOnlyDictionary #76802
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionSystem.Text.Json code generation fails with Reproduction StepsInclude these two classes in a C# library or console app that references System.Text.Json 6.0.6: public class Dto
{
public IReadOnlyDictionary<string, Dto>? Data { get; set; }
}
[JsonSerializable(typeof(Dto))]
public partial class JsonContext : JsonSerializerContext
{
} Expected behaviorCompilation fails with: That badly generated line of code looks like this: Actual behaviorIt should succeed without error, as it does if Regression?No response Known WorkaroundsUse Configuration.NET 6.0.401 on Windows x64. Other informationNo response
|
I can repro the issue -- fundamentally the issue stems from the fact that the source generator's "parser" component eagerly evaluates properties of the recursive type, even though it hasn't been fully populated yet: runtime/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs Lines 957 to 960 in 957f76f
I suspect that this issue has the potential to surface in other cases as well, so in fixing this we should carefully audit the parser code for any other instances where it could be reading from uninitialized metadata, perhaps by making such scenaria impossible by construction. |
Description
System.Text.Json code generation fails with
error CS1031: Type expected
if the serializable class has anIReadOnlyDictionary
whose value type is that same serializable class.Reproduction Steps
Include these two classes in a C# library or console app that references System.Text.Json 6.0.6:
Expected behavior
Compilation fails with:
...\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonContext.IReadOnlyDictionaryStringDto.g.cs(27,128,27,129): error CS1031: Type expected
That badly generated line of code looks like this:
ObjectCreator = () => new global::System.Collections.Generic.Dictionary<global::System.String, >(),
Actual behavior
It should succeed without error, as it does if
Dictionary
is used instead ofIReadOnlyDictionary
.Regression?
No response
Known Workarounds
Use
Dictionary
instead ofIReadOnlyDictionary
.Configuration
.NET 6.0.401 on Windows x64.
Other information
No response
The text was updated successfully, but these errors were encountered: