Skip to content

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

Closed
@eiriktsarpalis

Description

@eiriktsarpalis

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

Metadata

Metadata

Assignees

Labels

🏁 Release: .NET 7Work items for the .NET 7 releasebinary incompatibleExisting binaries may encounter a breaking change in behavior.breaking-changeIndicates a .NET Core breaking change

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions