Skip to content

Commit 03a3c06

Browse files
committed
Fix up suppression for trimming warnings
1 parent 8fc9d1b commit 03a3c06

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Http/Routing/src/ValidationEndpointFilterFactory.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ internal static class ValidationEndpointFilterFactory
1212
{
1313
private const string ValidationContextJustification = "The DisplayName property is always statically initialized in the ValidationContext through this codepath.";
1414

15-
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = ValidationContextJustification)]
1615
public static EndpointFilterDelegate Create(EndpointFilterFactoryContext context, EndpointFilterDelegate next)
1716
{
1817
var parameters = context.MethodInfo.GetParameters();
@@ -58,7 +57,7 @@ public static EndpointFilterDelegate Create(EndpointFilterFactoryContext context
5857
// initialize an explicit DisplayName. We can suppress the warning here.
5958
// Eventually, this can be removed when the code is updated to
6059
// use https://github.com/dotnet/runtime/issues/113134.
61-
var validationContext = new ValidationContext(argument, context.HttpContext.RequestServices, items: null) { DisplayName = validatableParameter.DisplayName };
60+
var validationContext = CreateValidationContext(argument, validatableParameter, context.HttpContext.RequestServices);
6261
validatableContext.ValidationContext = validationContext;
6362
await validatableParameter.Validate(argument, validatableContext);
6463
}
@@ -73,4 +72,14 @@ public static EndpointFilterDelegate Create(EndpointFilterFactoryContext context
7372
return next;
7473
};
7574
}
75+
76+
/// <remarks>
77+
/// ValidationContext is not trim-friendly in codepaths that don't
78+
/// initialize an explicit DisplayName. We can suppress the warning here.
79+
/// Eventually, this can be removed when the code is updated to
80+
/// use https://github.com/dotnet/runtime/issues/113134.
81+
/// </remarks>
82+
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = ValidationContextJustification)]
83+
private static ValidationContext CreateValidationContext(object argument, ValidatableParameterInfo validatableParameter, IServiceProvider serviceProvider)
84+
=> new(argument, serviceProvider, items: null) { DisplayName = validatableParameter.DisplayName };
7685
}

0 commit comments

Comments
 (0)