microsoft / RulesEngine

A Json based Rules Engine with extensive Dynamic expression support
https://microsoft.github.io/RulesEngine/
MIT License
3.47k stars 530 forks source link

v4 to v5 error: array like an enum #500

Closed ciacco85 closed 11 months ago

ciacco85 commented 11 months ago

I've the following classes

public class Transazione
{
    public List<Attore> Attori { get; set; } = new();
}
public class Attore
{
    public Guid Id { get; internal set; }
    public string Nome { get; internal set; }
    public RuoloAttore RuoloAttore { get; internal set; }
}

and expressions

new()
            {
                WorkflowName = nameof(TipoTransazione.Conferimento),
                Rules = new Rule[] {
                    new() {
                        RuleName = "Attore Da",
                        Enabled = true,
                        ErrorMessage = "Attore Da Id must be defined",
                        SuccessEvent = "10",
                        RuleExpressionType = RuleExpressionType.LambdaExpression,
                        Expression = "transazione.Attori.Any(a => a.RuoloAttore == 1)",
                    },
                    new() {
                        RuleName = "Attore A",
                        Enabled = true,
                        ErrorMessage = "Attore A must be defined",
                        SuccessEvent = "10",
                        RuleExpressionType = RuleExpressionType.LambdaExpression,
                        Expression = "transazione.Attori.Any(a => a.RuoloAttore == 2)",
                    },
                }
            }

But this exception is thrown when executing rule engine

Exception while parsing expression `transazione.Attori.Any(a => a.RuoloAttore == 1)` - Enum type 'Attori' not found

Initially I though it was due to this #209, even if it was working on v4, but then I've realized that 'Attori' was the property name of type Attore and not an enum

abbasc52 commented 11 months ago

Looks like an issue related dependent lib https://dynamic-linq.net/advanced-configuration#prioritizepropertyorfieldoverthetype preference changes. I will try updating the config so that it is compatible to previous way.

This issue is happening as you input name is same as type name and the expression are case insensitive. Your rule works if the input is named differently. Check this fiddle - https://dotnetfiddle.net/yFFz3h

ciacco85 commented 11 months ago

works like a charm

abbasc52 commented 11 months ago

Added an option to disable this behavior in ReSettings:

https://github.com/microsoft/RulesEngine/blob/103e8174310d0eb24976c153b03990ec9631bfa2/src/RulesEngine/Models/ReSettings.cs#L71-L76

You can turn it off in case you want to maintain existing naming