<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using NJsonSchema.CodeGeneration.CSharp;
using NJsonSchema.CodeGeneration.TypeScript;
using NJsonSchema.Converters;

namespace NJsonSchema.CodeGeneration.Tests
{
    public class Container
    {
        public Animal Animal { get; set; }
    }

    [JsonConverter(typeof(JsonInheritanceConverter), "discriminator")]
    [KnownType(typeof(Dog))]
    public class Animal
    {
        public string Foo { get; set; }
    }

    public class Dog : Animal
    {
        public string Bar { get; set; }
        public List<SubClass> SubElements { get; set; }
    }

    [JsonConverter(typeof(JsonInheritanceConverter), "discriminator")]
    [KnownType(typeof(SubClass1))]
    [KnownType(typeof(SubClass2))]
    public class SubClass
    {
    }

    public class SubClass1 : SubClass
    {
        public string Prop1 { get; set; }
    }

    public class SubClass2 : SubClass
    {
        public string Prop2 { get; set; }
    }

    [TestClass]
    public class InheritanceSerializationTests
    {
        [TestMethod]
        public void When_JsonInheritanceConverter_is_used_then_inheritance_is_correctly_serialized_and_deserialized()
        {
            //// Arrange
            var container = new Container
            {
                Animal = new Dog
                {
                    Foo = "foo",
                    Bar = "bar",
                    SubElements = new List<SubClass> { new SubClass1 { Prop1 = "x" }, new SubClass2 { Prop2 = "x" } }
                }
            };

            //// Act
            var json = JsonConvert.SerializeObject(container, Formatting.Indented);
            var deserializedContainer = JsonConvert.DeserializeObject<Container>(json);

            //// Assert
            Assert.IsTrue(deserializedContainer.Animal is Dog);
            Assert.IsTrue((deserializedContainer.Animal as Dog).SubElements.First() is SubClass1);
        }

        [TestMethod]
        public void JsonInheritanceConverter_is_thread_safe()
        {
            //// Arrange
            var tasks = new List<Task>();
            for (int i = 0; i < 100; i++)
            {
                tasks.Add(Task.Run(() =>
                {
                    When_JsonInheritanceConverter_is_used_then_inheritance_is_correctly_serialized_and_deserialized();
                }));
            }

            //// Act
            Task.WaitAll(tasks.ToArray());

            //// Assert
            // No exceptions
        }

        [TestMethod]
        public void When_JsonInheritanceConverter_is_set_then_discriminator_field_is_set()<commit_msg>Updated unit test, #224<commit_after>﻿using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using NJsonSchema.CodeGeneration.CSharp;
using NJsonSchema.CodeGeneration.TypeScript;
using NJsonSchema.Converters;

namespace NJsonSchema.CodeGeneration.Tests
{
    public class Container
    {
        public Animal Animal { get; set; }
    }

    [JsonConverter(typeof(JsonInheritanceConverter), "discriminator")]
    [KnownType(typeof(Dog))]
    public class Animal
    {
        public string Foo { get; set; }
    }

    public class Dog : Animal
    {
        public string Bar { get; set; }
        public List<SubClass> SubElements { get; set; }
    }

    [JsonConverter(typeof(JsonInheritanceConverter), "discriminator")]
    [KnownType(typeof(SubClass1))]
    [KnownType(typeof(SubClass2))]
    public class SubClass
    {
    }

    public class SubClass1 : SubClass
    {
        public string Prop1 { get; set; }
    }

    public class SubClass2 : SubClass
    {
        public string Prop2 { get; set; }
    }

    [TestClass]
    public class InheritanceSerializationTests
    {
        [TestMethod]
        public void When_JsonInheritanceConverter_is_used_then_inheritance_is_correctly_serialized_and_deserialized()
        {
            //// Arrange
            var container = new Container
            {
                Animal = new Dog
                {
                    Foo = "foo",
                    Bar = "bar",
                    SubElements = new List<SubClass> { new SubClass1 { Prop1 = "x" }, new SubClass2 { Prop2 = "x" } }
                }
            };

            //// Act
            var json = JsonConvert.SerializeObject(container, Formatting.Indented);
            var deserializedContainer = JsonConvert.DeserializeObject<Container>(json);

            var schema = JsonSchema4.FromType<Container>();
            var schemaJson = schema.ToJson();
            var errors = schema.Validate(json);

            //// Assert
            Assert.IsTrue(deserializedContainer.Animal is Dog);
            Assert.IsTrue((deserializedContainer.Animal as Dog).SubElements.First() is SubClass1);
        }

        [TestMethod]
        public void JsonInheritanceConverter_is_thread_safe()
        {
            //// Arrange
            var tasks = new List<Task>();
            for (int i = 0; i < 100; i++)
            {
                tasks.Add(Task.Run(() =>
                {
                    When_JsonInheritanceConverter_is_used_then_inheritance_is_correctly_serialized_and_deserialized();
                }));
            }

            //// Act
            Task.WaitAll(tasks.ToArray());

            //// Assert
            // No exceptions
        }

        [TestMethod]
        public void When_JsonInheritanceConverter_is_set_then_discriminator_field_is_set()
{'fraction_non_alphanumeric': 0.0920845710291805, 'fraction_numerical': 0.005940940066398742, 'mean_word_length': 2.0031479538300103, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3110482', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1532, 'n_words': 317}