-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsUsing 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
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 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.
|
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. |
@Jericho I was unable to repro this on Do you have the matching SDK? Or just the runtime? In VS if I right-click on the project's which matches my runtime. |
Earlier today I upgraded to the latest daily build: 6.0.0-rc.2.21417.16 The path in the Properties window is: To answer your question about the SDK, here's the result of
I continue to experience the same two exceptions but moving the namespace to a 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 |
To see the generated code: from the solution explorer, click |
@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; }
}
} |
@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 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 I'm afraid that I may I sent us on a wild goose chase went I blamed the presence of the namespace in the |
Thanks @Jericho for the feedback. Please re-open if this re-appears. |
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):Here is the culprit causing this problem:
The problem goes away when I remove the namespace from the
JsonSerializable
attribute and I add ausing
statement. Like so: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.
The text was updated successfully, but these errors were encountered: