Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OfType(string) fails with "'.' or '(' or string literal expected" when AllowNewToEvaluateAnyType is true on version 1.2.24 and above #692

Closed
jbhelm opened this issue Mar 16, 2023 · 4 comments
Assignees

Comments

@jbhelm
Copy link

jbhelm commented Mar 16, 2023

Calling OfType() when AllowNewToEvaluateAnyType is true fails on version 1.2.24 and above; it works on 1.2.23.

Here's a repro:

using System.Linq.Dynamic.Core;

namespace DynamicLinqExample {

    internal class Base { }

    internal class DerivedA : Base { }

    internal class DerivedB : Base { }

    internal class Parent {
        public IEnumerable<Base> Children { get; set; }
    }


    internal class Program {
        private static readonly ParsingConfig DefaultParsingConfig = new() {
            AllowNewToEvaluateAnyType = true,   // <-- works if this is false
        };

        static void Main(string[] args) {
            var data = new Parent[] {
                new() {
                    Children = new Base[] {
                        new DerivedA(),
                        new DerivedB(),
                    }
                }
            };

            var query = data.AsQueryable()
                .Select(DefaultParsingConfig, "Children.OfType(\"DynamicLinqExample.DerivedA\")");
            var result = query.ToDynamicArray();
        }
    }
}

This results in:

System.Linq.Dynamic.Core.Exceptions.ParseException
  HResult=0x80131500
  Message='.' or '(' or string literal expected
  Source=System.Linq.Dynamic.Core
  StackTrace:
   at System.Linq.Dynamic.Core.Tokenizer.TextParser.ValidateToken(TokenId tokenId, String errorMessage)

Thanks!

@StefH
Copy link
Collaborator

StefH commented Mar 16, 2023

@jbhelm
I've tried to reproduce this issue with the latest version:

[Theory]
[InlineData(true)]
[InlineData(false)]
public void OfType_Dynamic_WithFullName_AllowNewToEvaluateAnyType(bool allowNewToEvaluateAnyType)
{
    // Arrange
    var config = new ParsingConfig
    {
        AllowNewToEvaluateAnyType = allowNewToEvaluateAnyType
    };

    var queryable = new Parent[]
    {
        new()
        {
            Children = new Base[]
            {
                new DerivedA(),
                new DerivedB()
            }
        }
    }.AsQueryable();

    var fullType = typeof(DerivedA).FullName;

    // Act
    var query = queryable.Select(config, $"Children.OfType(\"{fullType}\")");
    var result = query.ToDynamicArray();

    // Assert
    result.Should().HaveCount(1);
}

And both options seem to work correct.

@StefH StefH self-assigned this Mar 16, 2023
@StefH StefH added the bug label Mar 16, 2023
@jbhelm
Copy link
Author

jbhelm commented Mar 16, 2023

@StefH Thanks for the quick response! The code I posted fails for me on version 1.3.1, which is the latest version in NuGet. I also checked MyGet and I don't see any version newer than 1.3.1. To which version are you referring as the latest?

@StefH
Copy link
Collaborator

StefH commented Mar 16, 2023

By latest I mean the source-code in master branch. (I added this unit test and it works) (#694)

It could be that this PR also fixes your issue:
#687

@jbhelm
Copy link
Author

jbhelm commented Mar 17, 2023

Ah, my apologies -- I should have also tried the current master branch before reporting.

I can confirm that the current master branch code does work for our use case. I'll wait for the next release. Thanks!

@jbhelm jbhelm closed this as completed Mar 17, 2023
@StefH StefH removed the bug label Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants