dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.96k stars 4.65k forks source link

System.Text.Json SourceGenerator doesn't handle generic models #66882

Closed canton7 closed 2 years ago

canton7 commented 2 years ago

Description

If the model passed to the JsonSerializable attribute on a JsonSerializerContext subclass, the JsonSourceGenerator generates invalid C#.

Reproduction Steps

Create a new .NET 6 project with the contents:

public class Model<T>
{
}

[JsonSerializable(typeof(Model<>))]
internal partial class MyJsonContext : JsonSerializerContext
{
}

Expected behavior

The source generator either produces a diagnostic saying that unbound generic models aren't supported, or produces valid C#.

Actual behavior

The source generator generates the following code in MyJsonContext.GetJsonTypeInfo.g.cs:

if (type == typeof(global::Model<global::T>))
{
    return this.ModelT;
}

MyJsonContext.ModelT.g.cs also contains lots of instances of the same error, e.g.:

private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Model<global::T>>? _ModelT;

Regression?

No

Known Workarounds

None

Configuration

.NET 6, Visual Studio 17.2.0 Preview 1.0.

Other information

Similar to https://github.com/dotnet/runtime/issues/66880, but I suspect the solution is different, since we can't create a JsonTypeInfo for an unbound generic type: it might be that the SG has to raise an error in this case, and require that all generic types passed to JsonSerializable are bound.

ghost commented 2 years ago

Tagging subscribers to this area: @dotnet/area-system-text-json See info in area-owners.md if you want to be subscribed.

Issue Details
### Description If the model passed to the `JsonSerializable` attribute on a `JsonSerializerContext` subclass, the `JsonSourceGenerator` generates invalid C#. ### Reproduction Steps Create a new .NET 6 project with the contents: ```cs public class Model { } [JsonSerializable(typeof(Model<>))] internal partial class MyJsonContext : JsonSerializerContext { } ``` ### Expected behavior The source generator either produces a diagnostic saying that unbound generic models aren't supported, or produces valid C#. ### Actual behavior The source generator generates the following code in `MyJsonContext.GetJsonTypeInfo.g.cs`: ```cs if (type == typeof(global::Model)) { return this.ModelT; } ``` `MyJsonContext.ModelT.g.cs` also contains lots of instances of the same error, e.g.: ```cs private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo>? _ModelT; ``` ### Regression? No ### Known Workarounds None ### Configuration .NET 6, Visual Studio 17.2.0 Preview 1.0. ### Other information Similar to https://github.com/dotnet/runtime/issues/66880, but I suspect the solution is different, since we can't create a `JsonTypeInfo` for an unbound generic type: it might be that the SG has to raise an error in this case, and require that all generic types passed to `JsonSerializable` are bound.
Author: canton7
Assignees: -
Labels: `area-System.Text.Json`, `untriaged`
Milestone: -
eiriktsarpalis commented 2 years ago

Duplicate of #61782.