akkadotnet / Hyperion

Polymorphic serialization for .NET
Apache License 2.0
277 stars 62 forks source link

Add a test where the manifest for collections of a known type aren't … #346

Open AppcenterAucxis opened 1 year ago

AppcenterAucxis commented 1 year ago

Hi I'm using Hyperion as the serializer and I know all my types beforehand so I'm trying to use the knownType array. It seems that generic List aren't counted as knownTypes.

Example:

public class Program
{
    static void Main(string[] args)
    {
        var stream = new MemoryStream();
        var msg = new Parent()
        {
            Name = "Sander",
            Children = new List<Child>()
            {
                new Child() { Name = "Juuls", },
                new Child() { Name = "Raeven", },
            },
        };
        var knownTypes = new[]
        {
            typeof(string),
            typeof(Parent),
            typeof(Child),
            msg.Children.GetType(), //same as typeof(List<Child>)
        };

        var serializer = new Serializer(new SerializerOptions(false, false, null, null, knownTypes, false, null, false, null));
        serializer.Serialize(msg, stream);
        stream.Position = 0;
        var a = stream.ToArray();
        var text = string.Join("", a.Select(x => x < 32 || x > 126 ? "" : ((char)x).ToString()));
        var res = (Parent)serializer.Deserialize(stream);
        Console.WriteLine(text); // output = "bSystem.Collections.Generic.List1[[ConsoleApp1.Child, ConsoleApp1]], System.Private.CoreLib,%core%JuulsRaevenSander"
        Console.ReadLine();
    }
}

public class Parent
{
    public string Name { get; set; }
    public List<Child> Children { get; set; }
}

public class Child
{
    public string Name { get; set; }
}

Result: bSystem.Collections.Generic.List1[[ConsoleApp1.Child, ConsoleApp1]], System.Private.CoreLib,%core%JuulsRaevenSander

Expected result: No mention of "System.Collections.Generic.List"

Aaronontheweb: "yeah sounds like the knownTypes mechanism is pretty perscriptive and doesn't have innate support for counting collections of knownTypes too. Do you think you could submit a PR for that?

I'll meet you half way - submit a failing PR to the repo that reproduces the problem and someone from our team will look into it"

Fixes #

Changes

I have added a test with a list of poco's, which now fails because the serializer still contains "System.Collections.Generic.List"

Checklist

For significant changes, please ensure that the following have been completed (delete if not relevant):

Latest dev Benchmarks

Include data from the relevant benchmark prior to this change here.

This PR's Benchmarks

Include data from after this change here.