Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/RulesEngine/RulesEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ private RuleParameter[] ApplyGlobalParams(string compiledRulesCacheKey, RulePara

private string GetCompiledRulesKey(string workflowName, RuleParameter[] ruleParams)
{
var ruleParamsKey = string.Join("-", ruleParams.Select(c => $"{c.Name}_{c.Type.Name}"));
var ruleParamsKey = string.Join("-", ruleParams.Select(c => $"{c.Name}_{c.Type?.FullName ?? c.Type?.Name}"));
var key = $"{workflowName}-" + ruleParamsKey;
return key;
}
Expand Down
90 changes: 90 additions & 0 deletions test/RulesEngine.UnitTest/CompiledRulesKeyCollisionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using RulesEngine.Models;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Xunit;

namespace RulesEngine.UnitTest
{
[Trait("Category", "Unit")]
[ExcludeFromCodeCoverage]
public class CompiledRulesKeyCollisionTest
{
private class ScopeA
{
public class Item
{
public int Value { get; set; }
}
}

private class ScopeB
{
public class Item
{
public int Value { get; set; }
}
}

[Fact]
public async Task ExecuteAllRulesAsync_DifferentGenericListTypes_DoNotCollideInCompiledRulesCache()
{
var workflow = new Workflow
{
WorkflowName = "GenericListWorkflow",
Rules = new List<Rule>
{
new Rule
{
RuleName = "HasTwoElements",
Expression = "input1.Count == 2"
}
}
};

var engine = new RulesEngine(new[] { workflow });

var intListResult = await engine.ExecuteAllRulesAsync("GenericListWorkflow",
new RuleParameter("input1", new List<int> { 1, 2 }));
Assert.True(intListResult[0].IsSuccess);

// List<string>.Name is also "List`1". Previously this collided with the cached List<int>
// compiled rule and failed with InvalidCastException.
var stringListResult = await engine.ExecuteAllRulesAsync("GenericListWorkflow",
new RuleParameter("input1", new List<string> { "a", "b" }));
Assert.True(stringListResult[0].IsSuccess);
Assert.True(string.IsNullOrEmpty(stringListResult[0].ExceptionMessage));
}

[Fact]
public async Task ExecuteAllRulesAsync_SameShortTypeNameInDifferentScopes_DoNotCollideInCompiledRulesCache()
{
var workflow = new Workflow
{
WorkflowName = "SameShortNameWorkflow",
Rules = new List<Rule>
{
new Rule
{
RuleName = "CheckValue",
Expression = "input1.Value == 42"
}
}
};

var engine = new RulesEngine(new[] { workflow });

var resA = await engine.ExecuteAllRulesAsync("SameShortNameWorkflow",
new RuleParameter("input1", new ScopeA.Item { Value = 42 }));
Assert.True(resA[0].IsSuccess);

var resB = await engine.ExecuteAllRulesAsync("SameShortNameWorkflow",
new RuleParameter("input1", new ScopeB.Item { Value = 42 }));
Assert.True(resB[0].IsSuccess);
Assert.True(string.IsNullOrEmpty(resB[0].ExceptionMessage));
}
}
}
Loading