-
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 code gen: collision when two classes share same name but different namespaces #58198
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsGiven a class named [JsonSerializable(typeof(MyLibrary.Models.ClassA))]
[JsonSerializable(typeof(MyLibrary.Models.Legacy.ClassA))]
internal partial class MyJsonSerializerContext : JsonSerializerContext
{
} I get different symptoms depending on the version of VS.NET I'm using. When using VS.NET 2022
When using VS.NET 2019 Repro using System;
using System.Text.Json.Serialization;
namespace MultipleClassesWithSameName
{
public static class Program
{
public static void Main()
{
Console.WriteLine("Hello World!");
}
}
}
namespace MultipleClassesWithSameName.Utilities
{
[JsonSerializable(typeof(MultipleClassesWithSameName.Models.ClassA))]
[JsonSerializable(typeof(MultipleClassesWithSameName.Models.Legacy.ClassA))]
internal partial class MyJsonSerializerContext : JsonSerializerContext
{
}
}
namespace MultipleClassesWithSameName.Models
{
public class ClassA
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
}
}
namespace MultipleClassesWithSameName.Models.Legacy
{
public class ClassA
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
}
}
|
I can reproduce -- no workaround I can think of except renaming one of the classes or using a separate serializer context for each namespace. We should fix this. |
The workaround for these cases is to specify the source-generated property name such as:
@layomia please re-open this issue if there was an alternate plan for these, such as auto pre-pending the parent class\namespace for the conflict cases. |
I would recommend reopening this and moving to 7.0.0 -- we should not need to manually specify a property name in case of a conflict. |
I confirm the workaround proposed by @steveharter works but it sure would be nice if this was handled automatically! These are the two attributes I added to my custom context to solve the problem: [JsonSerializable(typeof(MyLibrary.Models.ClassA))]
[JsonSerializable(typeof(MyLibrary.Models.Legacy.ClassA), TypeInfoPropertyName = "LegacyClassA")] Side note: I worked around this issue by adding the following attribute to my context: [JsonSerializable(typeof(MyLibrary.Models.Legacy.ClassA[]), TypeInfoPropertyName = "LegacyClassAArray")] |
Closing as dup of #58938. The generator now issues a diagnostic if there's a type name collision:
There's no great strategy for resolving name conflicts, it is better to leave it to the developer. |
Given a class named
ClassA
in theMyLibrary.Models
namespace and another class also namedClassA
but in theMyLibrary.Models.Legacy
namespace, and given the fact that I am decorating my custom serializer context like so:I get different symptoms depending on the version of VS.NET I'm using.
When using VS.NET 2022
When using VS.NET 2019
Visual Studio does not throw any error which leads me to think, incorrectly, that everything worked successfully but in actuality only one of the two
ClassA
is processed and the generated code for the secondClassA
class is missing. As you can see in this screenshot of the generated code, only one of the twoClassA
classes has been processed:Repro
The text was updated successfully, but these errors were encountered: