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

[Breaking change]: System.Text.Json source generator no longer fall backs to reflection-based serialization #30755

Closed
1 of 2 tasks
eiriktsarpalis opened this issue Aug 19, 2022 · 1 comment · Fixed by #31132
Assignees
Labels
binary incompatible Existing binaries may encounter a breaking change in behavior. breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 7 Work items for the .NET 7 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3

Comments

@eiriktsarpalis
Copy link
Member

Description

Starting with .NET 7 Preview 7, the System.Text.Json source generator will no longer implicitly fall back to reflection-based serialization for unrecognized types, when using one of the JsonSerializer methods accepting JsonSerializerOptions.

Version

.NET 7 Preview 7

Previous behavior

Consider the following source gen example in .NET 6:

JsonSerializer.Serialize(new Poco2(), typeof(Poco2), MyContext.Default);

[JsonSerializable(typeof(Poco1))]
public partial class MyContext : JsonSerializerContext {}

public class Poco1 { }
public class Poco2 { }

Since MyContext does not include Poco2 in its serializable types, the above will fail with the following exception:

System.InvalidOperationException:

'Metadata for type 'Poco2' was not provided to the serializer. The serializer method used does not 
support reflection-based creation of serialization-related type metadata. If using source generation, 
ensure that all root types passed to the serializer have been indicated with 'JsonSerializableAttribute', 
along with any types that might be serialized polymorphically.

Note however that if we try to serialize the same type using the JsonSerializerOptions instance constructed by the source generator:

JsonSerializer.Serialize(new Poco2(), MyContext.Default.Options);

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.

New behavior

Using the above example, the statement

JsonSerializer.Serialize(new Poco2(), MyContext.Default.Options);

Should fail with the same exception as using the JsonSerializerContext overload.

The same fallback logic applies to JsonSerializerOptions.GetConverter for options instances attached to a JsonSerializerContext. The following statement

JsonConverter converter = MyContext.Default.Options.GetConverter(typeof(Poco2));

will return a converter using the built-in reflection converter. In .NET 7 this will start failing with NotSupportedException.

Type of breaking change

  • Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load/execute or different run-time behavior.
  • Source incompatible: Source code may encounter a breaking change in behavior when targeting the new runtime/component/SDK, such as compile errors or different run-time behavior.

Reason for change

We believe that the existing behavior violates the principle of least surprise and ultimately defeats the purpose of source generation. With the release of dotnet/runtime#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.

Recommended action

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 dotnet/runtime#63686:

var options = new JsonSerializerOptions
{
     TypeInfoResolver = JsonTypeInfoResolver.Combine(MyContext.Default, new DefaultJsonTypeInfoResolver());
}

JsonSerializer.Serialize(new Poco2(), options); // contract resolution falls back to the default reflection-based resolver.
options.GetConverter(typeof(Poco2)); // returns the reflection-based converter.

Feature area

Core .NET libraries

Affected APIs

No response

@eiriktsarpalis eiriktsarpalis added doc-idea Indicates issues that are suggestions for new topics [org][type][category] breaking-change Indicates a .NET Core breaking change Pri1 High priority, do before Pri2 and Pri3 labels Aug 19, 2022
@dotnet-bot dotnet-bot added 🏁 Release: .NET 7 Work items for the .NET 7 release binary incompatible Existing binaries may encounter a breaking change in behavior. ⌚ Not Triaged Not triaged labels Aug 19, 2022
@eiriktsarpalis
Copy link
Member Author

eiriktsarpalis commented Aug 19, 2022

Related to dotnet/runtime#71714.

@gewarren gewarren removed the ⌚ Not Triaged Not triaged label Aug 19, 2022
@ghost ghost added the in-pr This issue will be closed (fixed) by an active pull request. label Sep 13, 2022
@ghost ghost removed the in-pr This issue will be closed (fixed) by an active pull request. label Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
binary incompatible Existing binaries may encounter a breaking change in behavior. breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 7 Work items for the .NET 7 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants