Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumerate paramnames on startup #46086

Merged
merged 2 commits into from
Jan 23, 2023
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
2 changes: 1 addition & 1 deletion src/Http/Http.Extensions/src/RequestDelegateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private static RequestDelegateFactoryContext CreateFactoryContext(RequestDelegat
Handler = handler,
ServiceProvider = serviceProvider,
ServiceProviderIsService = serviceProvider.GetService<IServiceProviderIsService>(),
RouteParameters = options?.RouteParameterNames?.ToList(),
RouteParameters = options?.RouteParameterNames,
ThrowOnBadRequest = options?.ThrowOnBadRequest ?? false,
DisableInferredFromBody = options?.DisableInferBodyFromParameters ?? false,
EndpointBuilder = endpointBuilder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class RequestDelegateFactoryContext
// Options
public required IServiceProvider ServiceProvider { get; init; }
public required IServiceProviderIsService? ServiceProviderIsService { get; init; }
public required List<string>? RouteParameters { get; init; }
public required IEnumerable<string>? RouteParameters { get; init; }
public required bool ThrowOnBadRequest { get; init; }
public required bool DisableInferredFromBody { get; init; }
public required EndpointBuilder EndpointBuilder { get; init; }
Expand Down
16 changes: 9 additions & 7 deletions src/Http/Routing/src/RouteEndpointDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,20 +277,22 @@ static RequestDelegate CreateHandlerRequestDelegate(Delegate handler, RequestDel

private RequestDelegateFactoryOptions CreateRdfOptions(RouteEntry entry, RoutePattern pattern, RouteEndpointBuilder builder)
{
var routeParamNames = new List<string>(pattern.Parameters.Count);
foreach (var parameter in pattern.Parameters)
{
routeParamNames.Add(parameter.Name);
}

return new()
{
ServiceProvider = _applicationServices,
RouteParameterNames = routeParamNames,
RouteParameterNames = ProduceRouteParamNames(),
ThrowOnBadRequest = _throwOnBadRequest,
DisableInferBodyFromParameters = ShouldDisableInferredBodyParameters(entry.HttpMethods),
EndpointBuilder = builder,
};

IEnumerable<string> ProduceRouteParamNames()
{
foreach (var routePatternPart in pattern.Parameters)
{
yield return routePatternPart.Name;
}
}
}

private static bool ShouldDisableInferredBodyParameters(IEnumerable<string>? httpMethods)
Expand Down