Skip to content

Commit

Permalink
Merge pull request MessagePack-CSharp#1277 from seriousbox/fix-int-ke…
Browse files Browse the repository at this point in the history
…y-constructor-matching

Updating constructor matching for int keys
  • Loading branch information
AArnott authored Jul 14, 2021
2 parents 15d6851 + c2a7583 commit 1551f93
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,7 @@ public static ObjectSerializationInfo CreateOrNull(Type type, bool forceStringKe
var constructorParameters = new List<EmittableMemberAndConstructorParameter>();
if (ctor != null)
{
IReadOnlyDictionary<int, EmittableMember> ctorParamIndexIntMembersDictionary = intMembers.OrderBy(x => x.Key).Select((x, i) => (Key: x.Value, Index: i)).ToDictionary(x => x.Index, x => x.Key);
ILookup<string, KeyValuePair<string, EmittableMember>> constructorLookupByKeyDictionary = stringMembers.ToLookup(x => x.Key, x => x, StringComparer.OrdinalIgnoreCase);
ILookup<string, KeyValuePair<string, EmittableMember>> constructorLookupByMemberNameDictionary = stringMembers.ToLookup(x => x.Value.Name, x => x, StringComparer.OrdinalIgnoreCase);
do
Expand All @@ -1798,7 +1799,7 @@ public static ObjectSerializationInfo CreateOrNull(Type type, bool forceStringKe
EmittableMember paramMember;
if (isIntKey)
{
if (intMembers.TryGetValue(ctorParamIndex, out paramMember))
if (ctorParamIndexIntMembersDictionary.TryGetValue(ctorParamIndex, out paramMember))
{
if ((item.ParameterType == paramMember.Type ||
item.ParameterType.GetTypeInfo().IsAssignableFrom(paramMember.Type))
Expand All @@ -1811,7 +1812,7 @@ public static ObjectSerializationInfo CreateOrNull(Type type, bool forceStringKe
if (ctorEnumerator != null)
{
ctor = null;
continue;
break;
}
else
{
Expand All @@ -1824,7 +1825,7 @@ public static ObjectSerializationInfo CreateOrNull(Type type, bool forceStringKe
if (ctorEnumerator != null)
{
ctor = null;
continue;
break;
}
else
{
Expand Down Expand Up @@ -1857,7 +1858,7 @@ public static ObjectSerializationInfo CreateOrNull(Type type, bool forceStringKe
if (ctorEnumerator != null)
{
ctor = null;
continue;
break;
}
else
{
Expand All @@ -1875,7 +1876,7 @@ public static ObjectSerializationInfo CreateOrNull(Type type, bool forceStringKe
if (ctorEnumerator != null)
{
ctor = null;
continue;
break;
}
else
{
Expand All @@ -1888,7 +1889,7 @@ public static ObjectSerializationInfo CreateOrNull(Type type, bool forceStringKe
if (ctorEnumerator != null)
{
ctor = null;
continue;
break;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,30 @@ public TestConstructor9(int x, int y, int z)
}
}

/// <summary>
/// This constructor tests the case where there are missing int keys, to ensure that
/// the correct ctor is still found using index numbers of the keys, not their values.
/// </summary>
[MessagePackObject]
public class TestConstructor10
{
[Key(0)]
public int X { get; }

[Key(2)]
public int Y { get; }

[Key(5)]
public int Z { get; }

public TestConstructor10(int x, int y, int z)
{
X = x;
Y = y;
Z = z;
}
}

[Fact]
public void StringKey()
{
Expand Down Expand Up @@ -350,5 +374,17 @@ public void MatchedStructCtorFoundWithMixOfMemberNamesAndStringKeys()
r.Y.Is(8);
r.Z.Is(9);
}

[Fact]
public void MatchedClassCtorFoundWithMissingIntKeys()
{
var ctor = new TestConstructor10(10, 11, 12);
var bin = MessagePackSerializer.Serialize(ctor);
var r = MessagePackSerializer.Deserialize<TestConstructor10>(bin);

r.X.Is(10);
r.Y.Is(11);
r.Z.Is(12);
}
}
}

0 comments on commit 1551f93

Please sign in to comment.