Skip to content

Commit 10319ab

Browse files
committed
fix: normalizes expressions before comparison
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
1 parent 1967bfd commit 10319ab

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Microsoft.OpenApi/Services/OpenApiWalker.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq.Expressions;
88
using System.Net.Http;
99
using System.Text.Json.Nodes;
10+
using System.Text.RegularExpressions;
1011

1112
namespace Microsoft.OpenApi
1213
{
@@ -48,6 +49,17 @@ public OpenApiWalker(OpenApiVisitorBase visitor):this(visitor, null)
4849
{
4950
// TODO: remove this constructor in next major version and make nodesToVisit params
5051
}
52+
private static readonly Regex _normalizeExpression = new(@"\w+ => \w+\.", RegexOptions.Compiled);
53+
private static string NormalizeExpressionRepresentation(Expression<Func<OpenApiDocument, object>> expression)
54+
{
55+
var stringRepresentation = expression.ToString();
56+
var matchResult = _normalizeExpression.Match(stringRepresentation);
57+
if (!matchResult.Success)
58+
{
59+
throw new InvalidOperationException($"Could not parse expression: {stringRepresentation}, the passed lambda expression is not in the expected format: d => d.Paths");
60+
}
61+
return _normalizeExpression.Replace(stringRepresentation, "d => d.");
62+
}
5163
private HashSet<string>? nodesToVisitPaths;
5264
private bool IsNodeSelected(Expression<Func<OpenApiDocument, object>> candidate)
5365
{
@@ -61,11 +73,11 @@ private bool IsNodeSelected(Expression<Func<OpenApiDocument, object>> candidate)
6173
nodesToVisitPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
6274
foreach (var node in _nodesToVisit)
6375
{
64-
nodesToVisitPaths.Add(node.ToString());
76+
nodesToVisitPaths.Add(NormalizeExpressionRepresentation(node));
6577
}
6678
}
6779

68-
return nodesToVisitPaths.Contains(candidate.ToString());
80+
return nodesToVisitPaths.Contains(NormalizeExpressionRepresentation(candidate));
6981
}
7082

7183
/// <summary>

0 commit comments

Comments
 (0)