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

JSON source generator fails to compile when providing the full name of serializable class #57579

Closed
Jericho opened this issue Aug 17, 2021 · 9 comments

Comments

@Jericho
Copy link

Jericho commented Aug 17, 2021

Using latest daily version of System.Text.Json (6.0.0-rc.1.21417.4), I get the following two exceptions when my custom serializer context is decorated with a JsonSerializable attribute and I specify the full name of a class (including namespace):

CS0534 'MyJsonSerializerContext' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'
CS7036 There is no argument given that corresponds to the required formal parameter 'instanceOptions' of 'JsonSerializerContext.JsonSerializerContext(JsonSerializerOptions?, JsonSerializerOptions?)'

Here is the culprit causing this problem:

namespace MyLibrary.Utilities
{
    [JsonSerializable(typeof(MyLibrary.Models.MyClass))]
    internal partial class MyJsonSerializerContext : JsonSerializerContext
    {
    }
}

The problem goes away when I remove the namespace from the JsonSerializable attribute and I add a using statement. Like so:

using MyLibrary.Models;

namespace MyLibrary.Utilities
{
    [JsonSerializable(typeof(MyClass))]
    internal partial class MyJsonSerializerContext : JsonSerializerContext
    {
    }
}

The symptoms appear to be exactly the same as described by @stephentoub in #56920, and the root cause is somewhat similar (it has to do with namespace) but not exactly the same.

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

ghost commented Aug 17, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

Using latest daily version of System.Text.Json (6.0.0-rc.1.21417.4), I get the following two exceptions when my custom serializer context is decorated with a JsonSerializable attribute and I specify the full name of a class (including namespace):

CS0534 'MyJsonSerializerContext' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'
CS7036 There is no argument given that corresponds to the required formal parameter 'instanceOptions' of 'JsonSerializerContext.JsonSerializerContext(JsonSerializerOptions?, JsonSerializerOptions?)'

Here is the culprit causing this problem:

namespace MyLibrary.Utilities
{
    [JsonSerializable(typeof(MyLibrary.Models.MyClass))]
    internal partial class MyJsonSerializerContext : JsonSerializerContext
    {
    }
}

The problem goes away when I remove the namespace from the JsonSerializable attribute and I add a using statement. Like so:

using MyLibrary.Models;

namespace MyLibrary.Utilities
{
    [JsonSerializable(typeof(MyClass))]
    internal partial class MyJsonSerializerContext : JsonSerializerContext
    {
    }
}

The symptoms appear to be exactly the same as described by @stephentoub in #56920, and the root cause is somewhat similar (it has to do with namespace) but not exactly the same.

Author: Jericho
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@Jericho
Copy link
Author

Jericho commented Aug 17, 2021

Please note that I was using VS2022 Preview 3.1 at the time I experienced this problem. I switched to VS2019 version 16.11.1 and I am no longer able to reproduce the problem.

@eiriktsarpalis
Copy link
Member

Related to #57516 and #57378.

@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Aug 17, 2021
@eiriktsarpalis eiriktsarpalis added this to the 6.0.0 milestone Aug 17, 2021
@steveharter
Copy link
Member

@Jericho I was unable to repro this on 6.0.0-rc.1.21414.4 which is actually older than your 6.0.0-rc.1.21417.4.

Do you have the matching SDK? Or just the runtime?

In VS if I right-click on the project's Dependencies, then select Analyzers and System.Text.Json.SourceGeneration I get this in the property window for Path:
C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\6.0.0-rc.1.21414.4\analyzers/dotnet/cs/System.Text.Json.SourceGeneration.dll

which matches my runtime.

@Jericho
Copy link
Author

Jericho commented Aug 18, 2021

Earlier today I upgraded to the latest daily build: 6.0.0-rc.2.21417.16

The path in the Properties window is: C:\Users\desau\.nuget\packages\system.text.json\6.0.0-rc.2.21417.16\analyzers\dotnet\cs\System.Text.Json.SourceGeneration.dll

To answer your question about the SDK, here's the result of dotnet --info:

.NET SDK (reflecting any global.json):
 Version:   5.0.400
 Commit:    d61950f9bf

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22000
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.400\

Host (useful for support):
  Version: 6.0.0-preview.7.21377.19
  Commit:  91ba01788d

.NET SDKs installed:
  2.1.816 [C:\Program Files\dotnet\sdk]
  3.1.302 [C:\Program Files\dotnet\sdk]
  3.1.400 [C:\Program Files\dotnet\sdk]
  3.1.411 [C:\Program Files\dotnet\sdk]
  5.0.103 [C:\Program Files\dotnet\sdk]
  5.0.104 [C:\Program Files\dotnet\sdk]
  5.0.205 [C:\Program Files\dotnet\sdk]
  5.0.302 [C:\Program Files\dotnet\sdk]
  5.0.400 [C:\Program Files\dotnet\sdk]
  6.0.100-preview.7.21379.14 [C:\Program Files\dotnet\sdk]

I continue to experience the same two exceptions but moving the namespace to a using statement no longer seem to resolve the issue. I wonder if it was simply a coincidence that the exceptions went away yesterday around the same time I removed the namespace from the JsonSerializable attribute.

Does the source generator produce some log and/or is it possible to review the source generated? Maybe this could help me learn more about why I'm getting the CS0534 and the CS7036 exceptions.

@steveharter
Copy link
Member

Does the source generator produce some log and/or is it possible to review the source generated? Maybe this could help me learn more about why I'm getting the CS0534 and the CS7036 exceptions.

To see the generated code: from the solution explorer, click Dependencies, then Analyzers and System.Text.Json.SourceGeneration. The generated code will exist under that.

@steveharter
Copy link
Member

@Jericho can you provide a stand-alone repro? Do you have two assemblies?

Here's what I tried but it doesn't repro:

using System.Text.Json;
using System.Text.Json.Serialization;

Console.WriteLine(JsonSerializer.Serialize(new MyLibrary.Models.MyClass(), MyLibrary.Utilities.MyJsonSerializerContext.Default.MyClass));

namespace MyLibrary.Utilities
{
    [JsonSerializable(typeof(MyLibrary.Models.MyClass))]
    internal partial class MyJsonSerializerContext : JsonSerializerContext
    {
    }
}

namespace MyLibrary.Models
{
    public class MyClass
    {
        public string Name { get; set; }
    }
}

@Jericho
Copy link
Author

Jericho commented Aug 20, 2021

@steveharter thanks for looking into this. I was able to consistently reproduce until I switched to a different version of VS at which point I was no longer able to consistently reproduce. I doubt the version of VS (correct me if I'm wrong) would have an impact on this problem though. It's probably more likely that changing version of VS had the side effect of clearing a cache of generated code (or something along those lines). I was probably mislead to believe that move the namespace to a using statement resolved the issue when in fact it probably had nothing to do with it.

Having said all this, I was consistently getting the CS0534 and the CS7036 exceptions and I was struggling to diagnose the root cause. Thanks for pointing out where I could review the generated code but unfortunately it didn't help me in my investigation.

This morning I upgraded to System.Text.Json 6.0.0-rc.2.21420.10 and the two exceptions magically went away!

I'm afraid that I may I sent us on a wild goose chase went I blamed the presence of the namespace in the JsonSerializable. The good news is that this problem seems to have been taken care of in the latest daily build so I think it's safe to close this issue.

@steveharter
Copy link
Member

Thanks @Jericho for the feedback. Please re-open if this re-appears.

@ghost ghost locked as resolved and limited conversation to collaborators Sep 19, 2021
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

3 participants