Skip to content

Commit

Permalink
Merge pull request #26 from ZEXSM/bugfix/not-converted-to-nul-literal
Browse files Browse the repository at this point in the history
#ZEXSM#bugfix null objects are not converted to null literal, in error
  • Loading branch information
ZEXSM committed Aug 23, 2020
2 parents b5ffdc2 + c071713 commit 50ac53f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
22 changes: 6 additions & 16 deletions src/OData.QueryBuilder/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,12 @@ namespace OData.QueryBuilder.Extensions
{
internal static class ReflectionExtensions
{
public static object GetValue(this MemberInfo memberInfo, object obj = default)
public static object GetValue(this MemberInfo memberInfo, object obj = default) => memberInfo switch
{
try
{
return memberInfo switch
{
FieldInfo fieldInfo => fieldInfo.GetValue(obj),
PropertyInfo propertyInfo => propertyInfo.GetValue(obj, default),
_ => default,
};
}
catch (Exception)
{
return default;
}
}
FieldInfo fieldInfo => fieldInfo.GetValue(obj),
PropertyInfo propertyInfo => propertyInfo.GetValue(obj, default),
_ => default,
};

public static bool IsNullableType(this Type type) =>
Nullable.GetUnderlyingType(type) != default;
Expand All @@ -32,7 +22,7 @@ public static string ConvertToString(object @object)
switch (@object)
{
case null:
return default;
return "null";
case string @string:
return $"'{@string}'";
case bool @bool:
Expand Down
2 changes: 1 addition & 1 deletion src/OData.QueryBuilder/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
internal static class StringExtensions
{
public static bool IsNullOrQuotes(this string value) =>
string.IsNullOrEmpty(value) || value == "''";
string.IsNullOrEmpty(value) || value == "null" || value == "''";
}
}
3 changes: 1 addition & 2 deletions src/OData.QueryBuilder/Visitors/VisitorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ protected virtual string VisitBinaryExpression(BinaryExpression binaryExpression
CreateResourcePath(memberExpression) : ReflectionExtensions.ConvertToString(GetValueOfMemberExpression(memberExpression));

protected virtual string VisitConstantExpression(ConstantExpression constantExpression) =>
constantExpression.Value == default ?
"null" : ReflectionExtensions.ConvertToString(constantExpression.Value);
ReflectionExtensions.ConvertToString(constantExpression.Value);

protected virtual string VisitMethodCallExpression(MethodCallExpression methodCallExpression)
{
Expand Down
14 changes: 14 additions & 0 deletions test/OData.QueryBuilder.Test/ODataQueryOptionListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@ public void ODataQueryBuilderList_Expand_Filter_Select_OrderBy_OrderByDescending
uri.OriginalString.Should().Be($"http://mock/odata/ODataType?$expand=ODataKind&$filter=IdType lt 2 and 3 le ODataKind/ODataCode/IdCode or IdType eq 5 and IdRule ne null and IdRule eq null&$select=ODataKind,Sum&$orderby=IdType asc&$skip=1&$top=1&$count=true");
}

[Fact(DisplayName = "Filter nullable bool eq null => Success")]
public void ODataQueryBuilderList_filter_nullable_bool_eq_null_success()
{
var constValue = default(bool?);

var uri = _odataQueryBuilderDefault
.For<ODataTypeEntity>(s => s.ODataType)
.ByList()
.Filter(s => s.IsOpen == constValue)
.ToUri();

uri.OriginalString.Should().Be("http://mock/odata/ODataType?$filter=IsOpen eq null");
}

[Fact(DisplayName = "Function Date => Success")]
public void ODataQueryBuilderList_Function_Date_Success()
{
Expand Down

0 comments on commit 50ac53f

Please sign in to comment.