Skip to content

Commit 6bb05d5

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

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

src/AutoMapper/Configuration/Conventions.cs

Lines changed: 5 additions & 7 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);
@@ -45,10 +45,6 @@ public void Seal()
4545
}
4646
public void Merge(MemberConfiguration other)
4747
{
48-
if (other == null)
49-
{
50-
return;
51-
}
5248
var initialCount = NameToMemberMappers.Count;
5349
for (int index = 0; index < other.NameToMemberMappers.Count; index++)
5450
{
@@ -64,6 +60,8 @@ public void Merge(MemberConfiguration other)
6460
}
6561
NameToMemberMappers.Add(otherMapper);
6662
}
63+
SourceNamingConvention ??= other.SourceNamingConvention;
64+
DestinationNamingConvention ??= other.DestinationNamingConvention;
6765
}
6866
}
6967
public sealed class PrePostfixName : ISourceToDestinationNameMapper

src/AutoMapper/ProfileMap.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ public ProfileMap(IProfileConfiguration profile, IGlobalConfigurationExpression
2727
ValueTransformers = profile.ValueTransformers.Concat(configuration?.ValueTransformers).ToArray();
2828
var profileInternal = (IProfileExpressionInternal)profile;
2929
MemberConfiguration = profileInternal.MemberConfiguration;
30-
MemberConfiguration.Merge(configuration.Internal()?.MemberConfiguration);
30+
if(configuration == null)
31+
{
32+
MemberConfiguration.SourceNamingConvention ??= PascalCaseNamingConvention.Instance;
33+
MemberConfiguration.DestinationNamingConvention ??= PascalCaseNamingConvention.Instance;
34+
}
35+
else
36+
{
37+
MemberConfiguration.Merge(configuration.Internal().MemberConfiguration);
38+
}
3139
var globalIgnores = profile.GlobalIgnores.Concat(globalProfile?.GlobalIgnores);
3240
GlobalIgnores = globalIgnores == Array.Empty<string>() ? EmptyHashSet : new HashSet<string>(globalIgnores);
3341
SourceExtensionMethods = profile.SourceExtensionMethods.Concat(globalProfile?.SourceExtensionMethods).ToArray();

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)