From df1259edc7d7f716c74a507bd3e77c4d1cdfe8a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20Pi=C4=85tek?= Date: Sat, 13 Mar 2021 13:06:58 +0100 Subject: [PATCH] #168: Handle conversions between simple types wrapped in nullable like decimal? -> double --- .../TestCaseData/036_PureMappingMethodWithNullable.txt | 2 ++ .../036_PureMappingMethodWithNullable_FIXED.txt | 3 +++ .../MappingGenerator/Mappings/MappingEngine.cs | 8 +++++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/036_PureMappingMethodWithNullable.txt b/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/036_PureMappingMethodWithNullable.txt index 677f2a4..742eb54 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/036_PureMappingMethodWithNullable.txt +++ b/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/036_PureMappingMethodWithNullable.txt @@ -28,6 +28,7 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData public string? MiddleName { get; set; } public string LastName { get; set; } public int AgeValue { get; set; } + public double Weight { get; set; } public AccountDTO? Account { get; set; } public AccountDTO Account2 { get; set; } public List? Debs { get; set; } @@ -96,6 +97,7 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData public string LastName { get; set; } public string? MiddleName { get; set; } public int? Age { get; set; } + public decimal? Weight { get; set; } public AccountEntity? Account { get; set; } public AccountEntity? Account2 { get; set; } public List? Debs { get; set; } diff --git a/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/036_PureMappingMethodWithNullable_FIXED.txt b/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/036_PureMappingMethodWithNullable_FIXED.txt index e901959..6260f3d 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/036_PureMappingMethodWithNullable_FIXED.txt +++ b/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/036_PureMappingMethodWithNullable_FIXED.txt @@ -19,6 +19,7 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData MiddleName = entity.MiddleName, LastName = entity.LastName, AgeValue = entity.Age ?? throw new ArgumentNullException(nameof(entity), "The value of 'entity.Age' should not be null"), + Weight = (double)(entity.Weight ?? throw new ArgumentNullException(nameof(entity), "The value of 'entity.Weight' should not be null")), Account = entity.Account != null ? new AccountDTO { BankName = entity.Account.BankName, @@ -74,6 +75,7 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData public string? MiddleName { get; set; } public string LastName { get; set; } public int AgeValue { get; set; } + public double Weight { get; set; } public AccountDTO? Account { get; set; } public AccountDTO Account2 { get; set; } public List? Debs { get; set; } @@ -142,6 +144,7 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData public string LastName { get; set; } public string? MiddleName { get; set; } public int? Age { get; set; } + public decimal? Weight { get; set; } public AccountEntity? Account { get; set; } public AccountEntity? Account2 { get; set; } public List? Debs { get; set; } diff --git a/MappingGenerator/MappingGenerator/MappingGenerator/Mappings/MappingEngine.cs b/MappingGenerator/MappingGenerator/MappingGenerator/Mappings/MappingEngine.cs index 485e8fb..34a637b 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator/Mappings/MappingEngine.cs +++ b/MappingGenerator/MappingGenerator/MappingGenerator/Mappings/MappingEngine.cs @@ -84,13 +84,15 @@ public async Task MapExpression(MappingElement source, Annotated } - if (ObjectHelper.IsSimpleType(targetType.Type) && SymbolHelper.IsNullable(sourceType.Type, out var underlyingType) ) + if (ObjectHelper.IsSimpleType(targetType.Type) && SymbolHelper.IsNullable(sourceType.Type, out var underlyingType)) { - return new MappingElement + var mapping = new MappingElement { - Expression = OrFailWhenArgumentNull(source.Expression), + Expression = OrFailWhenArgumentNull(source.Expression), ExpressionType = new AnnotatedType(underlyingType, false) }; + + return IsConversionToSimpleTypeNeeded(targetType.Type, underlyingType) ? ConvertToSimpleType(targetType, mapping, mappingContext) : mapping; } if (IsConversionToSimpleTypeNeeded(targetType.Type, source.ExpressionType.Type))