Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit f70f16f

Browse files
committed
only allow methods that matches shapes of object getters/setters
1 parent 03bb422 commit f70f16f

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,6 @@ internal static AssignmentDefinition GetAssignmentDefinition(Type toType, Type f
444444

445445
return AssignmentDefinitionCache.GetOrAdd(cacheKey, delegate
446446
{
447-
448447
var definition = new AssignmentDefinition
449448
{
450449
ToType = toType,
@@ -515,7 +514,7 @@ private static Dictionary<string, AssignmentMember> GetMembers(Type type, bool i
515514
var parameterInfos = methodInfo.GetParameters();
516515
if (isReadable)
517516
{
518-
if (parameterInfos.Length == 0)
517+
if (parameterInfos.Length == 0 && methodInfo.ReturnType == typeof(object))
519518
{
520519
var name = info.Name.StartsWith("get_") ? info.Name.Substring(4) : info.Name;
521520
if (!map.ContainsKey(name))
@@ -527,7 +526,8 @@ private static Dictionary<string, AssignmentMember> GetMembers(Type type, bool i
527526
}
528527
else
529528
{
530-
if (parameterInfos.Length == 1 && methodInfo.ReturnType == typeof(void))
529+
if (parameterInfos.Length == 1 && methodInfo.ReturnType == typeof(void) &&
530+
methodInfo.GetParameters()[0].ParameterType == typeof(object))
531531
{
532532
var name = info.Name.StartsWith("set_") ? info.Name.Substring(4) : info.Name;
533533
if (!map.ContainsKey(name))
@@ -539,7 +539,6 @@ private static Dictionary<string, AssignmentMember> GetMembers(Type type, bool i
539539
}
540540
}
541541
}
542-
543542
return map;
544543
}
545544

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,22 +1566,22 @@ public void Does_convert_nested_collections()
15661566

15671567
public class TestMethod
15681568
{
1569-
public void MyMethod(string myProp)
1569+
public void MyMethod()
15701570
{
1571-
MyProp = myProp;
1571+
MyProp = nameof(MyProp);
15721572
}
15731573
public string MyProp { get; set; }
15741574
}
1575-
1575+
15761576
public class TestMethod2
15771577
{
1578-
public void MyMethod()
1578+
public void MyMethod(string myProp)
15791579
{
15801580
MyProp = nameof(MyMethod);
15811581
}
15821582
public string MyProp { get; set; }
15831583
}
1584-
1584+
15851585
[Test]
15861586
public void Does_not_try_to_map_methods()
15871587
{

0 commit comments

Comments
 (0)