< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 28
Coverable lines: 28
Total lines: 52
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
CanConvert(...)0%880%
CreateConverter(...)100%110%
CreateUnsupportedConverterForType(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UnsupportedTypeConverterFactory.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Reflection;
 7using System.Runtime.Serialization;
 8
 9namespace System.Text.Json.Serialization.Converters
 10{
 11    [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)]
 12    internal sealed class UnsupportedTypeConverterFactory : JsonConverterFactory
 13    {
 14        public override bool CanConvert(Type type)
 015        {
 16            // If a type is added, also add to the SourceGeneration project.
 17
 018            return
 019                // There's no safe way to construct a Type/MemberInfo from untrusted user input.
 020                typeof(MemberInfo).IsAssignableFrom(type) ||
 021                // (De)serialization of SerializationInfo is already disallowed due to Type being disallowed
 022                // (the two ctors on SerializationInfo take a Type, and a Type member is present when serializing).
 023                // Explicitly disallowing this type provides a clear exception when ctors with
 024                // .ctor(SerializationInfo, StreamingContext) signatures are attempted to be used for deserialization.
 025                // Invoking such ctors is not safe when used with untrusted user input.
 026                type == typeof(SerializationInfo) ||
 027                type == typeof(IntPtr) ||
 028                type == typeof(UIntPtr) ||
 029                // Exclude delegates.
 030                typeof(Delegate).IsAssignableFrom(type);
 031        }
 32
 33        public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options)
 034        {
 035            Debug.Assert(CanConvert(type));
 036            return CreateUnsupportedConverterForType(type);
 037        }
 38
 39        internal static JsonConverter CreateUnsupportedConverterForType(Type type, string? errorMessage = null)
 040        {
 041            JsonConverter converter = (JsonConverter)Activator.CreateInstance(
 042                typeof(UnsupportedTypeConverter<>).MakeGenericType(type),
 043                BindingFlags.Instance | BindingFlags.Public,
 044                binder: null,
 045                args: new object?[] { errorMessage },
 046                culture: null)!;
 47
 048            return converter;
 049        }
 50    }
 51}
 52