Skip to content

fix: discover underlying mappings for nullable types #140

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

Closed
wants to merge 3 commits into from
Closed
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
29 changes: 23 additions & 6 deletions src/AutoMapper.Extensions.ExpressionMapping/MapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,29 @@ private static bool HasUnderlyingType(this Type type)

private static void AddUnderlyingTypes(this Dictionary<Type, Type> typeMappings, IConfigurationProvider configurationProvider, Type sourceType, Type destType)
{
typeMappings.DoAddTypeMappings
(
configurationProvider,
!sourceType.HasUnderlyingType() ? new List<Type>() : ElementTypeHelper.GetElementTypes(sourceType).ToList(),
!destType.HasUnderlyingType() ? new List<Type>() : ElementTypeHelper.GetElementTypes(destType).ToList()
);
if (sourceType.IsNullableType() || destType.IsNullableType())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of adding literal types to typeMappings. Is there a chance of unwanted conversions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're expressly checking below for a user-defined mapping between the two... It seems unlikely

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need more tests. I suspect if you're mapping a LINQ select for a class like this one say you want some integers converted but not others then there may be problems.

{
Type underlyingSourceType = Nullable.GetUnderlyingType(sourceType) ?? sourceType;
Type underlyingDestType = Nullable.GetUnderlyingType(destType) ?? destType;
if (configurationProvider.Internal().ResolveTypeMap(underlyingSourceType, underlyingDestType) != null)
{
typeMappings.AddTypeMapping
(
configurationProvider,
underlyingSourceType,
underlyingDestType
);
}
}
else
{
typeMappings.DoAddTypeMappings
(
configurationProvider,
!sourceType.HasUnderlyingType() ? new List<Type>() : ElementTypeHelper.GetElementTypes(sourceType).ToList(),
!destType.HasUnderlyingType() ? new List<Type>() : ElementTypeHelper.GetElementTypes(destType).ToList()
);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Xunit;

namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
Expand Down Expand Up @@ -243,6 +242,37 @@ public void Can_map_local_variable_nullable_in_filter()
//Assert
Assert.NotNull(expMapped);
}

[Fact]
public void Can_map_struct_with_asymmetric_nullability()
{
// Arrange
var config = new MapperConfiguration(c =>
{
c.CreateMap<Instant, DateTimeOffset?>()
.ForMember(d => d.HasValue, c => c.MapFrom(_ => true))
.ConvertUsing(i => DateTimeOffset.FromUnixTimeSeconds(i.SecondsSinceUnixEpoch));
c.CreateMap<DateTimeOffset?, Instant>().ConvertUsing(d => d.HasValue ? new Instant { SecondsSinceUnixEpoch = d.Value.ToUnixTimeSeconds() } : default);
c.CreateMap<DateTimeOffset, Instant>().ConvertUsing(d => new Instant { SecondsSinceUnixEpoch = d.ToUnixTimeSeconds() });

c.CreateMap<ItemWithInstant, ItemWithDateTimeOffset>();
c.CreateMap<ItemWithDateTimeOffset, ItemWithInstant>();
});

config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
DateTimeOffset firstReleaseDate = DateTimeOffset.FromUnixTimeSeconds(0);
DateTimeOffset lastReleaseDate = DateTimeOffset.FromUnixTimeSeconds(10);
Expression<Func<ItemWithDateTimeOffset, bool>> exp = x => x.Date.HasValue && (x.Date >= firstReleaseDate && x.Date <= lastReleaseDate);

//Act
Expression<Func<ItemWithInstant, bool>> expMapped = mapper.MapExpression<Expression<Func<ItemWithInstant, bool>>>(exp);

//Assert
Assert.NotNull(expMapped);
Assert.True(expMapped.Compile()(new ItemWithInstant { Date = new Instant { SecondsSinceUnixEpoch = 7 } }));
Assert.False(expMapped.Compile()(new ItemWithInstant { Date = new Instant { SecondsSinceUnixEpoch = 12 } }));
}
}

public struct Source
Expand Down Expand Up @@ -460,6 +490,36 @@ public struct ItemWithDateLiteral
public DateTime Date { get; set; }
}

public struct ItemWithDateTimeOffset
{
public DateTimeOffset? Date { get; set; }
}

public struct ItemWithInstant
{
public Instant Date { get; set; }
}

public struct Instant
{
public long SecondsSinceUnixEpoch { get; set; }

public static bool operator >=(Instant self, Instant other) => self.SecondsSinceUnixEpoch >= other.SecondsSinceUnixEpoch;
public static bool operator <=(Instant self, Instant other) => self.SecondsSinceUnixEpoch <= other.SecondsSinceUnixEpoch;
public static bool operator !=(Instant self, Instant other) => self.SecondsSinceUnixEpoch != other.SecondsSinceUnixEpoch;
public static bool operator ==(Instant self, Instant other) => self.SecondsSinceUnixEpoch == other.SecondsSinceUnixEpoch;

public override bool Equals(object obj)
{
return obj is Instant instant && instant.SecondsSinceUnixEpoch == SecondsSinceUnixEpoch;
}

public override int GetHashCode()
{
return SecondsSinceUnixEpoch.GetHashCode();
}
}

static class Extensions
{
internal static ICollection<TModel> GetItems<TModel, TData>(this IQueryable<TData> query, IMapper mapper,
Expand Down