Skip to content

Commit

Permalink
the default naming conventions for a profile should come from the glo…
Browse files Browse the repository at this point in the history
…bal configuration
  • Loading branch information
lbargaoanu committed Apr 6, 2024
1 parent 1fbdf63 commit 6bb05d5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
12 changes: 5 additions & 7 deletions src/AutoMapper/Configuration/Conventions.cs
Expand Up @@ -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<ISourceToDestinationNameMapper> NameToMemberMappers { get; } = new();
public INamingConvention SourceNamingConvention { get; set; }
public INamingConvention DestinationNamingConvention { get; set; }
public List<ISourceToDestinationNameMapper> NameToMemberMappers { get; } = [];
public bool IsMatch(ProfileMap options, TypeDetails sourceTypeDetails, Type destType, Type destMemberType, string nameToSearch, List<MemberInfo> resolvers, bool isReverseMap)
{
var matchingMemberInfo = GetSourceMember(sourceTypeDetails, destType, destMemberType, nameToSearch);
Expand Down Expand Up @@ -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++)
{
Expand All @@ -64,6 +60,8 @@ public void Merge(MemberConfiguration other)
}
NameToMemberMappers.Add(otherMapper);
}
SourceNamingConvention ??= other.SourceNamingConvention;
DestinationNamingConvention ??= other.DestinationNamingConvention;
}
}
public sealed class PrePostfixName : ISourceToDestinationNameMapper
Expand Down
10 changes: 9 additions & 1 deletion src/AutoMapper/ProfileMap.cs
Expand Up @@ -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<string>() ? EmptyHashSet : new HashSet<string>(globalIgnores);
SourceExtensionMethods = profile.SourceExtensionMethods.Concat(globalProfile?.SourceExtensionMethods).ToArray();
Expand Down
42 changes: 41 additions & 1 deletion src/UnitTests/Bug/NamingConventions.cs
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Neda, Dario>();
});
cfg.CreateProfile("MyMapperProfile2", prf =>
{
prf.SourceMemberNamingConvention = PascalCaseNamingConvention.Instance;
prf.CreateMap<Dario, Neda>();
});
});

protected override void Because_of()
{
_dario = Mapper.Map<Neda, Dario>(new Neda { ja_se_zovem_imenom = "foo" });
_neda = Mapper.Map<Dario, Neda>(_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()
{
Expand Down

0 comments on commit 6bb05d5

Please sign in to comment.