diff --git a/src/AutoMapper/Configuration/Conventions.cs b/src/AutoMapper/Configuration/Conventions.cs index 191861472f..a57cb01af1 100644 --- a/src/AutoMapper/Configuration/Conventions.cs +++ b/src/AutoMapper/Configuration/Conventions.cs @@ -8,9 +8,9 @@ public interface ISourceToDestinationNameMapper public sealed class MemberConfiguration { NameSplitMember _nameSplitMember; - public INamingConvention SourceNamingConvention { get; set; } = PascalCaseNamingConvention.Instance; - public INamingConvention DestinationNamingConvention { get; set; } = PascalCaseNamingConvention.Instance; - public List NameToMemberMappers { get; } = new(); + public INamingConvention SourceNamingConvention { get; set; } + public INamingConvention DestinationNamingConvention { get; set; } + public List NameToMemberMappers { get; } = []; public bool IsMatch(ProfileMap options, TypeDetails sourceTypeDetails, Type destType, Type destMemberType, string nameToSearch, List resolvers, bool isReverseMap) { var matchingMemberInfo = GetSourceMember(sourceTypeDetails, destType, destMemberType, nameToSearch); @@ -45,10 +45,6 @@ public void Seal() } public void Merge(MemberConfiguration other) { - if (other == null) - { - return; - } var initialCount = NameToMemberMappers.Count; for (int index = 0; index < other.NameToMemberMappers.Count; index++) { @@ -64,6 +60,8 @@ public void Merge(MemberConfiguration other) } NameToMemberMappers.Add(otherMapper); } + SourceNamingConvention ??= other.SourceNamingConvention; + DestinationNamingConvention ??= other.DestinationNamingConvention; } } public sealed class PrePostfixName : ISourceToDestinationNameMapper diff --git a/src/AutoMapper/ProfileMap.cs b/src/AutoMapper/ProfileMap.cs index 5b0ae92417..3e05b50645 100644 --- a/src/AutoMapper/ProfileMap.cs +++ b/src/AutoMapper/ProfileMap.cs @@ -27,7 +27,15 @@ public ProfileMap(IProfileConfiguration profile, IGlobalConfigurationExpression ValueTransformers = profile.ValueTransformers.Concat(configuration?.ValueTransformers).ToArray(); var profileInternal = (IProfileExpressionInternal)profile; MemberConfiguration = profileInternal.MemberConfiguration; - MemberConfiguration.Merge(configuration.Internal()?.MemberConfiguration); + if(configuration == null) + { + MemberConfiguration.SourceNamingConvention ??= PascalCaseNamingConvention.Instance; + MemberConfiguration.DestinationNamingConvention ??= PascalCaseNamingConvention.Instance; + } + else + { + MemberConfiguration.Merge(configuration.Internal().MemberConfiguration); + } var globalIgnores = profile.GlobalIgnores.Concat(globalProfile?.GlobalIgnores); GlobalIgnores = globalIgnores == Array.Empty() ? EmptyHashSet : new HashSet(globalIgnores); SourceExtensionMethods = profile.SourceExtensionMethods.Concat(globalProfile?.SourceExtensionMethods).ToArray(); diff --git a/src/UnitTests/Bug/NamingConventions.cs b/src/UnitTests/Bug/NamingConventions.cs index b10629bb0a..1344b0c8d8 100644 --- a/src/UnitTests/Bug/NamingConventions.cs +++ b/src/UnitTests/Bug/NamingConventions.cs @@ -67,7 +67,7 @@ public class Dario public string JaSeZovemImenom { get; set; } } -public class When_mapping_with_lowercae_naming_conventions_two_ways_in_profiles : AutoMapperSpecBase +public class When_mapping_with_lowercase_naming_conventions_two_ways_in_profiles : AutoMapperSpecBase { private Dario _dario; private Neda _neda; @@ -98,6 +98,46 @@ public void Should_map_from_lower_to_pascal() _neda.ja_se_zovem_imenom.ShouldBe("foo"); } + [Fact] + public void Should_map_from_pascal_to_lower() + { + _dario.JaSeZovemImenom.ShouldBe("foo"); + } +} + +public class When_mapping_with_lowercase_naming_conventions_two_ways : AutoMapperSpecBase +{ + private Dario _dario; + private Neda _neda; + + protected override MapperConfiguration CreateConfiguration() => new(cfg => + { + cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention(); + cfg.DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention(); + cfg.CreateProfile("MyMapperProfile", prf => + { + prf.DestinationMemberNamingConvention = PascalCaseNamingConvention.Instance; + prf.CreateMap(); + }); + cfg.CreateProfile("MyMapperProfile2", prf => + { + prf.SourceMemberNamingConvention = PascalCaseNamingConvention.Instance; + prf.CreateMap(); + }); + }); + + protected override void Because_of() + { + _dario = Mapper.Map(new Neda { ja_se_zovem_imenom = "foo" }); + _neda = Mapper.Map(_dario); + } + + [Fact] + public void Should_map_from_lower_to_pascal() + { + _neda.ja_se_zovem_imenom.ShouldBe("foo"); + } + [Fact] public void Should_map_from_pascal_to_lower() {