Skip to content
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 source generator fails with two nested classes of the same name #75926

Closed
CollinAlpert opened this issue Sep 20, 2022 · 2 comments

Comments

@CollinAlpert
Copy link
Contributor

Description

The SG for the JsonSerializerContext fails to generate if multiple classes specified by the JsonSerializableAttribute have nested classes with the same name.

Reproduction Steps

using System.Text.Json.Serialization;

namespace Test;

public class CreateTicketRequest
{
    public IReadOnlyList<Model> Models { get; set; }

    public class Model
    {
    }
}

public class UpdateTicketRequest
{
    public IReadOnlyList<Model> Models { get; set; }
	
    public class Model
    {
    }
}



[JsonSerializable(typeof(CreateTicketRequest))]
[JsonSerializable(typeof(UpdateTicketRequest))]
partial class JsonContext : JsonSerializerContext
{
}

Expected behavior

The project compiles and JsonTypeInfo is generated for all classes.

Actual behavior

The build fails with Argument 2: cannot convert from 'System.Collections.Generic.IReadOnlyList<Test.UpdateTicketRequest.Model>' to 'System.Collections.Generic.IReadOnlyList<Test.CreateTicketRequest.Model>?'

Regression?

I reproduced this in .NET 6 as well as .NET 7 RC1.

Known Workarounds

Rename one of the nested classes.

Configuration

.NET 7.0.100-rc.1.22431.12
macOS 12.5.1 with Apple Silicon

Other information

I found #72671 which might have something to do with this, however my usage and error message is different.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Sep 20, 2022
@ghost
Copy link

ghost commented Sep 20, 2022

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

Issue Details

Description

The SG for the JsonSerializerContext fails to generate if multiple classes specified by the JsonSerializableAttribute have nested classes with the same name.

Reproduction Steps

using System.Text.Json.Serialization;

namespace Test;

public class CreateTicketRequest
{
    public IReadOnlyList<Model> Models { get; set; }

    public class Model
    {
    }
}

public class UpdateTicketRequest
{
    public IReadOnlyList<Model> Models { get; set; }
	
    public class Model
    {
    }
}



[JsonSerializable(typeof(CreateTicketRequest))]
[JsonSerializable(typeof(UpdateTicketRequest))]
partial class JsonContext : JsonSerializerContext
{
}

Expected behavior

The project compiles and JsonTypeInfo is generated for all classes.

Actual behavior

The build fails with Argument 2: cannot convert from 'System.Collections.Generic.IReadOnlyList<Test.UpdateTicketRequest.Model>' to 'System.Collections.Generic.IReadOnlyList<Test.CreateTicketRequest.Model>?'

Regression?

I reproduced this in .NET 6 as well as .NET 7 RC1.

Known Workarounds

Rename one of the nested classes.

Configuration

.NET 7.0.100-rc.1.22431.12
macOS 12.5.1 with Apple Silicon

Other information

I found #72671 which might have something to do with this, however my usage and error message is different.

Author: CollinAlpert
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@eiriktsarpalis
Copy link
Member

Duplicate of #58198. Per feedback in #58198 (comment) you can use the JsonSerializableAttribute.TypeInfoPropertyName property to resolve any naming conflicts.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Sep 21, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Oct 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants