Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eng/TrimmableProjects.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<TrimmableProject Include="Microsoft.AspNetCore.Http" />
<TrimmableProject Include="Microsoft.AspNetCore.Metadata" />
<TrimmableProject Include="Microsoft.AspNetCore.Routing.Abstractions" />
<TrimmableProject Include="Microsoft.AspNetCore.Routing" />
<TrimmableProject Include="Microsoft.AspNetCore.WebUtilities" />
<TrimmableProject Include="Microsoft.AspNetCore.Connections.Abstractions" />
<TrimmableProject Include="Microsoft.AspNetCore.Server.HttpSys" />
Expand Down
1 change: 0 additions & 1 deletion src/Http/Http/src/Microsoft.AspNetCore.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore</PackageTags>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

Expand Down
23 changes: 21 additions & 2 deletions src/Http/Routing/src/Builder/EndpointRouteBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
Expand All @@ -18,6 +19,8 @@ namespace Microsoft.AspNetCore.Builder;
/// </summary>
public static class EndpointRouteBuilderExtensions
{
internal const string MapEndpointTrimmerWarning = "This API may perform reflection on the supplied delegate and its parameters. These types may be trimmed if not directly referenced.";

// Avoid creating a new array every call
private static readonly string[] GetVerb = new[] { HttpMethods.Get };
private static readonly string[] PostVerb = new[] { HttpMethods.Post };
Expand All @@ -33,6 +36,7 @@ public static class EndpointRouteBuilderExtensions
/// <param name="pattern">The route pattern.</param>
/// <param name="requestDelegate">The delegate executed when the endpoint is matched.</param>
/// <returns>A <see cref="IEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
[RequiresUnreferencedCode(EndpointRouteBuilderExtensions.MapEndpointTrimmerWarning)]
public static IEndpointConventionBuilder MapGet(
this IEndpointRouteBuilder endpoints,
string pattern,
Expand Down Expand Up @@ -220,6 +224,7 @@ public static IEndpointConventionBuilder Map(
/// <param name="pattern">The route pattern.</param>
/// <param name="handler">The delegate executed when the endpoint is matched.</param>
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
public static RouteHandlerBuilder MapGet(
this IEndpointRouteBuilder endpoints,
string pattern,
Expand All @@ -236,6 +241,7 @@ public static RouteHandlerBuilder MapGet(
/// <param name="pattern">The route pattern.</param>
/// <param name="handler">The delegate executed when the endpoint is matched.</param>
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
public static RouteHandlerBuilder MapPost(
this IEndpointRouteBuilder endpoints,
string pattern,
Expand All @@ -252,6 +258,7 @@ public static RouteHandlerBuilder MapPost(
/// <param name="pattern">The route pattern.</param>
/// <param name="handler">The delegate executed when the endpoint is matched.</param>
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
public static RouteHandlerBuilder MapPut(
this IEndpointRouteBuilder endpoints,
string pattern,
Expand All @@ -268,6 +275,7 @@ public static RouteHandlerBuilder MapPut(
/// <param name="pattern">The route pattern.</param>
/// <param name="handler">The delegate executed when the endpoint is matched.</param>
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
public static RouteHandlerBuilder MapDelete(
this IEndpointRouteBuilder endpoints,
string pattern,
Expand All @@ -284,6 +292,7 @@ public static RouteHandlerBuilder MapDelete(
/// <param name="pattern">The route pattern.</param>
/// <param name="handler">The <see cref="Delegate" /> executed when the endpoint is matched.</param>
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
public static RouteHandlerBuilder MapPatch(
this IEndpointRouteBuilder endpoints,
string pattern,
Expand All @@ -301,6 +310,7 @@ public static RouteHandlerBuilder MapPatch(
/// <param name="handler">The delegate executed when the endpoint is matched.</param>
/// <param name="httpMethods">HTTP methods that the endpoint will match.</param>
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
public static RouteHandlerBuilder MapMethods(
this IEndpointRouteBuilder endpoints,
string pattern,
Expand Down Expand Up @@ -348,6 +358,7 @@ static bool ShouldDisableInferredBody(string method)
/// <param name="pattern">The route pattern.</param>
/// <param name="handler">The delegate executed when the endpoint is matched.</param>
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
public static RouteHandlerBuilder Map(
this IEndpointRouteBuilder endpoints,
string pattern,
Expand All @@ -364,6 +375,7 @@ public static RouteHandlerBuilder Map(
/// <param name="pattern">The route pattern.</param>
/// <param name="handler">The delegate executed when the endpoint is matched.</param>
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
public static RouteHandlerBuilder Map(
this IEndpointRouteBuilder endpoints,
RoutePattern pattern,
Expand Down Expand Up @@ -391,6 +403,7 @@ public static RouteHandlerBuilder Map(
/// <c>{*path:nonfile}</c>. The order of the registered endpoint will be <c>int.MaxValue</c>.
/// </para>
/// </remarks>
[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
public static RouteHandlerBuilder MapFallback(this IEndpointRouteBuilder endpoints, Delegate handler)
{
if (endpoints == null)
Expand Down Expand Up @@ -427,6 +440,7 @@ public static RouteHandlerBuilder MapFallback(this IEndpointRouteBuilder endpoin
/// to exclude requests for static files.
/// </para>
/// </remarks>
[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
public static RouteHandlerBuilder MapFallback(
this IEndpointRouteBuilder endpoints,
string pattern,
Expand All @@ -453,6 +467,7 @@ public static RouteHandlerBuilder MapFallback(
return conventionBuilder;
}

[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
private static RouteHandlerBuilder Map(
this IEndpointRouteBuilder endpoints,
RoutePattern pattern,
Expand Down Expand Up @@ -515,7 +530,11 @@ private static RouteHandlerBuilder Map(
}

var routeHandlerBuilder = new RouteHandlerBuilder(dataSource.AddEndpointBuilder(builder));
routeHandlerBuilder.Add(endpointBuilder =>
routeHandlerBuilder.Add(RouteHandlerBuilderConvention);

[UnconditionalSuppressMessage("Trimmer", "IL2026", Justification = "We surface a RequireUnreferencedCode in the call to enclosing Map method. " +
"The trimmer is unable to infer this on the nested lambda.")]
void RouteHandlerBuilderConvention(EndpointBuilder endpointBuilder)
{
var options = new RequestDelegateFactoryOptions
{
Expand Down Expand Up @@ -545,7 +564,7 @@ private static RouteHandlerBuilder Map(
}
}
endpointBuilder.RequestDelegate = filteredRequestDelegateResult.RequestDelegate;
});
}

return routeHandlerBuilder;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Routing/src/DefaultInlineConstraintResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public DefaultInlineConstraintResolver(IOptions<RouteOptions> routeOptions, ISer
throw new ArgumentNullException(nameof(serviceProvider));
}

_inlineConstraintMap = routeOptions.Value.ConstraintMap;
_inlineConstraintMap = routeOptions.Value.TrimmerSafeConstraintMap;
_serviceProvider = serviceProvider;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Http/Routing/src/DefaultParameterPolicyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ public override IParameterPolicy Create(RoutePatternParameterPart? parameter, st
}

var parameterPolicy = ParameterPolicyActivator.ResolveParameterPolicy<IParameterPolicy>(
_options.ConstraintMap,
_options.TrimmerSafeConstraintMap,
_serviceProvider,
inlineText,
out var parameterPolicyKey);

if (parameterPolicy == null)
{
throw new InvalidOperationException(Resources.FormatRoutePattern_ConstraintReferenceNotFound(
parameterPolicyKey,
typeof(RouteOptions),
nameof(RouteOptions.ConstraintMap)));
parameterPolicyKey,
typeof(RouteOptions),
nameof(RouteOptions.ConstraintMap)));
}

if (parameterPolicy is IRouteConstraint constraint)
Expand Down
12 changes: 10 additions & 2 deletions src/Http/Routing/src/EndpointRoutingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing.Matching;
Expand Down Expand Up @@ -96,14 +97,21 @@ private Task SetRoutingAndContinue(HttpContext httpContext)
// Raise an event if the route matched
if (_diagnosticListener.IsEnabled() && _diagnosticListener.IsEnabled(DiagnosticsEndpointMatchedKey))
{
// We're just going to send the HttpContext since it has all of the relevant information
_diagnosticListener.Write(DiagnosticsEndpointMatchedKey, httpContext);
Write(_diagnosticListener, httpContext);
}

Log.MatchSuccess(_logger, endpoint);
}

return _next(httpContext);

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Justification = "The values being passed into Write are being consumed by the application already.")]
static void Write(DiagnosticListener diagnosticListener, HttpContext httpContext)
{
// We're just going to send the HttpContext since it has all of the relevant information
diagnosticListener.Write(DiagnosticsEndpointMatchedKey, httpContext);
}
}

// Initialization is async to avoid blocking threads while reflection and things
Expand Down
Loading