From 6bafc4c747068adf0e5e1f68e21e6b05d1363b3a Mon Sep 17 00:00:00 2001 From: jkuehner Date: Wed, 23 Sep 2020 18:05:07 +0200 Subject: [PATCH 1/4] bugfix not using know serializers when defined --- .../SerializerFactories/ArraySerializerFactory.cs | 9 ++++++++- .../ConstructorInfoSerializerFactory.cs | 8 +++++++- .../DefaultDictionarySerializerFactory.cs | 8 +++++++- .../SerializerFactories/DelegateSerializerFactory.cs | 8 +++++++- .../SerializerFactories/DictionarySerializerFactory.cs | 8 +++++++- .../SerializerFactories/EnumerableSerializerFactory.cs | 8 +++++++- .../SerializerFactories/ExceptionSerializerFactory.cs | 8 +++++++- .../ExpandoObjectSerializerFactory.cs | 8 +++++++- .../SerializerFactories/FSharpListSerializerFactory.cs | 8 +++++++- .../SerializerFactories/FSharpMapSerializerFactory.cs | 8 +++++++- .../SerializerFactories/FieldInfoSerializerFactory.cs | 8 +++++++- .../ImmutableCollectionsSerializerFactory.cs | 8 +++++++- .../SerializerFactories/MethodInfoSerializerFactory.cs | 8 +++++++- .../MultipleDimensionalArraySerialzierFactory.cs | 8 +++++++- .../SerializerFactories/PropertyInfoSerializerFactory.cs | 8 +++++++- 15 files changed, 106 insertions(+), 15 deletions(-) diff --git a/src/Hyperion/SerializerFactories/ArraySerializerFactory.cs b/src/Hyperion/SerializerFactories/ArraySerializerFactory.cs index a4ac76a3..797d635b 100644 --- a/src/Hyperion/SerializerFactories/ArraySerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ArraySerializerFactory.cs @@ -71,7 +71,14 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type WriteValues((dynamic)arr, stream, elementType, elementSerializer, session); }; arraySerializer.Initialize(reader, writer); - typeMapping.TryAdd(type, arraySerializer); + + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(arraySerializer, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, arraySerializer); return arraySerializer; } } diff --git a/src/Hyperion/SerializerFactories/ConstructorInfoSerializerFactory.cs b/src/Hyperion/SerializerFactories/ConstructorInfoSerializerFactory.cs index 877bc950..6cf7f5e3 100644 --- a/src/Hyperion/SerializerFactories/ConstructorInfoSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ConstructorInfoSerializerFactory.cs @@ -32,7 +32,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - typeMapping.TryAdd(type, os); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(os, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, os); ObjectReader reader = (stream, session) => { var owner = stream.ReadObject(session) as Type; diff --git a/src/Hyperion/SerializerFactories/DefaultDictionarySerializerFactory.cs b/src/Hyperion/SerializerFactories/DefaultDictionarySerializerFactory.cs index b7d26ece..29902e95 100644 --- a/src/Hyperion/SerializerFactories/DefaultDictionarySerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/DefaultDictionarySerializerFactory.cs @@ -31,7 +31,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var ser = new ObjectSerializer(type); - typeMapping.TryAdd(type, ser); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(ser, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, ser); var elementSerializer = serializer.GetSerializerByType(typeof (DictionaryEntry)); var preserveObjectReferences = serializer.Options.PreserveObjectReferences; ObjectReader reader = (stream, session) => diff --git a/src/Hyperion/SerializerFactories/DelegateSerializerFactory.cs b/src/Hyperion/SerializerFactories/DelegateSerializerFactory.cs index 850233df..6181c81e 100644 --- a/src/Hyperion/SerializerFactories/DelegateSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/DelegateSerializerFactory.cs @@ -31,7 +31,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - typeMapping.TryAdd(type, os); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(os, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, os); var methodInfoSerializer = serializer.GetSerializerByType(typeof(MethodInfo)); var preserveObjectReferences = serializer.Options.PreserveObjectReferences; ObjectReader reader = (stream, session) => diff --git a/src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs b/src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs index 113317b5..4c6fdbec 100644 --- a/src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs @@ -39,7 +39,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type { var preserveObjectReferences = serializer.Options.PreserveObjectReferences; var ser = new ObjectSerializer(type); - typeMapping.TryAdd(type, ser); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(ser, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, ser); var dictionaryTypes = GetKeyValuePairType(type); var elementSerializer = serializer.GetSerializerByType(dictionaryTypes.KeyValuePairType); diff --git a/src/Hyperion/SerializerFactories/EnumerableSerializerFactory.cs b/src/Hyperion/SerializerFactories/EnumerableSerializerFactory.cs index ebadbf2b..854d40e2 100644 --- a/src/Hyperion/SerializerFactories/EnumerableSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/EnumerableSerializerFactory.cs @@ -136,7 +136,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var x = new ObjectSerializer(type); - typeMapping.TryAdd(type, x); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(x, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, x); var preserveObjectReferences = serializer.Options.PreserveObjectReferences; diff --git a/src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs b/src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs index f7ac7b63..aca00d58 100644 --- a/src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs @@ -91,7 +91,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type StringSerializer.WriteValueImpl(stream, stackTraceString, session); stream.WriteObjectWithManifest(innerException, session); }); - typeMapping.TryAdd(type, exceptionSerializer); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(exceptionSerializer, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, exceptionSerializer); return exceptionSerializer; } } diff --git a/src/Hyperion/SerializerFactories/ExpandoObjectSerializerFactory.cs b/src/Hyperion/SerializerFactories/ExpandoObjectSerializerFactory.cs index b04775a0..48ba56af 100644 --- a/src/Hyperion/SerializerFactories/ExpandoObjectSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ExpandoObjectSerializerFactory.cs @@ -29,7 +29,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type { var preserveObjectReferences = serializer.Options.PreserveObjectReferences; var ser = new ObjectSerializer(type); - typeMapping.TryAdd(type, ser); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(ser, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, ser); var elementSerializer = serializer.GetSerializerByType(typeof(DictionaryEntry)); ObjectReader reader = (stream, session) => diff --git a/src/Hyperion/SerializerFactories/FSharpListSerializerFactory.cs b/src/Hyperion/SerializerFactories/FSharpListSerializerFactory.cs index abcfd41f..4716d355 100644 --- a/src/Hyperion/SerializerFactories/FSharpListSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/FSharpListSerializerFactory.cs @@ -48,7 +48,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var x = new ObjectSerializer(type); - typeMapping.TryAdd(type, x); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(x, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, x); var elementType = GetEnumerableType(type); var arrType = elementType.MakeArrayType(); diff --git a/src/Hyperion/SerializerFactories/FSharpMapSerializerFactory.cs b/src/Hyperion/SerializerFactories/FSharpMapSerializerFactory.cs index 55db6c2d..5d2738e0 100644 --- a/src/Hyperion/SerializerFactories/FSharpMapSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/FSharpMapSerializerFactory.cs @@ -64,7 +64,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var x = new ObjectSerializer(type); - typeMapping.TryAdd(type, x); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(x, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, x); var keyType = GetKeyType(type); var valueType = GetValyeType(type); diff --git a/src/Hyperion/SerializerFactories/FieldInfoSerializerFactory.cs b/src/Hyperion/SerializerFactories/FieldInfoSerializerFactory.cs index 84685233..adf5e1d3 100644 --- a/src/Hyperion/SerializerFactories/FieldInfoSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/FieldInfoSerializerFactory.cs @@ -31,7 +31,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - typeMapping.TryAdd(type, os); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(os, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, os); ObjectReader reader = (stream, session) => { var name = stream.ReadString(session); diff --git a/src/Hyperion/SerializerFactories/ImmutableCollectionsSerializerFactory.cs b/src/Hyperion/SerializerFactories/ImmutableCollectionsSerializerFactory.cs index e6eab05e..29004b9e 100644 --- a/src/Hyperion/SerializerFactories/ImmutableCollectionsSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ImmutableCollectionsSerializerFactory.cs @@ -49,7 +49,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var x = new ObjectSerializer(type); - typeMapping.TryAdd(type, x); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(x, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, x); var preserveObjectReferences = serializer.Options.PreserveObjectReferences; var elementType = GetEnumerableType(type) ?? typeof (object); diff --git a/src/Hyperion/SerializerFactories/MethodInfoSerializerFactory.cs b/src/Hyperion/SerializerFactories/MethodInfoSerializerFactory.cs index 44b63e41..7444cf8d 100644 --- a/src/Hyperion/SerializerFactories/MethodInfoSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/MethodInfoSerializerFactory.cs @@ -32,7 +32,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - typeMapping.TryAdd(type, os); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(os, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, os); ObjectReader reader = (stream, session) => { var name = stream.ReadString(session); diff --git a/src/Hyperion/SerializerFactories/MultipleDimensionalArraySerialzierFactory.cs b/src/Hyperion/SerializerFactories/MultipleDimensionalArraySerialzierFactory.cs index cdfab7a2..86d83bce 100644 --- a/src/Hyperion/SerializerFactories/MultipleDimensionalArraySerialzierFactory.cs +++ b/src/Hyperion/SerializerFactories/MultipleDimensionalArraySerialzierFactory.cs @@ -142,7 +142,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type WriteValues((Array)arr, stream, elementType, elementSerializer, session); }; arraySerializer.Initialize(reader, writer); - typeMapping.TryAdd(type, arraySerializer); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(arraySerializer, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, arraySerializer); return arraySerializer; } diff --git a/src/Hyperion/SerializerFactories/PropertyInfoSerializerFactory.cs b/src/Hyperion/SerializerFactories/PropertyInfoSerializerFactory.cs index c9359518..45d47ad8 100644 --- a/src/Hyperion/SerializerFactories/PropertyInfoSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/PropertyInfoSerializerFactory.cs @@ -31,7 +31,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - typeMapping.TryAdd(type, os); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(os, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, os); ObjectReader reader = (stream, session) => { var name = stream.ReadString(session); From 9c725c03afbc8288a1c853a8ff3e263a892888b8 Mon Sep 17 00:00:00 2001 From: jkuehner Date: Wed, 28 Oct 2020 22:19:32 +0100 Subject: [PATCH 2/4] Create Test for Issue #183 --- src/Hyperion.Tests/Bugs.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Hyperion.Tests/Bugs.cs b/src/Hyperion.Tests/Bugs.cs index a4ebe36c..9783aec4 100644 --- a/src/Hyperion.Tests/Bugs.cs +++ b/src/Hyperion.Tests/Bugs.cs @@ -8,9 +8,11 @@ #endregion using System; +using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.IO; +using System.Linq; using System.Reflection; using System.Reflection.Emit; using System.Text; @@ -246,5 +248,26 @@ public Recover(SnapshotSelectionCriteria fromSnapshot, long toSequenceNr = long. /// public long ReplayMax { get; private set; } } + + class Temp + { + public object[] SubArray { get; set; } + public string aa { get; set; } + public Dictionary dc { get; set; } + } + + [Fact] + public void WritesManifestEvenIfKnown1() + { + var stream = new MemoryStream(); + var msg = new Temp() { aa = "huhu", dc = new Dictionary() { { "a", "b" } }, SubArray = new object[] { 1, (byte)2, new object[] { 3 } } }; + var serializer = new Serializer(new SerializerOptions(knownTypes: new[] { typeof(DictionaryEntry), typeof(Dictionary), typeof(Temp), typeof(object[]), })); + serializer.Serialize(msg, stream); + stream.Position = 0; + var a = stream.ToArray(); + var text = string.Join("", a.Select(x => ((char)x).ToString())); + var res = serializer.Deserialize(stream); + Assert.DoesNotContain("System.Collections.Generic.Dictionary", text); + } } } From d13f8dd5bb3cb3cbf16db8529072f0090a65d3d4 Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Thu, 29 Oct 2020 13:39:40 +0700 Subject: [PATCH 3/4] Add a more aggresive unit test --- src/Hyperion.Tests/Bugs.cs | 222 +++++++++++++++++++++++++++++++++++-- 1 file changed, 214 insertions(+), 8 deletions(-) diff --git a/src/Hyperion.Tests/Bugs.cs b/src/Hyperion.Tests/Bugs.cs index 9783aec4..ebd9f562 100644 --- a/src/Hyperion.Tests/Bugs.cs +++ b/src/Hyperion.Tests/Bugs.cs @@ -19,12 +19,20 @@ using FluentAssertions; using Hyperion.Extensions; using Xunit; +using Xunit.Abstractions; namespace Hyperion.Tests { public class Bugs { + private readonly ITestOutputHelper _output; + + public Bugs(ITestOutputHelper output) + { + _output = output; + } + #region issue 58 public enum TrustLevel { Unknown, Suspicious, Partial, Fully } @@ -249,25 +257,223 @@ public Recover(SnapshotSelectionCriteria fromSnapshot, long toSequenceNr = long. public long ReplayMax { get; private set; } } - class Temp + delegate int TestDelegate(int x, int y); + + class Temp : IEquatable { public object[] SubArray { get; set; } - public string aa { get; set; } - public Dictionary dc { get; set; } + public int[] IntArray { get; set; } + public int[,] IntIntArray { get; set; } + public Poco Poco { get; set; } + public string String { get; set; } + public Dictionary Dictionary { get; set; } + public TestDelegate Delegate { get; set; } + public IEnumerable TestEnum { get; set; } + public Exception Exception { get; set; } + public ImmutableList ImmutableList { get; set; } + public ImmutableDictionary ImmutableDictionary { get; set; } + + public bool Equals(Temp other) + { + if (other == null) + throw new Exception("Equals failed."); + if (ReferenceEquals(this, other)) + throw new Exception("Equals failed."); + if (IntIntArray.Rank != other.IntIntArray.Rank) + throw new Exception("Equals failed."); + + for (var i = 0; i < IntIntArray.Rank; ++i) + { + for (var j = 0; j < IntIntArray.GetLength(i); ++j) + { + if (IntIntArray[j, i] != other.IntIntArray[j, i]) + throw new Exception("Equals failed."); + } + } + + if (Exception.GetType() != other.Exception.GetType()) + throw new Exception("Equals failed."); + if (Exception.Message != other.Exception.Message) + throw new Exception("Equals failed."); + if(Exception.InnerException != null + && Exception.InnerException.GetType() != other.Exception.InnerException.GetType()) + throw new Exception("Equals failed."); + + for (var i = 0; i < SubArray.Length; i++) + { + if (SubArray[i].GetType() != other.SubArray[i].GetType()) + throw new Exception("Equals failed."); + + if (SubArray[i] is Array arr) + { + var oArr = (Array)other.SubArray[i]; + for (var j = 0; j < arr.Length; ++j) + { + if (!arr.GetValue(j).Equals(oArr.GetValue(j))) + throw new Exception("Equals failed."); + } + } else if (!SubArray[i].Equals(other.SubArray[i])) + throw new Exception("Equals failed."); + } + + foreach (var key in Dictionary.Keys) + { + if (!Dictionary[key].Equals(other.Dictionary[key])) + throw new Exception("Equals failed."); + } + + foreach (var key in ImmutableDictionary.Keys) + { + if (!ImmutableDictionary[key].Equals(other.ImmutableDictionary[key])) + throw new Exception("Equals failed."); + } + + if (other.Delegate(2, 2) != 4) + throw new Exception("Equals failed."); + + if(!IntArray.SequenceEqual(other.IntArray)) + throw new Exception("Equals failed."); + if(!Equals(Poco, other.Poco)) + throw new Exception("Equals failed."); + if (String != other.String) + throw new Exception("Equals failed."); + if(!TestEnum.SequenceEqual(other.TestEnum)) + throw new Exception("Equals failed."); + if(!ImmutableList.SequenceEqual(other.ImmutableList)) + throw new Exception("Equals failed."); + + return true; + } + + public override bool Equals(object obj) + { + if (obj == null) throw new Exception("Equals failed."); + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) throw new Exception("Equals failed."); + return Equals((Temp) obj); + } + + public override int GetHashCode() + { + unchecked + { + var hashCode = (SubArray != null ? SubArray.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (IntArray != null ? IntArray.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (IntIntArray != null ? IntIntArray.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (Poco != null ? Poco.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (String != null ? String.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (Dictionary != null ? Dictionary.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (Delegate != null ? Delegate.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (TestEnum != null ? TestEnum.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (Exception != null ? Exception.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (ImmutableList != null ? ImmutableList.GetHashCode() : 0); + return hashCode; + } + } + } + + class Poco : IEquatable + { + public Poco() + { } + + public Poco(int intValue, string stringValue) + { + Int = intValue; + String = stringValue; + } + + public int Int { get; set; } + public string String { get; set; } + + public bool Equals(Poco other) + { + if (ReferenceEquals(null, other)) + throw new Exception("Equals failed."); + if (ReferenceEquals(this, other)) + throw new Exception("Equals failed."); + if(Int != other.Int) + throw new Exception("Equals failed."); + if(String != other.String) + throw new Exception("Equals failed."); + return true; + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) throw new Exception("Equals failed."); + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) throw new Exception("Equals failed."); + return Equals((Poco) obj); + } + + public override int GetHashCode() + { + unchecked + { + return (Int * 397) ^ (String != null ? String.GetHashCode() : 0); + } + } } [Fact] - public void WritesManifestEvenIfKnown1() + public void WritesManifestEvenIfKnown() { var stream = new MemoryStream(); - var msg = new Temp() { aa = "huhu", dc = new Dictionary() { { "a", "b" } }, SubArray = new object[] { 1, (byte)2, new object[] { 3 } } }; - var serializer = new Serializer(new SerializerOptions(knownTypes: new[] { typeof(DictionaryEntry), typeof(Dictionary), typeof(Temp), typeof(object[]), })); + var msg = new Temp + { + SubArray = new object[] { 1, (byte)2, new object[] { 3 } }, + IntArray = new [] {1, 2, 3, 4, 5}, + IntIntArray = new [,] {{1, 2}, {3,4}, {5,6}, {7,8}}, + Poco = new Poco(999, "666"), + String = "huhu", + Dictionary = new Dictionary + { + { 666, "b" }, + { 999, "testString" }, + { 42, "iMaGiNe" } + }, + Delegate = (x, y) => x * y, + TestEnum = new[]{4,8,9,3,2}, + Exception = new ArgumentException("Test Exception", new IndexOutOfRangeException("-999")), + ImmutableList = new [] {9, 4, 6, 2, 5}.ToImmutableList(), + ImmutableDictionary = new Dictionary + { + { 666, "b" }, + { 999, "testString" }, + { 42, "iMaGiNe" } + }.ToImmutableDictionary(), + }; + var serializer = new Serializer(new SerializerOptions(knownTypes: new[] + { + typeof(object[]), + typeof(int[]), + typeof(int[,]), + typeof(Dictionary), + typeof(DictionaryEntry), + typeof(KeyValuePair), + typeof(Temp), + typeof(TestDelegate), + typeof(Enumerable), + typeof(IEnumerable), + typeof(Exception), + typeof(ArgumentException), + typeof(IndexOutOfRangeException), + typeof(FieldInfo), + typeof(ImmutableList), + typeof(ImmutableList), + typeof(ImmutableDictionary), + typeof(MethodInfo), + typeof(PropertyInfo), + })); serializer.Serialize(msg, stream); stream.Position = 0; var a = stream.ToArray(); - var text = string.Join("", a.Select(x => ((char)x).ToString())); - var res = serializer.Deserialize(stream); + var text = string.Join("", a.Select(x => x < 32 || x > 126 ? "" : ((char)x).ToString())); + _output.WriteLine(text); + var res = (Temp)serializer.Deserialize(stream); Assert.DoesNotContain("System.Collections.Generic.Dictionary", text); + Assert.Equal(msg, res); } } } From 110f91ffe94623e06cc42a130c81527d46bdace6 Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Thu, 29 Oct 2020 13:44:39 +0700 Subject: [PATCH 4/4] Remove modifications that could not be reached/touched by the unit test --- .../ConstructorInfoSerializerFactory.cs | 8 +------- .../SerializerFactories/DictionarySerializerFactory.cs | 8 +------- .../SerializerFactories/EnumerableSerializerFactory.cs | 8 +------- .../SerializerFactories/ExpandoObjectSerializerFactory.cs | 8 +------- .../SerializerFactories/FSharpListSerializerFactory.cs | 8 +------- .../SerializerFactories/FSharpMapSerializerFactory.cs | 8 +------- .../SerializerFactories/FieldInfoSerializerFactory.cs | 8 +------- .../SerializerFactories/MethodInfoSerializerFactory.cs | 8 +------- .../SerializerFactories/PropertyInfoSerializerFactory.cs | 8 +------- 9 files changed, 9 insertions(+), 63 deletions(-) diff --git a/src/Hyperion/SerializerFactories/ConstructorInfoSerializerFactory.cs b/src/Hyperion/SerializerFactories/ConstructorInfoSerializerFactory.cs index 6cf7f5e3..877bc950 100644 --- a/src/Hyperion/SerializerFactories/ConstructorInfoSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ConstructorInfoSerializerFactory.cs @@ -32,13 +32,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) - { - var wrapper = new KnownTypeObjectSerializer(os, index); - typeMapping.TryAdd(type, wrapper); - } - else - typeMapping.TryAdd(type, os); + typeMapping.TryAdd(type, os); ObjectReader reader = (stream, session) => { var owner = stream.ReadObject(session) as Type; diff --git a/src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs b/src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs index 4c6fdbec..113317b5 100644 --- a/src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs @@ -39,13 +39,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type { var preserveObjectReferences = serializer.Options.PreserveObjectReferences; var ser = new ObjectSerializer(type); - if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) - { - var wrapper = new KnownTypeObjectSerializer(ser, index); - typeMapping.TryAdd(type, wrapper); - } - else - typeMapping.TryAdd(type, ser); + typeMapping.TryAdd(type, ser); var dictionaryTypes = GetKeyValuePairType(type); var elementSerializer = serializer.GetSerializerByType(dictionaryTypes.KeyValuePairType); diff --git a/src/Hyperion/SerializerFactories/EnumerableSerializerFactory.cs b/src/Hyperion/SerializerFactories/EnumerableSerializerFactory.cs index 854d40e2..ebadbf2b 100644 --- a/src/Hyperion/SerializerFactories/EnumerableSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/EnumerableSerializerFactory.cs @@ -136,13 +136,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var x = new ObjectSerializer(type); - if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) - { - var wrapper = new KnownTypeObjectSerializer(x, index); - typeMapping.TryAdd(type, wrapper); - } - else - typeMapping.TryAdd(type, x); + typeMapping.TryAdd(type, x); var preserveObjectReferences = serializer.Options.PreserveObjectReferences; diff --git a/src/Hyperion/SerializerFactories/ExpandoObjectSerializerFactory.cs b/src/Hyperion/SerializerFactories/ExpandoObjectSerializerFactory.cs index 48ba56af..b04775a0 100644 --- a/src/Hyperion/SerializerFactories/ExpandoObjectSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ExpandoObjectSerializerFactory.cs @@ -29,13 +29,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type { var preserveObjectReferences = serializer.Options.PreserveObjectReferences; var ser = new ObjectSerializer(type); - if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) - { - var wrapper = new KnownTypeObjectSerializer(ser, index); - typeMapping.TryAdd(type, wrapper); - } - else - typeMapping.TryAdd(type, ser); + typeMapping.TryAdd(type, ser); var elementSerializer = serializer.GetSerializerByType(typeof(DictionaryEntry)); ObjectReader reader = (stream, session) => diff --git a/src/Hyperion/SerializerFactories/FSharpListSerializerFactory.cs b/src/Hyperion/SerializerFactories/FSharpListSerializerFactory.cs index 4716d355..abcfd41f 100644 --- a/src/Hyperion/SerializerFactories/FSharpListSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/FSharpListSerializerFactory.cs @@ -48,13 +48,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var x = new ObjectSerializer(type); - if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) - { - var wrapper = new KnownTypeObjectSerializer(x, index); - typeMapping.TryAdd(type, wrapper); - } - else - typeMapping.TryAdd(type, x); + typeMapping.TryAdd(type, x); var elementType = GetEnumerableType(type); var arrType = elementType.MakeArrayType(); diff --git a/src/Hyperion/SerializerFactories/FSharpMapSerializerFactory.cs b/src/Hyperion/SerializerFactories/FSharpMapSerializerFactory.cs index 5d2738e0..55db6c2d 100644 --- a/src/Hyperion/SerializerFactories/FSharpMapSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/FSharpMapSerializerFactory.cs @@ -64,13 +64,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var x = new ObjectSerializer(type); - if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) - { - var wrapper = new KnownTypeObjectSerializer(x, index); - typeMapping.TryAdd(type, wrapper); - } - else - typeMapping.TryAdd(type, x); + typeMapping.TryAdd(type, x); var keyType = GetKeyType(type); var valueType = GetValyeType(type); diff --git a/src/Hyperion/SerializerFactories/FieldInfoSerializerFactory.cs b/src/Hyperion/SerializerFactories/FieldInfoSerializerFactory.cs index adf5e1d3..84685233 100644 --- a/src/Hyperion/SerializerFactories/FieldInfoSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/FieldInfoSerializerFactory.cs @@ -31,13 +31,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) - { - var wrapper = new KnownTypeObjectSerializer(os, index); - typeMapping.TryAdd(type, wrapper); - } - else - typeMapping.TryAdd(type, os); + typeMapping.TryAdd(type, os); ObjectReader reader = (stream, session) => { var name = stream.ReadString(session); diff --git a/src/Hyperion/SerializerFactories/MethodInfoSerializerFactory.cs b/src/Hyperion/SerializerFactories/MethodInfoSerializerFactory.cs index 7444cf8d..44b63e41 100644 --- a/src/Hyperion/SerializerFactories/MethodInfoSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/MethodInfoSerializerFactory.cs @@ -32,13 +32,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) - { - var wrapper = new KnownTypeObjectSerializer(os, index); - typeMapping.TryAdd(type, wrapper); - } - else - typeMapping.TryAdd(type, os); + typeMapping.TryAdd(type, os); ObjectReader reader = (stream, session) => { var name = stream.ReadString(session); diff --git a/src/Hyperion/SerializerFactories/PropertyInfoSerializerFactory.cs b/src/Hyperion/SerializerFactories/PropertyInfoSerializerFactory.cs index 45d47ad8..c9359518 100644 --- a/src/Hyperion/SerializerFactories/PropertyInfoSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/PropertyInfoSerializerFactory.cs @@ -31,13 +31,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) - { - var wrapper = new KnownTypeObjectSerializer(os, index); - typeMapping.TryAdd(type, wrapper); - } - else - typeMapping.TryAdd(type, os); + typeMapping.TryAdd(type, os); ObjectReader reader = (stream, session) => { var name = stream.ReadString(session);