Skip to content

Commit 0bbaed7

Browse files
committed
Fix SYSLIB1045: Use 'GeneratedRegexAttribute' to generate the regular expression implementation at compile time
1 parent 5530832 commit 0bbaed7

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SwaggerComponents/EndpointOrderingFilter.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66
namespace JsonApiDotNetCore.OpenApi.Swashbuckle.SwaggerComponents;
77

88
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
9+
#if NET6_0
910
internal sealed class EndpointOrderingFilter : IDocumentFilter
11+
#else
12+
internal sealed partial class EndpointOrderingFilter : IDocumentFilter
13+
#endif
1014
{
11-
private static readonly Regex RelationshipNameInUrlPattern =
12-
new($@".*{JsonApiRoutingTemplate.PrimaryEndpoint}/(?>{JsonApiRoutingTemplate.RelationshipsPart}\/)?(\w+)", RegexOptions.Compiled);
15+
private const string PatternText = $@".*{JsonApiRoutingTemplate.PrimaryEndpoint}/(?>{JsonApiRoutingTemplate.RelationshipsPart}\/)?(?<RelationshipName>\w+)";
16+
17+
#if NET6_0
18+
private const RegexOptions RegexOptionsNet60 = RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture;
19+
private static readonly Regex RelationshipNameInUrlPattern = new(PatternText, RegexOptionsNet60);
20+
#endif
1321

1422
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
1523
{
@@ -39,8 +47,18 @@ private static bool IsSecondaryEndpoint(KeyValuePair<string, OpenApiPathItem> en
3947

4048
private static string GetRelationshipName(KeyValuePair<string, OpenApiPathItem> entry)
4149
{
42-
Match match = RelationshipNameInUrlPattern.Match(entry.Key);
50+
Match match = RelationshipNameInUrlRegex().Match(entry.Key);
51+
52+
return match.Success ? match.Groups["RelationshipName"].Value : string.Empty;
53+
}
4354

44-
return match.Success ? match.Groups[1].Value : string.Empty;
55+
#if NET6_0
56+
private static Regex RelationshipNameInUrlRegex()
57+
{
58+
return RelationshipNameInUrlPattern;
4559
}
60+
#else
61+
[GeneratedRegex(PatternText, RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture)]
62+
private static partial Regex RelationshipNameInUrlRegex();
63+
#endif
4664
}

0 commit comments

Comments
 (0)