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

InvalidCastException with multi-dimensional arrays #3593

Closed
burns140 opened this issue Mar 4, 2021 · 2 comments
Closed

InvalidCastException with multi-dimensional arrays #3593

burns140 opened this issue Mar 4, 2021 · 2 comments
Labels
Milestone

Comments

@burns140
Copy link

burns140 commented Mar 4, 2021

gist: https://gist.github.com/burns140/53b8c0c571534b7af0192ee329ecec60

Source/destination types

// Put your source/destination types here

//Source
public class ResultSet {
  public string[,] IDMap { get; set; }
}

//Dest
public class DTO {
  public string[,] IDMap { get; set; }
}

Mapping configuration

// Mapper.Initialize or just the CreateMap snippet 
var config = new MapperConfiguration(c => {
    c.CreateMap<ResultSet, DTO>().ReverseMap();
});

Version: x.y.z

4.2.1

Expected behavior

A null result would map to an empty 2d array of string[,]

Actual behavior

The null mapped to an empty 1d array of string[] then threw an inner System.InvalidCastException trying to map from string[] to string[,]

Steps to reproduce

Create two classes with one of the fields being a multidimensional array. Set the value of that multidimensional array in the instance of the source class to null, then attempt to map it to the destination class.

// Your calls to Mapper.Map or ProjectTo here, with source/destination objects constructed
var source = new ResultSet {
    IDMap = null
};
  
try {
    var dest = mapper.Map<DTO>(source);
 } catch (Exception ex) {
    ex.ToString();
}

Stacktrace:

AutoMapper.AutoMapperMappingException: 

Mapping types:
String[,] -> String[,]
System.String[,] -> System.String[,]

Destination path:
ResultSet.IDMap.IDMap

Source value:
(null) ---> System.InvalidCastException: Unable to cast object of type 'System.String[]' to type 'System.String[,]'.
   at lambda_method(Closure , Object , Object )
   at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.AssignValue(PropertyMap propertyMap, Object mappedObject, Object propertyValueToAssign)
   at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, Object mappedObject, PropertyMap propertyMap)
   --- End of inner exception stack trace ---
@burns140 burns140 changed the title A null value for a multi-dimensional array results in an empty one-dimensional array, resulting in an InvalidCastException. A null value for a multi-dimensional array results in an empty one-dimensional array, throwing an InvalidCastException. Mar 4, 2021
@lbargaoanu
Copy link
Member

Try the MyGet build.

@lbargaoanu lbargaoanu changed the title A null value for a multi-dimensional array results in an empty one-dimensional array, throwing an InvalidCastException. InvalidCastException with multi-dimensional arrays Mar 5, 2021
@lbargaoanu lbargaoanu added this to the 11.0.0 milestone Mar 11, 2021
@github-actions
Copy link

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants