<commit_before>// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.DocAsCode.YamlSerialization
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    using YamlDotNet.Core;
    using YamlDotNet.Core.Events;
    using YamlDotNet.Serialization;
    using YamlDotNet.Serialization.NamingConventions;
    using YamlDotNet.Serialization.NodeDeserializers;
    using YamlDotNet.Serialization.NodeTypeResolvers;
    using YamlDotNet.Serialization.TypeResolvers;
    using YamlDotNet.Serialization.Utilities;
    using YamlDotNet.Serialization.ValueDeserializers;

    using Microsoft.DocAsCode.YamlSerialization.Helpers;
    using Microsoft.DocAsCode.YamlSerialization.NodeDeserializers;
    using Microsoft.DocAsCode.YamlSerialization.NodeTypeResolvers;
    using Microsoft.DocAsCode.YamlSerialization.ObjectFactories;
    using Microsoft.DocAsCode.YamlSerialization.TypeInspectors;

    /// <summary>
    /// A faade for the YAML library with the standard configuration.
    /// </summary>
    public sealed class YamlDeserializer
    {
        private static Dictionary<string, Type> PredefinedTagMappings { get; } =
            new Dictionary<string, Type>
            {
                { "tag:yaml.org,2002:map", typeof(Dictionary<object, object>) },
                { "tag:yaml.org,2002:bool", typeof(bool) },
                { "tag:yaml.org,2002:float", typeof(double) },
                { "tag:yaml.org,2002:int", typeof(int) },
                { "tag:yaml.org,2002:str", typeof(string) },
                { "tag:yaml.org,2002:timestamp", typeof(DateTime) },
            };

        private readonly Dictionary<string, Type> _tagMappings;
        private readonly List<IYamlTypeConverter> _converters;
        private readonly TypeDescriptorProxy _typeDescriptor =
            new TypeDescriptorProxy();
        private readonly IValueDeserializer _valueDeserializer;

        public IList<INodeDeserializer> NodeDeserializers { get; private set; }
        public IList<INodeTypeResolver> TypeResolvers { get; private set; }
        public IValueDeserializer ValueDeserializer => _valueDeserializer;
        private class TypeDescriptorProxy : ITypeInspector
        {
            public ITypeInspector TypeDescriptor;

            public IEnumerable<IPropertyDescriptor> GetProperties(Type type, object container)
            {
                return TypeDescriptor.GetProperties(type, container);
            }

            public IPropertyDescriptor GetProperty(Type type, object container, string name, bool ignoreUnmatched)
            {
                return TypeDescriptor.GetProperty(type, container, name, ignoreUnmatched);
            }
        }

        public YamlDeserializer(
            IObjectFactory objectFactory = null,
            INamingConvention namingConvention = null,
            bool ignoreUnmatched = false,
            bool ignoreNotFoundAnchor = true)
        {
            objectFactory = objectFactory ?? new DefaultEmitObjectFactory();
            namingConvention = namingConvention ?? new NullNamingConvention();

            _typeDescriptor.TypeDescriptor =
                new ExtensibleYamlAttributesTypeInspector(
                    new ExtensibleNamingConventionTypeInspector(
                        new ExtensibleReadableAndWritablePropertiesTypeInspector(
                            new EmitTypeInspector(
                                new StaticTypeResolver()
                            )
                        ),
                        namingConvention
                    )
                );

            _converters = new List<IYamlTypeConverter>();
            foreach (IYamlTypeConverter yamlTypeConverter in YamlTypeConverters.BuiltInConverters)
            {
                _converters.Add(yamlTypeConverter);
            }

            NodeDeserializers = new List<INodeDeserializer>
            {
                new TypeConverterNodeDeserializer(_converters),
                new NullNodeDeserializer(),
                new ScalarNodeDeserializer(),
                new EmitArrayNodeDeserializer(),
                new EmitGenericDictionaryNodeDeserializer(objectFactory),
                new DictionaryNodeDeserializer(objectFactory),
                new EmitGenericCollectionNodeDeserializer(objectFactory),
                new CollectionNodeDeserializer(objectFactory),
                new EnumerableNodeDeserializer(),
                new ExtensibleObjectNodeDeserializer(objectFactory, _typeDescriptor, ignoreUnmatched)
            };
            _tagMappings = new Dictionary<string, Type>(PredefinedTagMappings);
            TypeResolvers = new List<INodeTypeResolver>
            {
                new TagNodeTypeResolver(_tagMappings),
                new TypeNameInTagNodeTypeResolver(),
                new DefaultContainersNodeTypeResolver(),
                new ScalarYamlNodeTypeResolver()
            };
            if (ignoreNotFoundAnchor)<commit_msg>avoid vulnerability in YamlDotNet (#3511)<commit_after>// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.DocAsCode.YamlSerialization
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    using YamlDotNet.Core;
    using YamlDotNet.Core.Events;
    using YamlDotNet.Serialization;
    using YamlDotNet.Serialization.NamingConventions;
    using YamlDotNet.Serialization.NodeDeserializers;
    using YamlDotNet.Serialization.NodeTypeResolvers;
    using YamlDotNet.Serialization.TypeResolvers;
    using YamlDotNet.Serialization.Utilities;
    using YamlDotNet.Serialization.ValueDeserializers;

    using Microsoft.DocAsCode.YamlSerialization.Helpers;
    using Microsoft.DocAsCode.YamlSerialization.NodeDeserializers;
    using Microsoft.DocAsCode.YamlSerialization.NodeTypeResolvers;
    using Microsoft.DocAsCode.YamlSerialization.ObjectFactories;
    using Microsoft.DocAsCode.YamlSerialization.TypeInspectors;

    /// <summary>
    /// A faade for the YAML library with the standard configuration.
    /// </summary>
    public sealed class YamlDeserializer
    {
        private static Dictionary<string, Type> PredefinedTagMappings { get; } =
            new Dictionary<string, Type>
            {
                { "tag:yaml.org,2002:map", typeof(Dictionary<object, object>) },
                { "tag:yaml.org,2002:bool", typeof(bool) },
                { "tag:yaml.org,2002:float", typeof(double) },
                { "tag:yaml.org,2002:int", typeof(int) },
                { "tag:yaml.org,2002:str", typeof(string) },
                { "tag:yaml.org,2002:timestamp", typeof(DateTime) },
            };

        private readonly Dictionary<string, Type> _tagMappings;
        private readonly List<IYamlTypeConverter> _converters;
        private readonly TypeDescriptorProxy _typeDescriptor =
            new TypeDescriptorProxy();
        private readonly IValueDeserializer _valueDeserializer;

        public IList<INodeDeserializer> NodeDeserializers { get; private set; }
        public IList<INodeTypeResolver> TypeResolvers { get; private set; }
        public IValueDeserializer ValueDeserializer => _valueDeserializer;
        private class TypeDescriptorProxy : ITypeInspector
        {
            public ITypeInspector TypeDescriptor;

            public IEnumerable<IPropertyDescriptor> GetProperties(Type type, object container)
            {
                return TypeDescriptor.GetProperties(type, container);
            }

            public IPropertyDescriptor GetProperty(Type type, object container, string name, bool ignoreUnmatched)
            {
                return TypeDescriptor.GetProperty(type, container, name, ignoreUnmatched);
            }
        }

        public YamlDeserializer(
            IObjectFactory objectFactory = null,
            INamingConvention namingConvention = null,
            bool ignoreUnmatched = false,
            bool ignoreNotFoundAnchor = true)
        {
            objectFactory = objectFactory ?? new DefaultEmitObjectFactory();
            namingConvention = namingConvention ?? new NullNamingConvention();

            _typeDescriptor.TypeDescriptor =
                new ExtensibleYamlAttributesTypeInspector(
                    new ExtensibleNamingConventionTypeInspector(
                        new ExtensibleReadableAndWritablePropertiesTypeInspector(
                            new EmitTypeInspector(
                                new StaticTypeResolver()
                            )
                        ),
                        namingConvention
                    )
                );

            _converters = new List<IYamlTypeConverter>();
            foreach (IYamlTypeConverter yamlTypeConverter in YamlTypeConverters.BuiltInConverters)
            {
                _converters.Add(yamlTypeConverter);
            }

            NodeDeserializers = new List<INodeDeserializer>
            {
                new TypeConverterNodeDeserializer(_converters),
                new NullNodeDeserializer(),
                new ScalarNodeDeserializer(),
                new EmitArrayNodeDeserializer(),
                new EmitGenericDictionaryNodeDeserializer(objectFactory),
                new DictionaryNodeDeserializer(objectFactory),
                new EmitGenericCollectionNodeDeserializer(objectFactory),
                new CollectionNodeDeserializer(objectFactory),
                new EnumerableNodeDeserializer(),
                new ExtensibleObjectNodeDeserializer(objectFactory, _typeDescriptor, ignoreUnmatched)
            };
            _tagMappings = new Dictionary<string, Type>(PredefinedTagMappings);
            TypeResolvers = new List<INodeTypeResolver>
            {
                new TagNodeTypeResolver(_tagMappings),
                new DefaultContainersNodeTypeResolver(),
                new ScalarYamlNodeTypeResolver()
            };
            if (ignoreNotFoundAnchor)
{'fraction_non_alphanumeric': 0.06743566992014197, 'fraction_numerical': 0.005126688356502021, 'mean_word_length': 2.3139496896439073, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2402693', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2417, 'n_words': 497}