Skip to content

Commit 0878c62

Browse files
committed
Fix up suppression for ValidationContext trimming
1 parent 0195662 commit 0878c62

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/Http/Routing/src/ValidationEndpointFilterFactory.cs

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

44
using System.ComponentModel.DataAnnotations;
5-
using System.Diagnostics.CodeAnalysis;
65
using System.Linq;
76
using Microsoft.Extensions.DependencyInjection;
87
using Microsoft.Extensions.Options;
@@ -11,9 +10,6 @@ namespace Microsoft.AspNetCore.Http.Validation;
1110

1211
internal static class ValidationEndpointFilterFactory
1312
{
14-
private const string ValidationContextJustification = "The DisplayName property is always statically initialized in the ValidationContext through this codepath.";
15-
16-
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = ValidationContextJustification)]
1713
public static EndpointFilterDelegate Create(EndpointFilterFactoryContext context, EndpointFilterDelegate next)
1814
{
1915
var parameters = context.MethodInfo.GetParameters();
@@ -42,7 +38,13 @@ public static EndpointFilterDelegate Create(EndpointFilterFactoryContext context
4238
{
4339
continue;
4440
}
45-
var validationContext = new ValidationContext(argument, context.HttpContext.RequestServices, items: null);
41+
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
42+
// ValidationContext is not trim-friendly in codepaths that don't
43+
// initialize an explicit DisplayName. We can suppress the warning here.
44+
// Eventually, this can be removed when the code is updated to
45+
// use https://github.com/dotnet/runtime/issues/113134.
46+
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
4648
validatableContext.ValidationContext = validationContext;
4749
await validatableParameter.Validate(argument, validatableContext);
4850
}

0 commit comments

Comments
 (0)