Skip to content

Commit a36e57c

Browse files
committed
Address more pr feedback
1 parent 76bd3b4 commit a36e57c

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,6 @@ public async Task NoExceptionAreThrownForBadRequestsInProduction()
12971297

12981298
app.MapGet("/{parameterName}", (int parameterName) => { });
12991299

1300-
13011300
await app.StartAsync();
13021301

13031302
var client = app.GetTestClient();

src/Http/Routing/src/Builder/DelegateEndpointRouteBuilderExtensions.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,13 @@ public static DelegateEndpointConventionBuilder Map(
170170
routeParams.Add(part.Name);
171171
}
172172

173-
// REVIEW: Should we just default ThrowOnBadRequest to false if Options is somehow missing?
174-
var routeHandlerOptions = endpoints.ServiceProvider.GetRequiredService<IOptions<RouteHandlerOptions>>();
173+
var routeHandlerOptions = endpoints.ServiceProvider?.GetService<IOptions<RouteHandlerOptions>>();
175174

176175
var options = new RequestDelegateFactoryOptions
177176
{
178177
ServiceProvider = endpoints.ServiceProvider,
179178
RouteParameterNames = routeParams,
180-
ThrowOnBadRequest = routeHandlerOptions.Value.ThrowOnBadRequest
179+
ThrowOnBadRequest = routeHandlerOptions?.Value.ThrowOnBadRequest ?? false,
181180
};
182181

183182
var requestDelegateResult = RequestDelegateFactory.Create(handler, options);

src/Http/Routing/src/DependencyInjection/RoutingServiceCollectionExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static IServiceCollection AddRouting(this IServiceCollection services)
9999
services.TryAddSingleton<TemplateBinderFactory, DefaultTemplateBinderFactory>();
100100
services.TryAddSingleton<RoutePatternTransformer, DefaultRoutePatternTransformer>();
101101

102-
// if (_environment.IsDevelopment()) RouteHandlerOptions.ThrowOnBadRequest = true;
102+
// Set RouteHandlerOptions.ThrowOnBadRequest in development
103103
services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<RouteHandlerOptions>, ConfigureRouteHandlerOptions>());
104104

105105
return services;

src/Http/Routing/test/UnitTests/Builder/DelegateEndpointRouteBuilderExtensionsTest.cs

+19-7
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,6 @@ public void WithTags_CanSetTagsForEndpoint()
488488
Assert.Equal(new[] { "Some", "Test", "Tags" }, tagsMetadata?.Tags);
489489
}
490490

491-
[Fact]
492-
public void MapMethod_ThrowsIfItCannotResolveRouteHandlerOptions()
493-
{
494-
var builder = new DefaultEndpointRouteBuilder(new ApplicationBuilder(new ServiceCollection().BuildServiceProvider()));
495-
Assert.Throws<InvalidOperationException>(() => builder.Map("/", () => { }));
496-
}
497-
498491
[Theory]
499492
[InlineData(true)]
500493
[InlineData(false)]
@@ -526,6 +519,25 @@ public async Task MapMethod_FlowsThrowOnBadHttpRequest(bool throwOnBadRequest)
526519
}
527520
}
528521

522+
[Fact]
523+
public async Task MapMethod_DefaultsToNotThrowOnBadHttpRequestIfItCannotResolveRouteHandlerOptions()
524+
{
525+
var builder = new DefaultEndpointRouteBuilder(new ApplicationBuilder(new ServiceCollection().BuildServiceProvider()));
526+
527+
_ = builder.Map("/{id}", (int id) => { });
528+
529+
var dataSource = GetBuilderEndpointDataSource(builder);
530+
// Trigger Endpoint build by calling getter.
531+
var endpoint = Assert.Single(dataSource.Endpoints);
532+
533+
var httpContext = new DefaultHttpContext();
534+
httpContext.RequestServices = new ServiceCollection().AddLogging().BuildServiceProvider();
535+
httpContext.Request.RouteValues["id"] = "invalid!";
536+
537+
await endpoint.RequestDelegate!(httpContext);
538+
Assert.Equal(400, httpContext.Response.StatusCode);
539+
}
540+
529541
class FromRoute : Attribute, IFromRouteMetadata
530542
{
531543
public string? Name { get; set; }

0 commit comments

Comments
 (0)