-
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
System.Text.Json SourceGenerator behaves oddly if context is generic #66880
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescriptionIf the Reproduction StepsCreate a .NET 6 project containing the following, and look at the source generated by the System.Text.Json source generator: public class Model
{
}
[JsonSerializable(typeof(Model))]
internal partial class MyJsonContext<T> : JsonSerializerContext
{
} Expected behaviorThe source generator either produces a diagnostic saying that generic contexts aren't supported (and does not attempt to generate source), or the source generated is valid C#. Actual behaviorIt's a mix of things. The class declaration itself is non-generic: internal partial class MyJsonContext The default context is sort-of generic, but is confused about what a generic type parameter is: private static global::MyJsonContext<global::MyJsonContext.T>? s_defaultContext;
public static global::MyJsonContext<global::MyJsonContext.T> Default => s_defaultContext ??= new global::MyJsonContext<global::MyJsonContext.T>(new global::System.Text.Json.JsonSerializerOptions(s_defaultOptions)); The constructors are invalid C#: public MyJsonContext`1() : base(null)
{
}
public MyJsonContext`1(global::System.Text.Json.JsonSerializerOptions options) : base(options)
{
} Regression?No Known WorkaroundsNone Configuration.NET 6, Visual Studio 17.2.0 Preview 1.0. Other informationNo response
|
This is a bug since the generator is produced invalid syntax. Do you have any scenario/reason for making your context type generic? |
Same underlying issue as #62762. |
Description
If the
JsonSerializerContext
given to the System.Text.Json source generator is generic, the source generator generates some very odd code.Reproduction Steps
Create a .NET 6 project containing the following, and look at the source generated by the System.Text.Json source generator:
Expected behavior
The source generator either produces a diagnostic saying that generic contexts aren't supported (and does not attempt to generate source), or the source generated is valid C#.
Actual behavior
It's a mix of things.
The class declaration itself is non-generic:
The default context is sort-of generic, but is confused about what a generic type parameter is:
The constructors are invalid C#:
Regression?
No
Known Workarounds
None
Configuration
.NET 6, Visual Studio 17.2.0 Preview 1.0.
Other information
No response
The text was updated successfully, but these errors were encountered: