System.Linq.Dynamic.Core icon indicating copy to clipboard operation
System.Linq.Dynamic.Core copied to clipboard

Static helper class with name as property doesn't work after updating from v.1.0.8.18 to 1.3.8

Open OlegNadymov opened this issue 1 year ago • 5 comments

In continuation of https://github.com/zzzprojects/System.Linq.Dynamic.Core/issues/759

1. Description

Simplified demo:

public static class UserInfo
{
    public static Guid Key => Guid.NewGuid();
}

public class User
{
    public Guid Id { get; set; }

    public User UserInfo { get; set; }
}

I try to parse the expression this way:

var config = new ParsingConfig
{
     CustomTypeProvider = new TestCustomTypeProvider()
};

var exp = "Id == UserInfo.Key";
var lambda = System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda(config, typeof(User), null, exp);

But I got exception:

  System.Linq.Dynamic.Core.Exceptions.ParseException : No property or field 'Key' exists in type 'User'

If I put the full type name:

var exp = "Id == System.Linq.Dynamic.Core.Tests.UserInfo.Key";

I have another exception:

System.Linq.Dynamic.Core.Exceptions.ParseException : Enum value 'Key' is not defined in enum type 'System.Linq.Dynamic.Core.Tests.UserInfo'

2. Exception

The first stack trace:

  System.Linq.Dynamic.Core.Exceptions.ParseException : No property or field 'Key' exists in type 'User'

Stack Trace: 
  ExpressionParser.ParseMemberAccess(Type type, Expression expression) line 1891
  ExpressionParser.ParsePrimary() line 819
  ExpressionParser.ParseUnary() line 806
  ExpressionParser.ParseArithmetic() line 751
  ExpressionParser.ParseAdditive() line 718
  ExpressionParser.ParseShiftOperator() line 694
  ExpressionParser.ParseComparisonOperator() line 492
  ExpressionParser.ParseLogicalAndOrOperator() line 414
  ExpressionParser.ParseIn() line 325
  ExpressionParser.ParseAndOperator() line 308
  ExpressionParser.ParseOrOperator() line 290
  ExpressionParser.ParseLambdaOperator() line 270
  ExpressionParser.ParseNullCoalescingOperator() line 257
  ExpressionParser.ParseConditionalOperator() line 241
  ExpressionParser.Parse(Type resultType, Boolean createParameterCtor) line 156
  DynamicExpressionParser.ParseLambda(Type delegateType, ParsingConfig parsingConfig, Boolean createParameterCtor, ParameterExpression[] parameters, Type resultType, String expression, Object[] values) line 121
  DynamicExpressionParser.ParseLambda(ParsingConfig parsingConfig, Boolean createParameterCtor, ParameterExpression[] parameters, Type resultType, String expression, Object[] values) line 98
  DynamicExpressionParser.ParseLambda(ParsingConfig parsingConfig, Boolean createParameterCtor, Type itType, Type resultType, String expression, Object[] values) line 357
  DynamicExpressionParser.ParseLambda(ParsingConfig parsingConfig, Type itType, Type resultType, String expression, Object[] values) line 319
  DynamicExpressionParserTests.DynamicExpressionParser_ParseLambda_Operator_Less_Greater_With_Guids() line 1465

3. Fiddle or Project

Again, I have done changes in DynamicExpressionParserTests only locally.

4. Any further technical details

The first version of expression (non-full type name) was working v.1.0.8.18. But it is not working on the release version 1.3.8.

Is it possible to fix it?

OlegNadymov avatar Feb 06 '24 16:02 OlegNadymov

@OlegNadymov I assume that you did add that UserInfo to the types in that TestCustomTypeProvider ?

StefH avatar Feb 07 '24 13:02 StefH

Yes, I did: image

OlegNadymov avatar Feb 07 '24 19:02 OlegNadymov

https://github.com/zzzprojects/System.Linq.Dynamic.Core/pull/774

StefH avatar Feb 07 '24 20:02 StefH

The problem in my case is the class User has property named UserInfo exactly as static class UserInfo (like in my demo above).

OlegNadymov avatar Feb 08 '24 06:02 OlegNadymov

https://github.com/zzzprojects/System.Linq.Dynamic.Core/issues/773

StefH avatar Feb 08 '24 14:02 StefH

PR merged, a new NuGet will be released in some time

StefH avatar Feb 29 '24 08:02 StefH