diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfo.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfo.cs index 32b72459e92bc..486fffd61f59f 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfo.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfo.cs @@ -243,7 +243,7 @@ internal void EnsureConfigured() _isConfigured = true; } - internal void Configure() + internal virtual void Configure() { Debug.Assert(ParentTypeInfo != null, "We should have ensured parent is assigned in JsonTypeInfo"); DeclaringTypeNumberHandling = ParentTypeInfo.NumberHandling; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfoOfT.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfoOfT.cs index 82f0a3214bffe..8176763ce7bd3 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfoOfT.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfoOfT.cs @@ -15,6 +15,14 @@ namespace System.Text.Json.Serialization.Metadata /// or a type's converter, if the current instance is a . internal sealed class JsonPropertyInfo : JsonPropertyInfo { + /// + /// Returns true if the property's converter is external (a user's custom converter) + /// and the type to convert is not the same as the declared property type (polymorphic). + /// Used to determine whether to perform additional validation on the value returned by the + /// converter on deserialization. + /// + private bool _converterIsExternalAndPolymorphic; + // Since a converter's TypeToConvert (which is the T value in this type) can be different than // the property's type, we track that and whether the property type can be null. private readonly bool _propertyTypeEqualsTypeToConvert; @@ -190,6 +198,16 @@ internal JsonPropertyInfo(JsonSerializerOptions options, JsonPropertyInfoValues< NumberHandling = propertyInfo.NumberHandling; } + internal override void Configure() + { + base.Configure(); + + if (!IsForTypeInfo && !IsIgnored) + { + _converterIsExternalAndPolymorphic = !EffectiveConverter.IsInternalConverter && PropertyType != EffectiveConverter.TypeToConvert; + } + } + private protected override void DetermineEffectiveConverter() { JsonConverter? customConverter = CustomConverter;