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 SourceGenerator crashes if context is an inner class of a generic type #66881

Closed
canton7 opened this issue Mar 19, 2022 · 6 comments
Labels
area-System.Text.Json source-generator Indicates an issue with a source generator feature

Comments

@canton7
Copy link

canton7 commented Mar 19, 2022

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:

Warning CS8785: Generator 'JsonSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentOutOfRangeException' with message 'Length cannot be less than zero.

Reproduction Steps

Create 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 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:

ArgumentOutOfRangeException with message 'Length cannot be less than zero.'

Regression?

No

Known Workarounds

None

Configuration

.NET 6, Visual Studio 17.2.0 Preview 1.0

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Mar 19, 2022
@ghost
Copy link

ghost commented Mar 19, 2022

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

Issue Details

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:

Warning CS8785: Generator 'JsonSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentOutOfRangeException' with message 'Length cannot be less than zero.

Reproduction Steps

Create 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 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:

ArgumentOutOfRangeException with message 'Length cannot be less than zero.'

Regression?

No

Known Workarounds

None

Configuration

.NET 6, Visual Studio 17.2.0 Preview 1.0

Other information

No response

Author: canton7
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@SteveDunn
Copy link
Contributor

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.

@canton7
Copy link
Author

canton7 commented Mar 19, 2022

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.

@AaronRobinsonMSFT AaronRobinsonMSFT added the source-generator Indicates an issue with a source generator feature label Mar 20, 2022
@stephentoub
Copy link
Member

stephentoub commented Mar 24, 2022

Duplicate of #62762?

@stephentoub stephentoub marked this as a duplicate of #62762 Mar 24, 2022
@canton7
Copy link
Author

canton7 commented Mar 24, 2022

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.

@layomia
Copy link
Contributor

layomia commented Mar 30, 2022

Yes I believe this falls under the same general issue of the generator fumbling for open generic types.

@layomia layomia closed this as completed Mar 30, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Mar 30, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Apr 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

No branches or pull requests

5 participants