Skip to content

ResolutionContext lost in nested mapping #2937

@riiight

Description

@riiight

When using MapFrom(), any IMappingOperationOptions.Items that are passed in are not passed along to nested ResolutionContext.Mapper.Map() calls. I realize that I can then manually readd the Items to the nested Map() call, but I believe it should happen automatically.

(NOTE: The example below is extremely simplified. You may think, "these mappings should just be handled through _____ (e.g. conventions)", but just keep in mind the main point is that the IMappingOperationOptions.Items are lost.

Source/destination types

public class FromGarage
	{
		public List<FromCar> FromCars { get; set; }
	}

public class ToGarage
	{
		public List<ToCar> ToCars { get; set; }
	}

public class FromCar
	{
		public int Id { get; set; }
		public string Name { get; set; }
		public Door Door { get; set; }
	}

public class ToCar
	{
		public int Id { get; set; }
		public string Name { get; set; }
		public Door Door { get; set; }
	}

Mapping configuration

CreateMap<FromGarage, ToGarage>()
                .ForMember(dest => dest.ToCars, opts => opts.MapFrom((src, dest, destVal, ctx) => 
                {
                    var toCars= new List<ToCar>();

                    ToCar toCar;
                    foreach (var fromCar in src.FromCars)
                    {
// I KNOW I COULD PASS IN THE ITEM "DOOR" MANUALLY RIGHT HERE, BUT SHOULDN'T THIS HAPPEN AUTOMATICALLY?
                        toCar = ctx.Mapper.Map<ToCar>(fromCar);
                        if (toCar == null)
                            continue;

                        toCars.Add(toCar);
                    }

                    return toCars;
                }));

CreateMap<FromCar, ToCar>()
                .ConvertUsing((src, dest, ctx) =>
                {
                    ToCar toCar = null;

                    if (fromCar.Name != null)
                    {
                        toCar.Id= fromCar.Id;
                        toCar.Name = fromCar.Name;

                            // ERROR OCCURS HERE. ITEMS ARE NOT THERE.
                            var door = ((Door)ctx.Items["Door"]);

                        toCar.Door = door;
                    }

                    return toCar;
                });

Version: 8.0.0

Expected behavior

I would expect that the nested "ctx.Mapper.Map" call would pass along the IMappingOperationOptions.Items that were passed to it via the ResolutionContext.

Actual behavior

The ResolutionContext of the nested "ctx.Mapper.Map" call does not contain any of the Items that were originally passed in.

Steps to reproduce

var toGarage = _mapper.Map<ToGarage>(fromGarage, opts =>
            {
                opts.Items.Add("Door", _door);
            });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions