-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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 duplicate context class names used #66851
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescriptionSystem.Text.Json.SourceGeneration.JsonSourceGenerator does not make sure that the hintNames it uses are unique. If you pass it two different
Reproduction StepsCreate a new project targetting .NET 6. Add the following code: public class Model
{
}
namespace Test
{
[JsonSerializable(typeof(Model))]
internal partial class MyJsonContext : JsonSerializerContext
{
}
}
namespace Test2
{
[JsonSerializable(typeof(Model))]
internal partial class MyJsonContext : JsonSerializerContext
{
}
} Or: public partial class Test
{
[JsonSerializable(typeof(Model))]
internal partial class MyJsonContext : JsonSerializerContext
{
}
}
public partial class Test2
{
[JsonSerializable(typeof(Model))]
internal partial class MyJsonContext : JsonSerializerContext
{
}
} Observe that JsonSourceGenerator crashes, and does not generate source. Expected behaviorJsonSourceGenerator copes with two contexts which have the same name but are in different namespaces (or are inner types of different parent types). Actual behaviorJsonSourceGenerator crashes with the following message, and does not generate source
Regression?No response Known WorkaroundsNo response Configuration.NET 6, Visual Studio 17.2.0 Preview 1.0. Other informationNo response
|
Thanks @canton7 . Are you interested in offering a PR? |
Potentially. I had the same issue reported in one of my source generators, and wondered how Microsoft were handling it. Turns out, you don't, hence this issue. I've started a conversation around changing the behaviour of Roslyn here, as almost every SG will run into this problem. It's not too bad to fix in non-incremental SGs, but the only way I can come up with of fixing it for incremental SGs is just ugly. |
Duplicate of #58198. |
Description
System.Text.Json.SourceGeneration.JsonSourceGenerator does not make sure that the hintNames it uses are unique. If you pass it two different
JsonSerializerContext
subclasses with the same name in different namespaces (or within different parent classes) it will crash, and will not generate any sources. E.g.:Reproduction Steps
Create a new project targetting .NET 6. Add the following code:
Or:
Observe that JsonSourceGenerator crashes, and does not generate source.
Expected behavior
JsonSourceGenerator copes with two contexts which have the same name but are in different namespaces (or are inner types of different parent types).
Actual behavior
JsonSourceGenerator crashes with the following message, and does not generate source
Regression?
No response
Known Workarounds
No response
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: