Skip to content

Commit 0a241bd

Browse files
committed
fix: updates additional regex to use generated versions
1 parent ebaff49 commit 0a241bd

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎src/Microsoft.OpenApi/Expressions/CompositeExpression.cs‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Text.RegularExpressions;
@@ -10,10 +11,17 @@ namespace Microsoft.OpenApi
1011
/// <summary>
1112
/// String literal with embedded expressions
1213
/// </summary>
13-
public class CompositeExpression : RuntimeExpression
14+
public partial class CompositeExpression : RuntimeExpression
1415
{
1516
private readonly string template;
16-
private readonly Regex expressionPattern = new(@"{(?<exp>\$[^}]*)");
17+
private const string ExpressionPattern = @"{(?<exp>\$[^}]*)";
18+
19+
#if NET8_0_OR_GREATER
20+
[GeneratedRegex(ExpressionPattern, RegexOptions.None, matchTimeoutMilliseconds: 100)]
21+
private static partial Regex ExpressionRegex();
22+
#else
23+
private static readonly Regex ExpressionRegex = new(ExpressionPattern, RegexOptions.None, TimeSpan.FromMilliseconds(100));
24+
#endif
1725

1826
/// <summary>
1927
/// Expressions embedded into string literal
@@ -24,12 +32,17 @@ public class CompositeExpression : RuntimeExpression
2432
/// Create a composite expression from a string literal with an embedded expression
2533
/// </summary>
2634
/// <param name="expression"></param>
35+
/// <exception cref="RegexMatchTimeoutException">Extracting embedded expressions exceeds the regex match timeout.</exception>
2736
public CompositeExpression(string expression)
2837
{
2938
template = expression;
3039

3140
// Extract subexpressions and convert to RuntimeExpressions
32-
var matches = expressionPattern.Matches(expression);
41+
#if NET8_0_OR_GREATER
42+
var matches = ExpressionRegex().Matches(expression);
43+
#else
44+
var matches = ExpressionRegex.Matches(expression);
45+
#endif
3346

3447
foreach (var item in matches.Cast<Match>())
3548
{

‎test/Microsoft.OpenApi.Tests/Expressions/RuntimeExpressionTests.cs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,28 @@ public void BuildRuntimeExpressionTwiceCreatesNewEquivalentInstances(string expr
145145
Assert.Equal(runtimeExpression1, runtimeExpression2);
146146
}
147147

148+
[Fact]
149+
public void CompositeRuntimeExpressionPreservesMultilineCaptures()
150+
{
151+
const string expression = "prefix {$request.header.foo\nbar} {$url} suffix";
152+
153+
var composite = Assert.IsType<CompositeExpression>(RuntimeExpression.Build(expression));
154+
155+
Assert.Equal(expression, composite.Expression);
156+
Assert.Equal(new[] { "$request.header.foo\nbar", "$url" },
157+
composite.ContainedExpressions.Select(static item => item.Expression));
158+
}
159+
160+
[Fact]
161+
public void CompositeRuntimeExpressionPreservesUnterminatedCapture()
162+
{
163+
const string expression = "prefix {$url";
164+
165+
var composite = Assert.IsType<CompositeExpression>(RuntimeExpression.Build(expression));
166+
167+
Assert.IsType<UrlExpression>(Assert.Single(composite.ContainedExpressions));
168+
}
169+
148170
[Fact]
149171
public void CompositeRuntimeExpressionContainsExpression()
150172
{

0 commit comments

Comments
 (0)