-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
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.
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
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.
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:
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.