< Summary

Line coverage
14%
Covered lines: 7
Uncovered lines: 42
Coverable lines: 49
Total lines: 131
Line coverage: 14.2%
Branch coverage
10%
Covered branches: 2
Total branches: 20
Branch coverage: 10%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
VerifyMutable()0%220%
CreateFromAttributeDeclarations(...)33.33%6638.88%
.ctor(...)100%110%
OnCollectionModifying()0%220%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPolymorphismOptions.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.Collections.Generic;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Reflection;
 7
 8namespace System.Text.Json.Serialization.Metadata
 9{
 10    /// <summary>
 11    /// Defines polymorphic configuration for a specified base type.
 12    /// </summary>
 13    public class JsonPolymorphismOptions
 14    {
 15        private DerivedTypeList? _derivedTypes;
 16        private bool _isConfigured;
 17
 18        /// <summary>
 19        /// Creates an empty <see cref="JsonPolymorphismOptions"/> instance.
 20        /// </summary>
 021        public JsonPolymorphismOptions()
 022        {
 023        }
 24
 25        /// <summary>
 26        /// Gets the list of derived types supported in the current polymorphic type configuration.
 27        /// </summary>
 028        public IList<JsonDerivedType> DerivedTypes => _derivedTypes ??= new(this);
 29
 30        /// <summary>
 31        /// When set to <see langword="true"/>, instructs the serializer to ignore any
 32        /// unrecognized type discriminator id's and reverts to the contract of the base type.
 33        /// Otherwise, it will fail the deserialization.
 34        /// </summary>
 35        /// <exception cref="InvalidOperationException">
 36        /// The parent <see cref="JsonTypeInfo"/> instance has been locked for further modification.
 37        /// </exception>
 38        public bool IgnoreUnrecognizedTypeDiscriminators
 39        {
 040            get;
 41            set
 042            {
 043                VerifyMutable();
 044                _isConfigured = true;
 045                field = value;
 046            }
 47        }
 48
 49        /// <summary>
 50        /// Gets or sets the behavior when serializing an undeclared derived runtime type.
 51        /// </summary>
 52        /// <exception cref="InvalidOperationException">
 53        /// The parent <see cref="JsonTypeInfo"/> instance has been locked for further modification.
 54        /// </exception>
 55        public JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling
 56        {
 057            get;
 58            set
 059            {
 060                VerifyMutable();
 061                _isConfigured = true;
 062                field = value;
 063            }
 64        }
 65
 66        /// <summary>
 67        /// Gets or sets a custom type discriminator property name for the polymorhic type.
 68        /// Uses the default '$type' property name if left unset.
 69        /// </summary>
 70        /// <exception cref="InvalidOperationException">
 71        /// The parent <see cref="JsonTypeInfo"/> instance has been locked for further modification.
 72        /// </exception>
 73        [AllowNull]
 74        public string TypeDiscriminatorPropertyName
 75        {
 076            get => field ?? JsonSerializer.TypePropertyName;
 77            set
 078            {
 079                VerifyMutable();
 080                _isConfigured = true;
 081                field = value;
 082            }
 83        }
 84
 085        private void VerifyMutable() => DeclaringTypeInfo?.VerifyMutable();
 86
 087        internal JsonTypeInfo? DeclaringTypeInfo { get; set; }
 88
 089        internal bool IsEmpty => !_isConfigured && _derivedTypes is not { Count: > 0 };
 90
 91        internal static JsonPolymorphismOptions? CreateFromAttributeDeclarations(
 92            Type baseType,
 93            out JsonPolymorphicAttribute? polymorphicAttribute)
 414794        {
 414795            JsonPolymorphismOptions? options = null;
 414796            polymorphicAttribute = baseType.GetCustomAttribute<JsonPolymorphicAttribute>(inherit: false);
 97
 414798            if (polymorphicAttribute is not null)
 099            {
 0100                options = new()
 0101                {
 0102                    IgnoreUnrecognizedTypeDiscriminators = polymorphicAttribute.IgnoreUnrecognizedTypeDiscriminators,
 0103                    UnknownDerivedTypeHandling = polymorphicAttribute.UnknownDerivedTypeHandling,
 0104                    TypeDiscriminatorPropertyName = polymorphicAttribute.TypeDiscriminatorPropertyName,
 0105                };
 0106            }
 107
 12441108            foreach (JsonDerivedTypeAttribute attr in baseType.GetCustomAttributes<JsonDerivedTypeAttribute>(inherit: fa
 0109            {
 0110                (options ??= new()).DerivedTypes.Add(new JsonDerivedType(attr.DerivedType, attr.TypeDiscriminator));
 0111            }
 112
 4147113            return options;
 4147114        }
 115
 116        private sealed class DerivedTypeList : ConfigurationList<JsonDerivedType>
 117        {
 118            private readonly JsonPolymorphismOptions _parent;
 119
 0120            public DerivedTypeList(JsonPolymorphismOptions parent)
 0121            {
 0122                _parent = parent;
 0123            }
 124
 0125            public override bool IsReadOnly => _parent.DeclaringTypeInfo?.IsReadOnly == true;
 0126            protected override void OnCollectionModifying() => _parent.DeclaringTypeInfo?.VerifyMutable();
 127        }
 128
 129    }
 130}
 131