Uploaded image for project: 're-motion'
  1. RM-4771

Relinq breaks on parsing null values

    Details

    • Type: Bug
    • Status: Closed (View workflow)
    • Priority: Normal
    • Resolution: Fixed
    • Affects versions: 1.13.135
    • Fix versions: 1.13.170
    • Components: Data.Linq
    • Labels:
      None
    • Sprint:

      Description

      PartialEvaluatingExpressionTreeVisitor breaks while compiling lambda expression which has nested member access and upper member is null.

      for example : Property.NestedProperty.Value == something

      if Property == null (or NestedProperty == null) EvaluateSubtree will explode.

      Here is a fix which works for me:

      protected Expression EvaluateSubtree(Expression subtree)
      {
      	ArgumentUtility.CheckNotNull("subtree", subtree);
      
      	if (subtree.NodeType == ExpressionType.Constant)
      	{
      		var constantExpression = (ConstantExpression)subtree;
      		var valueAsIQueryable = constantExpression.Value as IQueryable;
      		if (valueAsIQueryable != null && valueAsIQueryable.Expression != constantExpression)
      			return valueAsIQueryable.Expression;
      
      		return constantExpression;
      	}
      	else if (subtree.NodeType == ExpressionType.MemberAccess && subtree is MemberExpression)
      	{
      		var me = (MemberExpression)subtree;
      		if (me.Expression is MemberExpression)
      		{
      			var smb = (MemberExpression)me.Expression;
      
      			Expression<Func<object>> outerLambda = Expression.Lambda<Func<object>>(Expression.Convert(smb, typeof(object)));
      
      			object nullValue = outerLambda.Compile()();
      			if (nullValue == null)
      				return Expression.Constant(null, subtree.Type);
      
      		}
      	}
      	Expression<Func<object>> lambdaWithoutParameters = Expression.Lambda<Func<object>>(Expression.Convert(subtree, typeof(object)));
      
      	object value = lambdaWithoutParameters.Compile()();
      	return Expression.Constant(value, subtree.Type);
      }
      

      I've added a check for nested member evaluation and returned constant null if member is null.
      While this is not entirely correct, since this part of lambda expression has purged evaluation shortcutting and this is consistent with sql behavior, I think this is accepted behavior.

      You can improve this patch by recursively evaluating member expressions to see if some other property is not null.
      For example if you have
      Property.FirstNestedProperty.SecondNestedProperty.ThirdNestedProperty.Value

      Regards,
      Rikard

        Attachments

          Issue links

            Activity

              People

              • Assignee:
                Fabian Schmied
                Reporter:
                User (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: