You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.Text.Json source generator does not ignore delegate properties #62354
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:
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
Which version of .NET is the code running on? dotnet 6.0
What OS and version, and what distro if applicable? Windows 10 64-bit
What is the architecture (x64, x86, ARM, ARM64)? x64
Do you know whether it is specific to that configuration? Unsure
If you're using Blazor, which web browser(s) do you see this issue in? I am using Blazor, but this isn't a runtime issue
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.
which is what produces the compiler error. Adding JsonIgnoreAttribute doesn't appear to suppress generation from the time. This suggests to me that this is a bug we might want to service in 6.
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)
ghost added
in-prThere is an active PR which will close this issue when it is merged
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.
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 options.PropertyNamingPolicy.
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.
@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.
Activity
eiriktsarpalis commentedon Dec 6, 2021
Minimal repro:
The source generator appears to be trying to deserialize delegates using the
Func(System.Object, IntPtr)constructor:which is what produces the compiler error. Adding
JsonIgnoreAttributedoesn't appear to suppress generation from the time. This suggests to me that this is a bug we might want to service in 6.ericstj commentedon Dec 6, 2021
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.
fbrosseau commentedon Dec 31, 2021
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)
layomia commentedon Jan 7, 2022
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
options.PropertyNamingPolicy.@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.
layomia commentedon Jan 8, 2022
Reopening to consider for 6.0 servicing - #63514.
layomia commentedon Jan 10, 2022
Fixed for 6.0.2 in #63514.