3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using System . Text . RegularExpressions ;
7
6
using Microsoft . OpenApi . Models ;
8
7
using Microsoft . OpenApi . Properties ;
9
8
@@ -36,7 +35,6 @@ public static class OpenApiPathsRules
36
35
}
37
36
} ) ;
38
37
39
- private static readonly Regex regexPath = new Regex ( "\\ {([^/}]+)\\ }" , RegexOptions . Compiled , TimeSpan . FromMilliseconds ( 100 ) ) ;
40
38
/// <summary>
41
39
/// A relative path to an individual endpoint. The field name MUST begin with a slash.
42
40
/// </summary>
@@ -50,7 +48,7 @@ public static class OpenApiPathsRules
50
48
{
51
49
context . Enter ( path ) ;
52
50
53
- var pathSignature = regexPath . Replace ( path , "{}" ) ;
51
+ var pathSignature = GetPathSignature ( path ) ;
54
52
55
53
if ( ! hashSet . Add ( pathSignature ) )
56
54
context . CreateError ( nameof ( PathMustBeUnique ) ,
@@ -60,6 +58,28 @@ public static class OpenApiPathsRules
60
58
}
61
59
} ) ;
62
60
61
+ /// <summary>
62
+ /// Replaces placeholders in the path with {}, e.g. /pets/{petId} becomes /pets/{} .
63
+ /// </summary>
64
+ /// <param name="path">The input path</param>
65
+ /// <returns>The path signature</returns>
66
+ private static string GetPathSignature ( string path )
67
+ {
68
+ for ( int openBrace = path . IndexOf ( '{' ) ; openBrace > - 1 ; openBrace = path . IndexOf ( '{' , openBrace + 2 ) )
69
+ {
70
+ int closeBrace = path . IndexOf ( '}' , openBrace ) ;
71
+
72
+ if ( closeBrace < 0 )
73
+ {
74
+ return path ;
75
+ }
76
+
77
+ path = path . Substring ( 0 , openBrace + 1 ) + path . Substring ( closeBrace ) ;
78
+ }
79
+
80
+ return path ;
81
+ }
82
+
63
83
// add more rules
64
84
}
65
85
}
0 commit comments