Skip to content

Commit 78bd6f1

Browse files
committed
Improve IgnoreMappings support w/ tests
1 parent fc1bd5c commit 78bd6f1

3 files changed

Lines changed: 58 additions & 13 deletions

File tree

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ public static void RegisterConverter<From, To>(Func<From, To> converter)
3838
converters[Tuple.Create(typeof(From), typeof(To))] = x => converter((From)x);
3939
}
4040

41-
public static void IgnoreMapping<From, To>()
42-
{
43-
JsConfig.InitStatics();
44-
ignoreMappings[Tuple.Create(typeof(From),typeof(To))] = true;
45-
}
41+
public static void IgnoreMapping<From, To>() => IgnoreMapping(typeof(From), typeof(To));
4642

4743
public static void IgnoreMapping(Type fromType, Type toType)
4844
{
@@ -108,6 +104,9 @@ public static object ConvertTo(this object from, Type toType, bool skipConverter
108104
if (fromType == toType)
109105
return from;
110106

107+
if (ShouldIgnoreMapping(fromType, toType))
108+
return null;
109+
111110
if (!skipConverters)
112111
{
113112
var converter = GetConverter(fromType, toType);
@@ -781,6 +780,29 @@ public void AddMatch(string name, AssignmentMember readMember, AssignmentMember
781780
if (AutoMappingUtils.ShouldIgnoreMapping(readMember.Type,writeMember.Type))
782781
return;
783782

783+
// Ignore mapping collections if Element Types are ignored
784+
if (typeof(IEnumerable).IsAssignableFrom(readMember.Type) && typeof(IEnumerable).IsAssignableFrom(writeMember.Type))
785+
{
786+
var fromGenericDef = readMember.Type.GetTypeWithGenericTypeDefinitionOf(typeof(IDictionary<,>));
787+
var toGenericDef = readMember.Type.GetTypeWithGenericTypeDefinitionOf(typeof(IDictionary<,>));
788+
if (fromGenericDef != null && toGenericDef != null)
789+
{
790+
// Check if to/from Key or Value Types are ignored
791+
if (AutoMappingUtils.ShouldIgnoreMapping(fromGenericDef.GetGenericArguments()[0],toGenericDef.GetGenericArguments()[0]))
792+
return;
793+
if (AutoMappingUtils.ShouldIgnoreMapping(fromGenericDef.GetGenericArguments()[1],toGenericDef.GetGenericArguments()[1]))
794+
return;
795+
}
796+
else if (readMember.Type != typeof(string) && writeMember.Type != typeof(string))
797+
{
798+
var elFromType = readMember.Type.GetCollectionType();
799+
var elToType = writeMember.Type.GetCollectionType();
800+
801+
if (AutoMappingUtils.ShouldIgnoreMapping(elFromType,elToType))
802+
return;
803+
}
804+
}
805+
784806
this.AssignmentMemberMap[name] = new AssignmentEntry(name, readMember, writeMember);
785807
}
786808

tests/ServiceStack.Text.Tests/AutoMappingCustomConverterTests.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,10 @@ public void Can_Convert_POCO_collections_with_custom_Converter()
157157
var users = new UsersData {
158158
Id = 1,
159159
User = user,
160-
Users = new List<User> { user }
160+
Users = { user }
161161
};
162162

163163
var dtoUsers = users.ConvertTo<UsersDto>();
164-
165-
dtoUsers.PrintDump();
166-
167164
Assert.That(dtoUsers.Id, Is.EqualTo(users.Id));
168165

169166
void AssertUser(UserDto userDto)
@@ -180,7 +177,33 @@ void AssertUser(UserDto userDto)
180177
}
181178

182179
[Test]
183-
public void Can_ignore_converting_collections()
180+
public void Does_ignore_POCO_mappings()
181+
{
182+
AutoMappingUtils.IgnoreMapping<User, UserDto>();
183+
184+
var user = new User {
185+
FirstName = "John",
186+
LastName = "Doe",
187+
Car = new Car { Name = "BMW X6", Age = 3 }
188+
};
189+
var users = new UsersData {
190+
Id = 1,
191+
User = user,
192+
Users = { user }
193+
};
194+
195+
var dtoUsers = users.ConvertTo<UsersDto>();
196+
Assert.That(dtoUsers.Id, Is.EqualTo(users.Id));
197+
198+
Assert.That(user.ConvertTo<UserDto>(), Is.Null);
199+
Assert.That(dtoUsers.User, Is.Null);
200+
Assert.That(dtoUsers.Users, Is.Empty);
201+
202+
AutoMappingUtils.Reset();
203+
}
204+
205+
[Test]
206+
public void Does_ignore_collection_mappings()
184207
{
185208
AutoMappingUtils.IgnoreMapping<List<User>, List<UserDto>>();
186209

@@ -199,7 +222,7 @@ public void Can_ignore_converting_collections()
199222
dtoUsers.PrintDump();
200223

201224
Assert.That(dtoUsers.Id, Is.EqualTo(users.Id));
202-
Assert.That(dtoUsers.Users, Is.Null);
225+
Assert.That(dtoUsers.Users, Is.Empty);
203226

204227
AutoMappingUtils.Reset();
205228
}

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ public class UsersData
6767
public int Id { get; set; }
6868

6969
public User User { get; set; }
70-
public List<User> Users { get; set; }
70+
public List<User> Users { get; set; } = new List<User>();
7171
}
7272
public class UsersDto
7373
{
7474
public int Id { get; set; }
7575

7676
public UserDto User { get; set; }
77-
public List<UserDto> Users { get; set; }
77+
public List<UserDto> Users { get; set; } = new List<UserDto>();
7878
}
7979

8080
public enum Color

0 commit comments

Comments
 (0)