Skip to content

Commit ac55942

Browse files
committed
the default naming conventions for a profile should come from the global configuration
1 parent 1fbdf63 commit ac55942

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/AutoMapper/Configuration/Conventions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public interface ISourceToDestinationNameMapper
88
public sealed class MemberConfiguration
99
{
1010
NameSplitMember _nameSplitMember;
11-
public INamingConvention SourceNamingConvention { get; set; } = PascalCaseNamingConvention.Instance;
12-
public INamingConvention DestinationNamingConvention { get; set; } = PascalCaseNamingConvention.Instance;
13-
public List<ISourceToDestinationNameMapper> NameToMemberMappers { get; } = new();
11+
public INamingConvention SourceNamingConvention { get; set; }
12+
public INamingConvention DestinationNamingConvention { get; set; }
13+
public List<ISourceToDestinationNameMapper> NameToMemberMappers { get; } = [];
1414
public bool IsMatch(ProfileMap options, TypeDetails sourceTypeDetails, Type destType, Type destMemberType, string nameToSearch, List<MemberInfo> resolvers, bool isReverseMap)
1515
{
1616
var matchingMemberInfo = GetSourceMember(sourceTypeDetails, destType, destMemberType, nameToSearch);
@@ -47,6 +47,8 @@ public void Merge(MemberConfiguration other)
4747
{
4848
if (other == null)
4949
{
50+
SourceNamingConvention ??= PascalCaseNamingConvention.Instance;
51+
DestinationNamingConvention ??= PascalCaseNamingConvention.Instance;
5052
return;
5153
}
5254
var initialCount = NameToMemberMappers.Count;
@@ -64,6 +66,8 @@ public void Merge(MemberConfiguration other)
6466
}
6567
NameToMemberMappers.Add(otherMapper);
6668
}
69+
SourceNamingConvention ??= other.SourceNamingConvention;
70+
DestinationNamingConvention ??= other.DestinationNamingConvention;
6771
}
6872
}
6973
public sealed class PrePostfixName : ISourceToDestinationNameMapper

src/UnitTests/Bug/NamingConventions.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class Dario
6767
public string JaSeZovemImenom { get; set; }
6868
}
6969

70-
public class When_mapping_with_lowercae_naming_conventions_two_ways_in_profiles : AutoMapperSpecBase
70+
public class When_mapping_with_lowercase_naming_conventions_two_ways_in_profiles : AutoMapperSpecBase
7171
{
7272
private Dario _dario;
7373
private Neda _neda;
@@ -98,6 +98,46 @@ public void Should_map_from_lower_to_pascal()
9898
_neda.ja_se_zovem_imenom.ShouldBe("foo");
9999
}
100100

101+
[Fact]
102+
public void Should_map_from_pascal_to_lower()
103+
{
104+
_dario.JaSeZovemImenom.ShouldBe("foo");
105+
}
106+
}
107+
108+
public class When_mapping_with_lowercase_naming_conventions_two_ways : AutoMapperSpecBase
109+
{
110+
private Dario _dario;
111+
private Neda _neda;
112+
113+
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
114+
{
115+
cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
116+
cfg.DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
117+
cfg.CreateProfile("MyMapperProfile", prf =>
118+
{
119+
prf.DestinationMemberNamingConvention = PascalCaseNamingConvention.Instance;
120+
prf.CreateMap<Neda, Dario>();
121+
});
122+
cfg.CreateProfile("MyMapperProfile2", prf =>
123+
{
124+
prf.SourceMemberNamingConvention = PascalCaseNamingConvention.Instance;
125+
prf.CreateMap<Dario, Neda>();
126+
});
127+
});
128+
129+
protected override void Because_of()
130+
{
131+
_dario = Mapper.Map<Neda, Dario>(new Neda { ja_se_zovem_imenom = "foo" });
132+
_neda = Mapper.Map<Dario, Neda>(_dario);
133+
}
134+
135+
[Fact]
136+
public void Should_map_from_lower_to_pascal()
137+
{
138+
_neda.ja_se_zovem_imenom.ShouldBe("foo");
139+
}
140+
101141
[Fact]
102142
public void Should_map_from_pascal_to_lower()
103143
{

0 commit comments

Comments
 (0)