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

Commit f8cbb40

Browse files
committed
fix finding get only collection when there is an object defined before the list
1 parent 2c97de0 commit f8cbb40

6 files changed

Lines changed: 181 additions & 11 deletions

File tree

MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/MappingGeneratorTestCases.Designer.cs

Lines changed: 58 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/MappingGeneratorTestCases.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,10 @@
322322
<data name="_033_ForeachMappingWithDictionary_FIXED" type="System.Resources.ResXFileRef, System.Windows.Forms">
323323
<value>TestCaseData\033_ForeachMappingWithDictionary_FIXED.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
324324
</data>
325+
<data name="_034_ForeachMappingObjectBeforeList" type="System.Resources.ResXFileRef, System.Windows.Forms">
326+
<value>TestCaseData\034_ForeachMappingObjectBeforeList.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
327+
</data>
328+
<data name="_034_ForeachMappingObjectBeforeList_FIXED" type="System.Resources.ResXFileRef, System.Windows.Forms">
329+
<value>TestCaseData\034_ForeachMappingObjectBeforeList_FIXED.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
330+
</data>
325331
</root>

MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/MappingGeneratorTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ public void should_be_able_to_use_add_for_get_only_collection_from_dictionary_va
209209
TestCodeRefactoring(_033_ForeachMappingWithDictionary, _033_ForeachMappingWithDictionary_FIXED);
210210
}
211211

212+
[Test]
213+
public void should_be_able_to_use_add_for_get_only_collection_with_single_object_before_list()
214+
{
215+
TestCodeRefactoring(_034_ForeachMappingObjectBeforeList, _034_ForeachMappingObjectBeforeList_FIXED);
216+
}
217+
212218
protected override string LanguageName => LanguageNames.CSharp;
213219

214220
protected override CodeRefactoringProvider CreateProvider()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Collections.ObjectModel;
5+
6+
namespace MappingGenerator.Test.MappingGenerator.TestCaseData
7+
{
8+
public class TestMapper
9+
{
10+
public static UserDTO [|Map|](UserEntity entity)
11+
{
12+
throw new NotImplementedException();
13+
}
14+
}
15+
16+
public class UserDTO
17+
{
18+
public int Age { get; set; }
19+
public AccountDTO Account { get; set; }
20+
public List<AccountDTO> Accounts { get; } = new List<AccountDTO>();
21+
}
22+
23+
public class UserEntity
24+
{
25+
public int Age { get; set; }
26+
public AccountEntity Account { get; set; }
27+
public List<AccountEntity> Accounts { get; set; }
28+
}
29+
30+
public class AccountDTO
31+
{
32+
public string BankName { get; set; }
33+
public string Number { get; set; }
34+
}
35+
36+
public class AccountEntity
37+
{
38+
public string BankName { get; set; }
39+
public string Number { get; set; }
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Collections.ObjectModel;
5+
6+
namespace MappingGenerator.Test.MappingGenerator.TestCaseData
7+
{
8+
public class TestMapper
9+
{
10+
public static UserDTO Map(UserEntity entity)
11+
{
12+
var userDTO = new UserDTO()
13+
{
14+
Age = entity.Age,
15+
Account = new AccountDTO()
16+
{
17+
BankName = entity.Account.BankName,
18+
Number = entity.Account.Number
19+
}
20+
};
21+
foreach (var account in entity.Accounts)
22+
{
23+
var accountDTO = new AccountDTO()
24+
{
25+
BankName = account.BankName,
26+
Number = account.Number
27+
};
28+
userDTO.Accounts.Add(accountDTO);
29+
}
30+
31+
return userDTO;
32+
}
33+
}
34+
35+
public class UserDTO
36+
{
37+
public int Age { get; set; }
38+
public AccountDTO Account { get; set; }
39+
public List<AccountDTO> Accounts { get; } = new List<AccountDTO>();
40+
}
41+
42+
public class UserEntity
43+
{
44+
public int Age { get; set; }
45+
public AccountEntity Account { get; set; }
46+
public List<AccountEntity> Accounts { get; set; }
47+
}
48+
49+
public class AccountDTO
50+
{
51+
public string BankName { get; set; }
52+
public string Number { get; set; }
53+
}
54+
55+
public class AccountEntity
56+
{
57+
public string BankName { get; set; }
58+
public string Number { get; set; }
59+
}
60+
}

MappingGenerator/MappingGenerator/MappingGenerator/Mappings/MappingImplementors/SingleParameterForeachMappingMethodImplementor.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,20 @@ private bool ContainsCollectionWithoutSetter(ITypeSymbol type)
3333
{
3434
foreach (var member in type.GetMembers().Where(ObjectHelper.IsPublicPropertySymbol).OfType<IPropertySymbol>())
3535
{
36-
if (ObjectHelper.IsSimpleType(member.Type))
36+
if (ObjectHelper.IsSimpleType(member.Type) && !MappingHelper.IsCollection(member.Type))
3737
{
3838
continue;
3939
}
4040

41-
return member.SetMethod == null && MappingHelper.IsCollection(member.Type) || ContainsCollectionWithoutSetter(MappingHelper.GetElementType(member.Type));
41+
if (member.SetMethod == null)
42+
{
43+
return true;
44+
}
45+
46+
if (ContainsCollectionWithoutSetter(MappingHelper.GetElementType(member.Type)))
47+
{
48+
return true;
49+
}
4250
}
4351
return false;
4452
}

0 commit comments

Comments
 (0)