Skip to content

Commit

Permalink
Issue #2255,Create the correct expression when casting entity/complex…
Browse files Browse the repository at this point in the history
… type to derived type. PR #2299
  • Loading branch information
KenitoInc authored and xuzhg committed Oct 21, 2020
1 parent 01f875c commit 8001662
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,19 @@ private Expression BindSingleResourceCastFunctionCall(SingleResourceFunctionCall
else if (arguments[0].Type.IsAssignableFrom(targetClrType))
{
// To support to cast Entity/Complex type to the sub type now.
Expression source = BindCastSourceNode(node.Source);

Expression source;
if(node.Source != null)
{
source = BindCastSourceNode(node.Source);
}
else
{
// if the cast is on the root i.e $it (~/Products?$filter=NS.PopularProducts/.....),
// node.Source would be null. Calling BindCastSourceNode will always return '$it'.
// In scenarios where we are casting a navigation property to return an expression that queries against the parent property,
// we need to have a memberAccess expression e.g '$it.Category'. We can get this from arguments[0].
source = arguments[0];
}
return Expression.TypeAs(source, targetClrType);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2316,7 +2316,7 @@ public void CastToUnquotedEntityType_ThrowsODataException(string filter, string

[Theory]
[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)")]
[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)")]
[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)")]
public void CastToQuotedEntityOrComplexType_DerivedProductName(string filter, string expectedExpression, string expectedExpressionWithNullCheck)
{
// Arrange, Act & Assert
Expand Down

0 comments on commit 8001662

Please sign in to comment.