Skip to content

Commit 8001662

Browse files
KenitoIncxuzhg
authored andcommitted
Issue #2255,Create the correct expression when casting entity/complex type to derived type. PR #2299
1 parent 01f875c commit 8001662

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

src/Microsoft.AspNet.OData.Shared/Query/Expressions/FilterBinder.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,19 @@ private Expression BindSingleResourceCastFunctionCall(SingleResourceFunctionCall
337337
else if (arguments[0].Type.IsAssignableFrom(targetClrType))
338338
{
339339
// To support to cast Entity/Complex type to the sub type now.
340-
Expression source = BindCastSourceNode(node.Source);
341-
340+
Expression source;
341+
if(node.Source != null)
342+
{
343+
source = BindCastSourceNode(node.Source);
344+
}
345+
else
346+
{
347+
// if the cast is on the root i.e $it (~/Products?$filter=NS.PopularProducts/.....),
348+
// node.Source would be null. Calling BindCastSourceNode will always return '$it'.
349+
// In scenarios where we are casting a navigation property to return an expression that queries against the parent property,
350+
// we need to have a memberAccess expression e.g '$it.Category'. We can get this from arguments[0].
351+
source = arguments[0];
352+
}
342353
return Expression.TypeAs(source, targetClrType);
343354
}
344355
else

test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/FilterBinderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2316,7 +2316,7 @@ public void CastToUnquotedEntityType_ThrowsODataException(string filter, string
23162316

23172317
[Theory]
23182318
[InlineData("cast('Microsoft.AspNet.OData.Test.Query.Expressions.DerivedProduct')/DerivedProductName eq null", "$it => (($it As DerivedProduct).DerivedProductName == null)","$it => (IIF((($it As DerivedProduct) == null), null, ($it As DerivedProduct).DerivedProductName) == null)")]
2319-
[InlineData("cast(Category,'Microsoft.AspNet.OData.Test.Query.Expressions.DerivedCategory')/DerivedCategoryName eq null", "$it => (($it As DerivedCategory).DerivedCategoryName == null)", "$it => (IIF((($it As DerivedCategory) == null), null, ($it As DerivedCategory).DerivedCategoryName) == null)")]
2319+
[InlineData("cast(Category,'Microsoft.AspNet.OData.Test.Query.Expressions.DerivedCategory')/DerivedCategoryName eq null", "$it => (($it.Category As DerivedCategory).DerivedCategoryName == null)", "$it => (IIF((($it.Category As DerivedCategory) == null), null, ($it.Category As DerivedCategory).DerivedCategoryName) == null)")]
23202320
public void CastToQuotedEntityOrComplexType_DerivedProductName(string filter, string expectedExpression, string expectedExpressionWithNullCheck)
23212321
{
23222322
// Arrange, Act & Assert

0 commit comments

Comments
 (0)