Skip to content

Commit 6a7874f

Browse files
authored
Merge pull request #84 from AutoMapper/Issue83
Fix for Issue #83. Unusual cast required after upgrading to version 4.0.0.
2 parents fef9d8b + d91aa56 commit 6a7874f

File tree

6 files changed

+114
-4
lines changed

6 files changed

+114
-4
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
- name: Set Variables
1818
run: |
19-
echo '::set-env name=VERSION_NUMBER::4.0.1-preview-1'
19+
echo '::set-env name=VERSION_NUMBER::4.0.1-preview-2'
2020
echo '::set-env name=DEPLOY_PACKAGE_URL::https://www.myget.org/F/automapperdev/api/v3/index.json'
2121
echo '::set-env name=DEPLOY_PACKAGE_API_KEY::${{ secrets.MYGET_CI_API_KEY }}'
2222
echo '::set-env name=REPO::${{ github.repository }}'

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
- name: Set Variables
1616
run: |
17-
echo '::set-env name=VERSION_NUMBER::4.0.1-preview-1'
17+
echo '::set-env name=VERSION_NUMBER::4.0.1-preview-2'
1818
echo '::set-env name=DEPLOY_PACKAGE_URL::https://api.nuget.org/v3/index.json'
1919
echo '::set-env name=DEPLOY_PACKAGE_API_KEY::${{ secrets.NUGET_API_KEY }}'
2020
echo '::set-env name=REPO::${{ github.repository }}'

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<Authors>Jimmy Bogard</Authors>
44
<LangVersion>latest</LangVersion>
5-
<VersionPrefix>4.0.1-preview-1</VersionPrefix>
5+
<VersionPrefix>4.0.1-preview-2</VersionPrefix>
66
<WarningsAsErrors>true</WarningsAsErrors>
77
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
88
</PropertyGroup>

src/AutoMapper.Extensions.ExpressionMapping/XpressionMapperVisitor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ protected override Expression VisitMember(MemberExpression node)
6565
((ConstantExpression)baseExpression).Value,
6666
node.GetPropertyFullName(),
6767
baseExpression.Type
68-
)
68+
),
69+
node.Type
6970
)
7071
);
7172
}

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapper.Structs.Tests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,57 @@ public void Can_map_listeral_child_property_of_local_variable_in_filter()
165165
//Assert
166166
Assert.NotNull(mappedfilter);
167167
}
168+
169+
[Fact]
170+
public void Can_map_local_variable_literal_in_filter()
171+
{
172+
// Arrange
173+
var config = new MapperConfiguration(c =>
174+
{
175+
c.CreateMap<ItemWithDateLiteral, ItemWithDateLiteralDto>()
176+
.ForMember(dest => dest.CreateDate, opts => opts.MapFrom(x => x.Date));
177+
});
178+
179+
config.AssertConfigurationIsValid();
180+
var mapper = config.CreateMapper();
181+
182+
DateTime firstReleaseDate = new DateTime();
183+
DateTime lastReleaseDate = new DateTime();
184+
185+
Expression<Func<ItemWithDateLiteralDto, bool>> exp = x => (firstReleaseDate == null || x.CreateDate >= firstReleaseDate) &&
186+
(lastReleaseDate == null || x.CreateDate <= lastReleaseDate);
187+
188+
//Act
189+
Expression<Func<ItemWithDateLiteral, bool>> expMapped = mapper.MapExpression<Expression<Func<ItemWithDateLiteral, bool>>>(exp);
190+
191+
//Assert
192+
Assert.NotNull(expMapped);
193+
}
194+
195+
[Fact]
196+
public void Can_map_local_variable_nullable_in_filter()
197+
{
198+
// Arrange
199+
var config = new MapperConfiguration(c =>
200+
{
201+
c.CreateMap<ItemWithDateLiteral, ItemWithDateLiteralDto>()
202+
.ForMember(dest => dest.CreateDate, opts => opts.MapFrom(x => x.Date));
203+
});
204+
205+
config.AssertConfigurationIsValid();
206+
var mapper = config.CreateMapper();
207+
DateTime? firstReleaseDate = new DateTime();
208+
DateTime? lastReleaseDate = new DateTime();
209+
210+
Expression<Func<ItemWithDateLiteralDto, bool>> exp = x => (firstReleaseDate == null || x.CreateDate >= firstReleaseDate) &&
211+
(lastReleaseDate == null || x.CreateDate <= lastReleaseDate);
212+
213+
//Act
214+
Expression<Func<ItemWithDateLiteral, bool>> expMapped = mapper.MapExpression<Expression<Func<ItemWithDateLiteral, bool>>>(exp);
215+
216+
//Assert
217+
Assert.NotNull(expMapped);
218+
}
168219
}
169220

170221
public struct Source
@@ -372,6 +423,16 @@ public override int GetHashCode()
372423
}
373424
}
374425

426+
public struct ItemWithDateLiteralDto
427+
{
428+
public DateTime CreateDate { get; set; }
429+
}
430+
431+
public struct ItemWithDateLiteral
432+
{
433+
public DateTime Date { get; set; }
434+
}
435+
375436
static class Extensions
376437
{
377438
internal static ICollection<TModel> GetItems<TModel, TData>(this IQueryable<TData> query, IMapper mapper,

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapperTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,54 @@ public void Map_accountModel_to_account()
703703
Assert.True(accounts.Count == 2);
704704
}
705705

706+
[Fact]
707+
public void Map_accountModel_to_account_with_local_nullables()
708+
{
709+
DateTime? firstReleaseDate = null;
710+
DateTime? lastReleaseDate = null;
711+
712+
Expression<Func<AccountModel, bool>> exp = x => (firstReleaseDate == null || x.DateCreated >= firstReleaseDate) &&
713+
(lastReleaseDate == null || x.DateCreated <= lastReleaseDate);
714+
715+
//Act
716+
Expression<Func<Account, bool>> expMapped = mapper.MapExpression<Expression<Func<Account, bool>>>(exp);
717+
718+
//Assert
719+
Assert.NotNull(expMapped);
720+
}
721+
722+
[Fact]
723+
public void Map_ItemDto_to_ItemDto_with_local_nullables()
724+
{
725+
DateTime? firstReleaseDate = new DateTime();
726+
DateTime? lastReleaseDate = new DateTime();
727+
728+
Expression<Func<ItemDto, bool>> exp = x => (firstReleaseDate == null || x.CreateDate >= firstReleaseDate) &&
729+
(lastReleaseDate == null || x.CreateDate <= lastReleaseDate);
730+
731+
//Act
732+
Expression<Func<Item, bool>> expMapped = mapper.MapExpression<Expression<Func<Item, bool>>>(exp);
733+
734+
//Assert
735+
Assert.NotNull(expMapped);
736+
}
737+
738+
[Fact]
739+
public void Map_ItemDto_to_ItemDto_with_local_literal_types()
740+
{
741+
DateTime firstReleaseDate = new DateTime();
742+
DateTime lastReleaseDate = new DateTime();
743+
744+
Expression<Func<ItemDto, bool>> exp = x => (firstReleaseDate == null || x.CreateDate >= firstReleaseDate) &&
745+
(lastReleaseDate == null || x.CreateDate <= lastReleaseDate);
746+
747+
//Act
748+
Expression<Func<Item, bool>> expMapped = mapper.MapExpression<Expression<Func<Item, bool>>>(exp);
749+
750+
//Assert
751+
Assert.NotNull(expMapped);
752+
}
753+
706754
[Fact]
707755
public void Map_accountModel_to_account_with_null_checks_against_value_types()
708756
{

0 commit comments

Comments
 (0)