-
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 crashes if context is an inner class of a generic type #66881
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescriptionIf the
Reproduction StepsCreate a .NET 6 project with the following code, and build: public class Model
{
}
public partial class Test<T>
{
[JsonSerializable(typeof(Model))]
internal partial class MyJsonContext : JsonSerializerContext
{
}
} Expected behaviorThe source generator either produces a diagnostic saying that this situation isn't supported, or correctly generates source. Actual behaviorThe source generator crashes with the exception:
Regression?No Known WorkaroundsNone Configuration.NET 6, Visual Studio 17.2.0 Preview 1.0 Other informationNo response
|
I think the only solution to this is to generate an analysis error if the class is nested. Seems like an odd design, although I can see from the use in this PR that it's test related. |
The JsonSourceGenerator has specific logic to handle the context being a nested type, so I think removing that would be a breaking change. It should be a fairly straightforward fix: you don't need to repeat generic type parameters or anything with declaration another part of a partial generic class. You just need to make sure that you repeat the same type parameter names in the same order. |
Duplicate of #62762? |
I'm not sure if it is a duplicate. #62762 is about the model being an inner type of a generic type; this issue is about the context being an inner type of a generic type. Although, the exception message is the same, so maybe it's the same underlying cause. |
Yes I believe this falls under the same general issue of the generator fumbling for open generic types. |
Description
If the
JsonSerializerContext
subclass given to the System.Text.Json source generator is a child of a generic class, the source generator crashes with the following exception, and does not generate any output:Reproduction Steps
Create a .NET 6 project with the following code, and build:
Expected behavior
The source generator either produces a diagnostic saying that this situation isn't supported, or correctly generates source.
Actual behavior
The source generator crashes with the exception:
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: