-
Notifications
You must be signed in to change notification settings - Fork 5k
System.Text.Json source generator does not ignore delegate properties #62354
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
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescriptionWhen using the dotnet 6 System.Text.Json source generator on a class or record that contains a delegate type (Func, Action), a CS0149 Method Name Expected error is thrown from the generated g.cs file. This happens even if using the [JsonIgnore] property. Reproduction StepsCreate a dotnet 6 project and create a new class/record with the source generator. The following code will reproduce the problem minimally: ` [JsonSerializable(typeof(TestRecord))] Expected behaviorThe expected behavior is that the g.cs files are generated without error and the [JsonIgnore] attribute is properly used for delegate types. Actual behaviorCurrently, the g.cs files are generated but throw a CS0149 Method Name Expected error. Regression?No response Known WorkaroundsOnly workaround I've used so far is to completely remove the delegate types as member properties or member variables from the class or converting them to a method (if possible for your requirements) so the source generator ignores it. Configuration
Other informationFor where the issue might be, perhaps the source generator does properly ignore the member for the generated serialization code, but it doesn't ignore it when traversing the member hierarchy to determine which classes need serialization code generated. Not sure.
|
Minimal repro: public class TestClass
{
[JsonIgnore]
Func<string, bool> Callback { get; set; }
}
[JsonSerializable(typeof(TestClass))]
public partial class TestRecordGenerationContext : JsonSerializerContext { } The source generator appears to be trying to deserialize delegates using the ObjectWithParameterizedConstructorCreator = static (args) => new global::System.Func<global::System.String, global::System.Boolean>((global::System.Object)args[0], (global::System.IntPtr)args[1]), which is what produces the compiler error. Adding |
This works with the runtime-serializer (no source gen), so that's a workaround. It seems that both metadata and fast-path generator produce the bad code. Honoring JsonIgnore here seems like a good safety mechanism. Folks should have this as a tool to workaround cases where we can't handle properties. It's similar reasoning to why we took 784687f. I'd also like us to look at why the member discovery is different between source-gen and runtime here, since even without JsonIgnore source gen tries to serialize this private property. |
Even when ignored types/properties do generate valid code, it results in very bloated deadcode that ends up in the final assembly, for instance I just encountered a JsonIgnored' X509Certificate2, which apparently can be source-generated fine, but comes with an enormous tree of deadcode for all of the various properties that that type contains. Plus the obsolete warnings due to some obsolete attributes somewhere in there. (bug #62076) |
I opened a fix for this issue - #63495.
Source gen doesn't try to serialize the property at runtime, but includes/generates metadata for the property to be inspected by the serializer at runtime so that it can handle things like property name collisions due to runtime-specified settings such as
@fbrosseau as mentioned right above, the current strategy is to generate metadata for ignored properties and their corresponding data types since this information might be needed at runtime. Can you create a separate issue for your scenario? In 7.0/future, we might be able to move more of the property resolution phase of the serializer to compile time and possibly generate less code for ignored members. |
Reopening to consider for 6.0 servicing - #63514. |
Fixed for 6.0.2 in #63514. |
Description
When using the dotnet 6 System.Text.Json source generator on a class or record that contains a delegate type (Func, Action), a CS0149 Method Name Expected error is thrown from the generated g.cs file. This happens even if using the [JsonIgnore] property.
Reproduction Steps
Create a dotnet 6 project and create a new class/record with the source generator. The following code will reproduce the problem minimally:
Expected behavior
The expected behavior is that the g.cs files are generated without error and the [JsonIgnore] attribute is properly used for delegate types.
Actual behavior
Currently, the g.cs files are generated but throw a CS0149 Method Name Expected error.
Regression?
No response
Known Workarounds
Only workaround I've used so far is to completely remove the delegate types as member properties or member variables from the class or converting them to a method (if possible for your requirements) so the source generator ignores it.
Configuration
Other information
For where the issue might be, perhaps the source generator does properly ignore the member for the generated serialization code, but it doesn't ignore it when traversing the member hierarchy to determine which classes need serialization code generated. Not sure.
The text was updated successfully, but these errors were encountered: