Skip to content

Commit

Permalink
Merge pull request #383 from zzzprojects/stef_call_methods_which_retu…
Browse files Browse the repository at this point in the history
…rn_nullable

Fixed : calling methods which return a nullable
  • Loading branch information
JonathanMagnan authored May 31, 2020
2 parents c8841b0 + 803ee4d commit 31945cf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/System.Linq.Dynamic.Core/Parser/PredefinedTypesHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ public static bool IsPredefinedType(ParsingConfig config, Type type)
Check.NotNull(config, nameof(config));
Check.NotNull(type, nameof(type));

if (PredefinedTypes.ContainsKey(type))
var nonNullableType = TypeHelper.GetNonNullableType(type);
if (PredefinedTypes.ContainsKey(nonNullableType))
{
return true;
}

return config.CustomTypeProvider != null && config.CustomTypeProvider.GetCustomTypes().Contains(type);
return config.CustomTypeProvider != null &&
(config.CustomTypeProvider.GetCustomTypes().Contains(type) || config.CustomTypeProvider.GetCustomTypes().Contains(nonNullableType));
}
}
}
64 changes: 64 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/QueryableTests.Methods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Xunit;

namespace System.Linq.Dynamic.Core.Tests
{
public partial class QueryableTests
{
[Fact]
public void CallMethod()
{
// Arrange
var query = new[] { new Test() }.AsQueryable();

// Act
var result = query.Select<decimal>("t => t.GetDecimal()").First();

// Assert
Assert.Equal(42, result);
}

[Fact]
public void CallMethodWhichReturnsNullable()
{
// Arrange
var query = new[] { new Test() }.AsQueryable();

// Act
var result = query.Select<decimal?>("t => t.GetNullableDecimal()").First();

// Assert
Assert.Equal(null, result);
}

[Fact]
public void CallMethodWhichReturnsNullable_WithValue()
{
// Arrange
var query = new[] { new Test() }.AsQueryable();

// Act
var result = query.Select<decimal?>("t => t.GetNullableDecimalWithValue()").First();

// Assert
Assert.Equal(100, result);
}
}

class Test
{
public decimal GetDecimal()
{
return 42;
}

public decimal? GetNullableDecimal()
{
return null;
}

public decimal? GetNullableDecimalWithValue()
{
return 100;
}
}
}

0 comments on commit 31945cf

Please sign in to comment.