Skip to content
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
41 changes: 31 additions & 10 deletions src/AutoMapper/Execution/TypeMapPlanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,14 @@ private void AddPathMaps(List<Expression> actions)
continue;
}

actions.Add(TryPathMap(pathMap));
try
{
actions.Add(TryPathMap(pathMap));
}
catch (Exception e) when (e is not AutoMapperConfigurationException)
{
throw new AutoMapperMappingException("Error building path mapping strategy.", e, pathMap);
}
}
}

Expand Down Expand Up @@ -287,14 +294,21 @@ private void AddPropertyMaps(List<Expression> actions)
continue;
}

var property = TryMemberMap(propertyMap,
CreatePropertyMapFunc(propertyMap, _destination, propertyMap.DestinationMember));
if (_typeMap.ConstructorParameterMatches(propertyMap.DestinationName))
try
{
property = _initialDestination.IfNullElse(_configuration.Default(property.Type), property);
var property = TryMemberMap(propertyMap,
CreatePropertyMapFunc(propertyMap, _destination, propertyMap.DestinationMember));
if (_typeMap.ConstructorParameterMatches(propertyMap.DestinationName))
{
property = _initialDestination.IfNullElse(_configuration.Default(property.Type), property);
}

actions.Add(property);
}
catch (Exception e) when (e is not AutoMapperConfigurationException)
{
throw new AutoMapperMappingException("Error building member mapping strategy.", e, propertyMap);
}

actions.Add(property);
}
}

Expand Down Expand Up @@ -366,9 +380,16 @@ private Expression ConstructorMapping(ConstructorMap constructorMap)
List<Expression> body = [];
foreach (var parameter in constructorMap.CtorParams)
{
var variable = Variable(parameter.DestinationType, parameter.DestinationName);
variables.Add(variable);
body.Add(Assign(variable, CreateConstructorParameterExpression(parameter)));
try
{
var variable = Variable(parameter.DestinationType, parameter.DestinationName);
variables.Add(variable);
body.Add(Assign(variable, CreateConstructorParameterExpression(parameter)));
}
catch (Exception e) when (e is not AutoMapperConfigurationException)
{
throw new AutoMapperMappingException("Error building constructor parameter mapping strategy.", e, parameter);
}
}

body.Add(CheckReferencesCache(New(constructorMap.Ctor, variables)));
Expand Down
26 changes: 22 additions & 4 deletions src/AutoMapper/QueryableExtensions/ProjectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,18 @@ void ProjectProperties()
{
continue;
}
var propertyProjection = TryProjectMember(propertyMap);
if(propertyProjection != null)

try
{
propertiesProjections.Add(Bind(propertyMap.DestinationMember, propertyProjection));
var propertyProjection = TryProjectMember(propertyMap);
if(propertyProjection != null)
{
propertiesProjections.Add(Bind(propertyMap.DestinationMember, propertyProjection));
}
}
catch (Exception e) when (e is not AutoMapperConfigurationException)
{
throw new AutoMapperMappingException("Error building queryable mapping strategy.", e, propertyMap);
}
}
}
Expand Down Expand Up @@ -209,7 +217,17 @@ IProjectionMapper GetProjectionMapper()
{
{ CustomCtorExpression: LambdaExpression ctorExpression } => (NewExpression)ctorExpression.ReplaceParameters(instanceParameter),
{ ConstructorMap: { CanResolve: true } constructorMap } =>
New(constructorMap.Ctor, constructorMap.CtorParams.Select(map => TryProjectMember(map, map.DefaultValue(null)) ?? Default(map.DestinationType))),
New(constructorMap.Ctor, constructorMap.CtorParams.Select(map =>
{
try
{
return TryProjectMember(map, map.DefaultValue(null)) ?? Default(map.DestinationType);
}
catch (Exception e) when (e is not AutoMapperConfigurationException)
{
throw new AutoMapperMappingException("Error building constructor projection strategy.", e, map);
}
})),
_ => New(typeMap.DestinationType)
};
}
Expand Down
11 changes: 9 additions & 2 deletions src/AutoMapper/TypeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,15 @@ public void Seal(IGlobalConfiguration configuration)
return;
}
_sealed = true;
_details?.Seal(configuration, this);
MapExpression = Projection ? EmptyLambda : CreateMapperLambda(configuration);
try
{
_details?.Seal(configuration, this);
MapExpression = Projection ? EmptyLambda : CreateMapperLambda(configuration);
}
catch (Exception e) when (e is not AutoMapperConfigurationException)
{
throw new AutoMapperMappingException("Error creating mapping strategy.", e, this);
}
SourceTypeDetails = null;
DestinationTypeDetails = null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/UnitTests/ForPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,11 @@ public class SourceModel
[Fact]
public void Should_throw_exception()
{
Assert.Throws<NullReferenceException>(() =>
Assert.Throws<AutoMapperMappingException>(() =>
{
var cfg = new MapperConfiguration(config =>
{
Assert.Throws<System.ArgumentNullException>(() =>
Assert.Throws<ArgumentNullException>(() =>
{
config.CreateMap<SourceModel, DestinationModel>()
.ForPath(sourceModel => sourceModel.Name, opts => opts.MapFrom<string>(null));
Expand Down