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 behaves oddly if context is generic #66880

Closed
canton7 opened this issue Mar 19, 2022 · 3 comments
Closed

System.Text.Json SourceGenerator behaves oddly if context is generic #66880

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

Comments

@canton7
Copy link

canton7 commented Mar 19, 2022

Description

If the JsonSerializerContext given to the System.Text.Json source generator is generic, the source generator generates some very odd code.

Reproduction Steps

Create a .NET 6 project containing the following, and look at the source generated by the System.Text.Json source generator:

public class Model
{
}

[JsonSerializable(typeof(Model))]
internal partial class MyJsonContext<T> : JsonSerializerContext
{
}

Expected behavior

The source generator either produces a diagnostic saying that generic contexts aren't supported (and does not attempt to generate source), or the source generated is valid C#.

Actual behavior

It's a mix of things.

The class declaration itself is non-generic:

internal partial class MyJsonContext

The default context is sort-of generic, but is confused about what a generic type parameter is:

        private static global::MyJsonContext<global::MyJsonContext.T>? s_defaultContext;
        public static global::MyJsonContext<global::MyJsonContext.T> Default => s_defaultContext ??= new global::MyJsonContext<global::MyJsonContext.T>(new global::System.Text.Json.JsonSerializerOptions(s_defaultOptions));

The constructors are invalid C#:

        public MyJsonContext`1() : base(null)
        {
        }
        
        public MyJsonContext`1(global::System.Text.Json.JsonSerializerOptions options) : base(options)
        {
        }

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 given to the System.Text.Json source generator is generic, the source generator generates some very odd code.

Reproduction Steps

Create a .NET 6 project containing the following, and look at the source generated by the System.Text.Json source generator:

public class Model
{
}

[JsonSerializable(typeof(Model))]
internal partial class MyJsonContext<T> : JsonSerializerContext
{
}

Expected behavior

The source generator either produces a diagnostic saying that generic contexts aren't supported (and does not attempt to generate source), or the source generated is valid C#.

Actual behavior

It's a mix of things.

The class declaration itself is non-generic:

internal partial class MyJsonContext

The default context is sort-of generic, but is confused about what a generic type parameter is:

        private static global::MyJsonContext<global::MyJsonContext.T>? s_defaultContext;
        public static global::MyJsonContext<global::MyJsonContext.T> Default => s_defaultContext ??= new global::MyJsonContext<global::MyJsonContext.T>(new global::System.Text.Json.JsonSerializerOptions(s_defaultOptions));

The constructors are invalid C#:

        public MyJsonContext`1() : base(null)
        {
        }
        
        public MyJsonContext`1(global::System.Text.Json.JsonSerializerOptions options) : base(options)
        {
        }

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

@AaronRobinsonMSFT AaronRobinsonMSFT added the source-generator Indicates an issue with a source generator feature label Mar 20, 2022
@layomia layomia added bug and removed untriaged New issue has not been triaged by the area owner labels Mar 30, 2022
@layomia layomia added this to the 7.0.0 milestone Mar 30, 2022
@layomia
Copy link
Contributor

layomia commented Mar 30, 2022

This is a bug since the generator is produced invalid syntax. Do you have any scenario/reason for making your context type generic?

@layomia
Copy link
Contributor

layomia commented Mar 30, 2022

Same underlying issue as #62762.

@layomia layomia closed this as completed 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 bug source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

No branches or pull requests

3 participants