-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Remove implicit fallback to reflection-based serialization in System.Text.Json sourcegen #71714
Copy link
Copy link
Labels
area-System.Text.Jsonbreaking-changeIssue or PR that represents a breaking API or functional change over a previous release.Issue or PR that represents a breaking API or functional change over a previous release.needs-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnetBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
Milestone
Metadata
Metadata
Assignees
Labels
area-System.Text.Jsonbreaking-changeIssue or PR that represents a breaking API or functional change over a previous release.Issue or PR that represents a breaking API or functional change over a previous release.needs-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnetBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
Type
Fields
Give feedbackNo fields configured for issues without a type.
Consider the following source gen example in .NET 6:
Since
MyContextdoes not includePoco2in its serializable types, the above will fail with the following exception:Note however that if we try to serialize the same type using the
JsonSerializerOptionsinstance constructed by the source generator:The options instance will silently incorporate the default reflection-based contract resolver as a fallback mechanism, and as such the above will serialize successfully -- using reflection.
We believe that this behavior violates the principle of least surprise and ultimately defeats the purpose of source generation. With the release of #63686 users will have the ability to fine tune the sources of their contract metadata -- as such silently introducing alternative sources becomes even less desirable.
This issue proposes that we remove the fallback-to-reflection behavior. Namely the call
Should fail with the same exception as using the
JsonSerializerContextoverload.The same fallback logic applies to
JsonSerializerOptions.GetConverterfor options instances attached to aJsonSerializerContext. The following statementwill return a converter using the built-in reflection converter. In .NET 7 this will start failing with
NotSupportedException.We acknowledge that certain users might depend on the current behavior, either intentionally or unintentionally. As such, we propose the following workaround using the APIs released in #63686:
cc @krwq @layomia @jeffhandley @ericstj