Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserve reference is checking type to prevent InvalidCastException #97

Merged
merged 1 commit into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Hyperion.Tests/Bugs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ public void CanSerializeImmutableGenericInterfaces()
}
}

[Fact]
public void CanSerializeUri()
{
var stream = new MemoryStream();
var msg = new Uri("http://localhost:9202/", UriKind.RelativeOrAbsolute);
var serializer = new Serializer(new SerializerOptions(preserveObjectReferences: true, versionTolerance: true));
serializer.Serialize(msg, stream);
stream.Position = 0;
var res = serializer.Deserialize(stream);

Assert.Equal(msg, res);
Assert.Equal(stream.Length, stream.Position);
}


public class SnapshotSelectionCriteria
{
public static SnapshotSelectionCriteria Latest { get; set; } = new SnapshotSelectionCriteria()
Expand Down
26 changes: 23 additions & 3 deletions src/Hyperion/SerializerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@

namespace Hyperion
{
internal class TypedEqualityComparer : IEqualityComparer<object>
{
public static readonly TypedEqualityComparer Instance = new TypedEqualityComparer();
public new bool Equals(object x, object y)
{
if (EqualityComparer<object>.Default.Equals(x, y))
{
if (x != null && y != null)
return x.GetType().Equals(y.GetType());
return true;
}
return false;
}

public int GetHashCode(object obj)
{
return EqualityComparer<object>.Default.GetHashCode(obj);
}
}

public class SerializerSession
{
public const int MinBufferSize = 9;
Expand All @@ -28,9 +48,9 @@ public SerializerSession(Serializer serializer)
Serializer = serializer;
if (serializer.Options.PreserveObjectReferences)
{
_objects = new Dictionary<object, int>();
_objects = new Dictionary<object, int>(TypedEqualityComparer.Instance);
}
_nextTypeId = (ushort)(serializer.Options.KnownTypes.Length );
_nextTypeId = (ushort)(serializer.Options.KnownTypes.Length);
}

public void TrackSerializedObject(object obj)
Expand Down Expand Up @@ -60,7 +80,7 @@ public byte[] GetBuffer(int length)
if (length <= _buffer.Length)
return _buffer;

length = Math.Max(length, _buffer.Length*2);
length = Math.Max(length, _buffer.Length * 2);

_buffer = new byte[length];

Expand Down