Skip to content

Commit 8fc9d1b

Browse files
committed
Actually use attribute-based suppression
1 parent 0878c62 commit 8fc9d1b

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/Http/Routing/src/ValidationEndpointFilterFactory.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.ComponentModel.DataAnnotations;
5-
using System.Linq;
5+
using System.Diagnostics.CodeAnalysis;
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Options;
88

99
namespace Microsoft.AspNetCore.Http.Validation;
1010

1111
internal static class ValidationEndpointFilterFactory
1212
{
13+
private const string ValidationContextJustification = "The DisplayName property is always statically initialized in the ValidationContext through this codepath.";
14+
15+
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = ValidationContextJustification)]
1316
public static EndpointFilterDelegate Create(EndpointFilterFactoryContext context, EndpointFilterDelegate next)
1417
{
1518
var parameters = context.MethodInfo.GetParameters();
@@ -18,33 +21,44 @@ public static EndpointFilterDelegate Create(EndpointFilterFactoryContext context
1821
{
1922
return next;
2023
}
21-
var validatableParameters = parameters
22-
.Select(p => options.TryGetValidatableParameterInfo(p, out var validatableParameter) ? validatableParameter : null);
23-
if (validatableParameters.All(p => p is null))
24+
25+
var parameterCount = parameters.Length;
26+
var validatableParameters = new ValidatableParameterInfo[parameterCount];
27+
var hasValidatableParameters = false;
28+
29+
for (var i = 0; i < parameterCount; i++)
30+
{
31+
if (options.TryGetValidatableParameterInfo(parameters[i], out var validatableParameter))
32+
{
33+
validatableParameters[i] = validatableParameter;
34+
hasValidatableParameters = true;
35+
}
36+
}
37+
38+
if (!hasValidatableParameters)
2439
{
2540
return next;
2641
}
42+
2743
var validatableContext = new ValidatableContext { ValidationOptions = options };
2844
return async (context) =>
2945
{
3046
validatableContext.ValidationErrors?.Clear();
3147

3248
for (var i = 0; i < context.Arguments.Count; i++)
3349
{
34-
var validatableParameter = validatableParameters.ElementAt(i);
50+
var validatableParameter = validatableParameters[i];
3551

3652
var argument = context.Arguments[i];
3753
if (argument is null || validatableParameter is null)
3854
{
3955
continue;
4056
}
41-
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
4257
// ValidationContext is not trim-friendly in codepaths that don't
4358
// initialize an explicit DisplayName. We can suppress the warning here.
4459
// Eventually, this can be removed when the code is updated to
4560
// use https://github.com/dotnet/runtime/issues/113134.
4661
var validationContext = new ValidationContext(argument, context.HttpContext.RequestServices, items: null) { DisplayName = validatableParameter.DisplayName };
47-
#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
4862
validatableContext.ValidationContext = validationContext;
4963
await validatableParameter.Validate(argument, validatableContext);
5064
}

0 commit comments

Comments
 (0)