Skip to content

Fix EnableEnumMappingValidation with non-enum configurations #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using AutoMapper.Extensions.EnumMapping.Tests.Internal;

namespace AutoMapper.Extensions.EnumMapping.Tests;

public class EnumMappingValidation
{
public class Default : AutoMapperSpecBase
{
protected override MapperConfiguration Configuration { get; } = new(cfg =>
{
cfg.EnableEnumMappingValidation();
cfg.CreateMap<object, object>();
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using AutoMapper.Extensions.EnumMapping.Internal;
using AutoMapper.Extensions.EnumMapping.Internal;
using AutoMapper.Internal;

namespace AutoMapper.Extensions.EnumMapping
Expand All @@ -17,16 +16,9 @@ public static void EnableEnumMappingValidation(this IMapperConfigurationExpressi
{
mapperConfigurationExpression.Internal().Validator(context =>
{
if (context.TypeMap != null)
{
foreach (var feature in context.TypeMap.Features)
{
if (feature is IEnumMappingValidationRuntimeFeature validator)
{
validator.Validate(context.TypeMap.Types);
}
}
}
var validator = context.TypeMap?.Features.Get<EnumMappingValidationRuntimeFeatureProxy>();

validator?.Validate(context.TypeMap.Types);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public void Configure(TypeMap typeMap)

var lambdaExpression = new CustomMapExpressionFactory<TSource, TDestination>(enumValueMappings).Create();
typeMap.TypeConverter = new ExpressionTypeConverter(lambdaExpression);
typeMap.Features.Set(new EnumMappingValidationRuntimeFeature<TSource, TDestination>(enumValueMappings, EnumMappingType));
typeMap.Features.Set(new EnumMappingValidationRuntimeFeatureProxy(
new EnumMappingValidationRuntimeFeature<TSource, TDestination>(enumValueMappings, EnumMappingType)
));
}

private Dictionary<TSource, TDestination> CreateOverridedEnumValueMappings(Type sourceType, Type destinationType)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using AutoMapper.Internal;

namespace AutoMapper.Extensions.EnumMapping.Internal;

internal class EnumMappingValidationRuntimeFeatureProxy : IEnumMappingValidationRuntimeFeature
{
private readonly IEnumMappingValidationRuntimeFeature _innerValidationRuntimeFeature;

public EnumMappingValidationRuntimeFeatureProxy(IEnumMappingValidationRuntimeFeature innerValidationRuntimeFeature)
{
_innerValidationRuntimeFeature = innerValidationRuntimeFeature;
}

public void Seal(IGlobalConfiguration configurationProvider) => _innerValidationRuntimeFeature.Seal(configurationProvider);

public void Validate(TypePair typePair) => _innerValidationRuntimeFeature.Validate(typePair);
}