Skip to content
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

Map to a list of destination object that set properties in the ctor throws an AutoMapperMappingException #264

Closed
jibedoubleve opened this issue Oct 29, 2012 · 1 comment
Milestone

Comments

@jibedoubleve
Copy link

Here's a small unit test that reproduces the issue (It throws a AutoMapperMappingException):

    Mapper.Reset();
    Mapper.CreateMap<ItemToMap, ItemToMapDto>();
    Mapper.CreateMap<Tag, TagDto>();

    var tag = new Tag() ;

    List<ItemToMap> entities = new List<ItemToMap>();

    for (int i = 0; i < 10; i++)
    {
        entities.Add(new ItemToMap()
        {
            Name = Guid.NewGuid().ToString(),
            Tag = tag,
        });
    }

    var dto = Mapper.Map<List<ItemToMap>, List<ItemToMapDto>>(entities);

Here are the classes

public class ItemToMapDto
{
    public ItemToMapDto()
    {
        /* Remove the line below and the mapping works correctly*/
        this.Tag = new TagDto() { Name = Guid.NewGuid().ToString() };
    }
    public string Name { get; set; }
    public TagDto Tag { get; set; }
}
public class TagDto
{
    public string Name { get; set; }
    public bool IsTrue { get; set; }
}
public class ItemToMap
{
    public string Name { get; set; }
    public Tag Tag { get; set; }
}
public class Tag
{
    public string Name { get; set; }
    public bool IsTrue { get; set; }
}

In AutoMapper version 1.1, in the PropertyMapMappingStrategy there was a check on UseDestinationValue to see whether to replace the existing value or not. Now it doesn't exist anymore.

When choosing the mapping strategy the method IsMatch returns False on the CacheMappingStrategy because DestinationValue is not null. But in the PropertyMapMappingStrategy, this same method crashes because a ResolutionContext with the same hash code already exists in the cache.

I don't have the problem anymore if I replace the line 111 in the file TypeMapObjectMapperRegistry.cs (PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, object mappedObject, PropertyMap propertyMap):

object destinationValue = propertyMap.DestinationProperty.GetValue(mappedObject);

with this one:

object destinationValue = (propertyMap.UseDestinationValue)
    ? propertyMap.DestinationProperty.GetValue(mappedObject)
    : null;
@lock
Copy link

lock bot commented May 8, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators May 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants