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

Fixed : calling methods which return a nullable #383

Merged
merged 1 commit into from
May 31, 2020
Merged
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
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;
}
}
}