Skip to content

Commit 97a5ada

Browse files
committed
Fix some tests and doc comments
1 parent e1c8b91 commit 97a5ada

File tree

5 files changed

+62
-71
lines changed

5 files changed

+62
-71
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,12 +1572,13 @@ public async Task BranchingPipelineHasOwnRoutes()
15721572
app.Start();
15731573

15741574
var ds = app.Services.GetRequiredService<EndpointDataSource>();
1575-
Assert.Equal(5, ds.Endpoints.Count);
1576-
Assert.Equal("One", ds.Endpoints[0].DisplayName);
1577-
Assert.Equal("Two", ds.Endpoints[1].DisplayName);
1578-
Assert.Equal("Three", ds.Endpoints[2].DisplayName);
1579-
Assert.Equal("Four", ds.Endpoints[3].DisplayName);
1580-
Assert.Equal("Five", ds.Endpoints[4].DisplayName);
1575+
var displayNames = ds.Endpoints.Select(e => e.DisplayName).ToArray();
1576+
Assert.Equal(5, displayNames.Length);
1577+
Assert.Contains("One", displayNames);
1578+
Assert.Contains("Two", displayNames);
1579+
Assert.Contains("Three", displayNames);
1580+
Assert.Contains("Four", displayNames);
1581+
Assert.Contains("Five", displayNames);
15811582

15821583
var client = app.GetTestClient();
15831584

src/Http/Http.Extensions/src/RequestDelegateFactoryOptions.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.AspNetCore.Builder;
45
using Microsoft.AspNetCore.Http.Metadata;
56
using Microsoft.Extensions.Logging;
67

@@ -38,13 +39,10 @@ public sealed class RequestDelegateFactoryOptions
3839
public IReadOnlyList<Func<RouteHandlerContext, RouteHandlerFilterDelegate, RouteHandlerFilterDelegate>>? RouteHandlerFilterFactories { get; init; }
3940

4041
/// <summary>
41-
/// The initial endpoint metadata to add as part of the creation of the <see cref="RequestDelegateResult.RequestDelegate"/>.
42+
/// The <see cref="EndpointBuilder.Metadata"/> to use for the creation of the <see cref="RequestDelegateResult.RequestDelegate"/>.
4243
/// </summary>
4344
/// <remarks>
44-
/// This metadata will be included in <see cref="RequestDelegateResult.EndpointMetadata" /> <b>before</b> any metadata inferred during creation of the
45-
/// <see cref="RequestDelegateResult.RequestDelegate"/> and <b>before</b> any metadata provided by types in the delegate signature that implement
46-
/// <see cref="IEndpointMetadataProvider" /> or <see cref="IEndpointParameterMetadataProvider" />, i.e. this metadata will be less specific than any
47-
/// inferred by the call to <see cref="RequestDelegateFactory.Create(Delegate, RequestDelegateFactoryOptions?)"/>.
45+
/// This metadata will be returned as <see cref="RequestDelegateResult.EndpointMetadata" /> if it is non-null.
4846
/// </remarks>
4947
public IList<object>? EndpointMetadata { get; init; }
5048
}

src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.cs

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#nullable enable
5+
46
using System.ComponentModel;
57
using System.Reflection;
68
using System.Security.Claims;
@@ -111,8 +113,6 @@ public void AddsMultipleRequestFormatsFromMetadataWithRequestTypeAndOptionalBody
111113
Assert.False(apiParameterDescription.IsRequired);
112114
}
113115

114-
#nullable enable
115-
116116
[Fact]
117117
public void AddsMultipleRequestFormatsFromMetadataWithRequiredBodyParameter()
118118
{
@@ -128,8 +128,6 @@ public void AddsMultipleRequestFormatsFromMetadataWithRequiredBodyParameter()
128128
Assert.True(apiParameterDescription.IsRequired);
129129
}
130130

131-
#nullable disable
132-
133131
[Fact]
134132
public void AddsJsonResponseFormatWhenFromBodyInferred()
135133
{
@@ -415,8 +413,6 @@ public void AddsDefaultValueFromParameters()
415413
Assert.Equal(42, param.DefaultValue);
416414
}
417415

418-
#nullable enable
419-
420416
[Fact]
421417
public void AddsMultipleParameters()
422418
{
@@ -446,7 +442,6 @@ public void AddsMultipleParameters()
446442
}
447443

448444
#nullable disable
449-
450445
[Fact]
451446
public void AddsMultipleParametersFromParametersAttribute()
452447
{
@@ -488,6 +483,7 @@ static void AssertParameters(ApiDescription apiDescription, string capturedName
488483
AssertParameters(GetApiDescription(([AsParameters] ArgumentListRecordWithoutAttributes req) => { }, "/{foo}"), "foo");
489484
AssertParameters(GetApiDescription(([AsParameters] ArgumentListRecordWithoutAttributes req) => { }, "/{Foo}"));
490485
}
486+
#nullable enable
491487

492488
[Fact]
493489
public void TestParameterIsRequired()
@@ -531,6 +527,7 @@ public void AddsMetadataFromRouteEndpoint()
531527
Assert.True(apiExplorerSettings.IgnoreApi);
532528
}
533529

530+
#nullable disable
534531
[Fact]
535532
public void TestParameterIsRequiredForObliviousNullabilityContext()
536533
{
@@ -577,6 +574,7 @@ public void TestParameterAttributesCanBeInspected()
577574
Assert.NotNull(description);
578575
Assert.Equal("The name.", description.Description);
579576
}
577+
#nullable enable
580578

581579
[Fact]
582580
public void RespectsProducesProblemExtensionMethod()
@@ -773,11 +771,12 @@ public void HandleAcceptsMetadata()
773771
});
774772
}
775773

774+
#nullable disable
776775
[Fact]
777776
public void HandleAcceptsMetadataWithTypeParameter()
778777
{
779778
// Arrange
780-
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(null));
779+
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(TestServiceProvider.Instance));
781780
builder.MapPost("/api/todos", (InferredJsonClass inferredJsonClass) => "")
782781
.Accepts(typeof(InferredJsonClass), "application/json");
783782
var context = new ApiDescriptionProviderContext(Array.Empty<ActionDescriptor>());
@@ -800,6 +799,7 @@ public void HandleAcceptsMetadataWithTypeParameter()
800799
Assert.Equal("inferredJsonClass", bodyParameterDescription.Name);
801800
Assert.False(bodyParameterDescription.IsRequired);
802801
}
802+
#nullable enable
803803

804804
[Fact]
805805
public void FavorsProducesMetadataOverAttribute()
@@ -832,15 +832,11 @@ public void FavorsProducesMetadataOverAttribute()
832832
});
833833
}
834834

835-
#nullable enable
836-
837835
[Fact]
838836
public void HandleDefaultIAcceptsMetadataForRequiredBodyParameter()
839837
{
840838
// Arrange
841-
var services = new ServiceCollection();
842-
var serviceProvider = services.BuildServiceProvider();
843-
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(serviceProvider));
839+
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(TestServiceProvider.Instance));
844840
builder.MapPost("/api/todos", (InferredJsonClass inferredJsonClass) => "");
845841
var context = new ApiDescriptionProviderContext(Array.Empty<ActionDescriptor>());
846842

@@ -872,9 +868,7 @@ public void HandleDefaultIAcceptsMetadataForRequiredBodyParameter()
872868
public void HandleDefaultIAcceptsMetadataForOptionalBodyParameter()
873869
{
874870
// Arrange
875-
var services = new ServiceCollection();
876-
var serviceProvider = services.BuildServiceProvider();
877-
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(serviceProvider));
871+
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(TestServiceProvider.Instance));
878872
builder.MapPost("/api/todos", (InferredJsonClass? inferredJsonClass) => "");
879873
var context = new ApiDescriptionProviderContext(Array.Empty<ActionDescriptor>());
880874

@@ -906,9 +900,7 @@ public void HandleDefaultIAcceptsMetadataForOptionalBodyParameter()
906900
public void HandleIAcceptsMetadataWithConsumesAttributeAndInferredOptionalFromBodyType()
907901
{
908902
// Arrange
909-
var services = new ServiceCollection();
910-
var serviceProvider = services.BuildServiceProvider();
911-
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(serviceProvider));
903+
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(TestServiceProvider.Instance));
912904
builder.MapPost("/api/todos", [Consumes("application/xml")] (InferredJsonClass? inferredJsonClass) => "");
913905
var context = new ApiDescriptionProviderContext(Array.Empty<ActionDescriptor>());
914906

@@ -940,9 +932,7 @@ public void HandleIAcceptsMetadataWithConsumesAttributeAndInferredOptionalFromBo
940932
public void HandleDefaultIAcceptsMetadataForRequiredFormFileParameter()
941933
{
942934
// Arrange
943-
var services = new ServiceCollection();
944-
var serviceProvider = services.BuildServiceProvider();
945-
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(serviceProvider));
935+
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(TestServiceProvider.Instance));
946936
builder.MapPost("/file/upload", (IFormFile formFile) => "");
947937
var context = new ApiDescriptionProviderContext(Array.Empty<ActionDescriptor>());
948938

@@ -971,9 +961,7 @@ public void HandleDefaultIAcceptsMetadataForRequiredFormFileParameter()
971961
public void HandleDefaultIAcceptsMetadataForOptionalFormFileParameter()
972962
{
973963
// Arrange
974-
var services = new ServiceCollection();
975-
var serviceProvider = services.BuildServiceProvider();
976-
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(serviceProvider));
964+
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(TestServiceProvider.Instance));
977965
builder.MapPost("/file/upload", (IFormFile? inferredFormFile) => "");
978966
var context = new ApiDescriptionProviderContext(Array.Empty<ActionDescriptor>());
979967

@@ -1002,9 +990,7 @@ public void HandleDefaultIAcceptsMetadataForOptionalFormFileParameter()
1002990
public void AddsMultipartFormDataResponseFormatWhenFormFileSpecified()
1003991
{
1004992
// Arrange
1005-
var services = new ServiceCollection();
1006-
var serviceProvider = services.BuildServiceProvider();
1007-
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(serviceProvider));
993+
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(TestServiceProvider.Instance));
1008994
builder.MapPost("/file/upload", (IFormFile file) => Results.NoContent());
1009995
var context = new ApiDescriptionProviderContext(Array.Empty<ActionDescriptor>());
1010996

@@ -1092,9 +1078,7 @@ public void AddsMultipartFormDataResponseFormatWhenFormFileCollectionSpecified()
10921078
static void AssertFormFileCollection(Delegate handler, string expectedName)
10931079
{
10941080
// Arrange
1095-
var services = new ServiceCollection();
1096-
var serviceProvider = services.BuildServiceProvider();
1097-
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(serviceProvider));
1081+
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(TestServiceProvider.Instance));
10981082
builder.MapPost("/file/upload", handler);
10991083
var context = new ApiDescriptionProviderContext(Array.Empty<ActionDescriptor>());
11001084

@@ -1119,8 +1103,6 @@ static void AssertFormFileCollection(Delegate handler, string expectedName)
11191103
}
11201104
}
11211105

1122-
#nullable restore
1123-
11241106
[Fact]
11251107
public void ProducesRouteInfoOnlyForRouteParameters()
11261108
{
@@ -1287,9 +1269,9 @@ private static IEnumerable<string> GetSortedMediaTypes(ApiResponseType apiRespon
12871269

12881270
private static IList<ApiDescription> GetApiDescriptions(
12891271
Delegate action,
1290-
string pattern = null,
1291-
IEnumerable<string> httpMethods = null,
1292-
string displayName = null)
1272+
string? pattern = null,
1273+
IEnumerable<string>? httpMethods = null,
1274+
string? displayName = null)
12931275
{
12941276
var methodInfo = action.Method;
12951277
var attributes = methodInfo.GetCustomAttributes();
@@ -1318,9 +1300,9 @@ private static IList<ApiDescription> GetApiDescriptions(
13181300
new ServiceProviderIsService());
13191301

13201302
private static TestEndpointRouteBuilder CreateBuilder() =>
1321-
new TestEndpointRouteBuilder(new ApplicationBuilder(new TestServiceProvider()));
1303+
new TestEndpointRouteBuilder(new ApplicationBuilder(TestServiceProvider.Instance));
13221304

1323-
private static ApiDescription GetApiDescription(Delegate action, string pattern = null, string displayName = null, IEnumerable<string> httpMethods = null) =>
1305+
private static ApiDescription GetApiDescription(Delegate action, string? pattern = null, string displayName = null, IEnumerable<string>? httpMethods = null) =>
13241306
Assert.Single(GetApiDescriptions(action, pattern, displayName: displayName, httpMethods: httpMethods));
13251307

13261308
private static void TestAction()
@@ -1393,28 +1375,28 @@ public static bool TryParse(string value, out BindAsyncRecord result) =>
13931375
throw new NotImplementedException();
13941376
}
13951377

1396-
private record ArgumentListRecord([FromRoute] int Foo, int Bar, InferredJsonClass FromBody, HttpContext context);
1378+
private record ArgumentListRecord([FromRoute] int Foo, int Bar, InferredJsonClass? FromBody, HttpContext context);
13971379

1398-
private record struct ArgumentListRecordStruct([FromRoute] int Foo, int Bar, InferredJsonClass FromBody, HttpContext context);
1380+
private record struct ArgumentListRecordStruct([FromRoute] int Foo, int Bar, InferredJsonClass? FromBody, HttpContext context);
13991381

1400-
private record ArgumentListRecordWithoutAttributes(int Foo, int Bar, InferredJsonClass FromBody, HttpContext context);
1382+
private record ArgumentListRecordWithoutAttributes(int Foo, int Bar, InferredJsonClass? FromBody, HttpContext context);
14011383

14021384
private record ArgumentListRecordWithoutPositionalParameters
14031385
{
14041386
[FromRoute]
14051387
public int Foo { get; set; }
14061388
public int Bar { get; set; }
1407-
public InferredJsonClass FromBody { get; set; }
1408-
public HttpContext Context { get; set; }
1389+
public InferredJsonClass? FromBody { get; set; }
1390+
public HttpContext Context { get; set; } = null!;
14091391
}
14101392

14111393
private class ArgumentListClass
14121394
{
14131395
[FromRoute]
14141396
public int Foo { get; set; }
14151397
public int Bar { get; set; }
1416-
public InferredJsonClass FromBody { get; set; }
1417-
public HttpContext Context { get; set; }
1398+
public InferredJsonClass? FromBody { get; set; }
1399+
public HttpContext Context { get; set; } = null!;
14181400
}
14191401

14201402
private class ArgumentListClassWithReadOnlyProperties : ArgumentListClass
@@ -1427,17 +1409,15 @@ private struct ArgumentListStruct
14271409
[FromRoute]
14281410
public int Foo { get; set; }
14291411
public int Bar { get; set; }
1430-
public InferredJsonClass FromBody { get; set; }
1412+
public InferredJsonClass? FromBody { get; set; }
14311413
public HttpContext Context { get; set; }
14321414
}
14331415

14341416
private class TestServiceProvider : IServiceProvider
14351417
{
1436-
public void Dispose()
1437-
{
1438-
}
1418+
public static TestServiceProvider Instance { get; } = new TestServiceProvider();
14391419

1440-
public object GetService(Type serviceType)
1420+
public object? GetService(Type serviceType)
14411421
{
14421422
if (serviceType == typeof(IOptions<RouteHandlerOptions>))
14431423
{

src/OpenApi/src/OpenApiRouteHandlerBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ private static void AddAndConfigureOperationForEndpoint(EndpointBuilder endpoint
7979
var pattern = routeEndpointBuilder.RoutePattern;
8080
var metadata = new EndpointMetadataCollection(routeEndpointBuilder.Metadata);
8181
var methodInfo = metadata.OfType<MethodInfo>().SingleOrDefault();
82-
var applicationServices = routeEndpointBuilder.ApplicationServices;
8382

84-
if (methodInfo is null || applicationServices is null)
83+
if (methodInfo is null)
8584
{
8685
return;
8786
}
8887

88+
var applicationServices = routeEndpointBuilder.ApplicationServices;
8989
var hostEnvironment = applicationServices.GetService<IHostEnvironment>();
9090
var serviceProviderIsService = applicationServices.GetService<IServiceProviderIsService>();
9191
var generator = new OpenApiGenerator(hostEnvironment, serviceProviderIsService);

src/OpenApi/test/OpenApiRouteHandlerBuilderExtensionTests.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,26 @@ static void WithLocalSummary(RouteHandlerBuilder builder)
134134

135135
WithLocalSummary(innerGroup.MapDelete("/inner-b", GetString));
136136

137-
var summaries = builder.DataSources.SelectMany(ds => ds.Endpoints).Select(e => e.Metadata.GetMetadata<OpenApiOperation>().Summary).ToArray();
138-
139-
Assert.Equal(5, summaries.Length);
140-
Assert.Contains(" | Local Summary | 200 Status Response Content-Type: text/plain", summaries);
141-
Assert.Contains("Outer Group Summary | Local Summary | 200 Status Response Content-Type: text/plain", summaries);
142-
Assert.Contains("Outer Group Summary | Local Summary | 200 Status Response Content-Type: text/plain", summaries);
143-
Assert.Contains("Outer Group Summary | Inner Group Summary | Local Summary | 200 Status Response Content-Type: text/plain", summaries);
144-
Assert.Contains("Outer Group Summary | Inner Group Summary | Local Summary | 200 Status Response Content-Type: text/plain", summaries);
137+
var summaries = builder.DataSources
138+
.SelectMany(ds => ds.Endpoints)
139+
.ToDictionary(
140+
e => ((RouteEndpoint)e).RoutePattern.RawText,
141+
e => e.Metadata.GetMetadata<OpenApiOperation>().Summary);
142+
143+
Assert.Equal(5, summaries.Count);
144+
145+
Assert.Equal(" | Local Summary | 200 Status Response Content-Type: text/plain",
146+
summaries["/root"]);
147+
148+
Assert.Equal("Outer Group Summary | Local Summary | 200 Status Response Content-Type: text/plain",
149+
summaries["/outer/outer-a"]);
150+
Assert.Equal("Outer Group Summary | Local Summary | 200 Status Response Content-Type: text/plain",
151+
summaries["/outer/outer-b"]);
152+
153+
Assert.Equal("Outer Group Summary | Inner Group Summary | Local Summary | 200 Status Response Content-Type: text/plain",
154+
summaries["/outer/inner/inner-a"]);
155+
Assert.Equal("Outer Group Summary | Inner Group Summary | Local Summary | 200 Status Response Content-Type: text/plain",
156+
summaries["/outer/inner/inner-b"]);
145157
}
146158

147159
private RouteEndpointDataSource GetBuilderEndpointDataSource(IEndpointRouteBuilder endpointRouteBuilder)

0 commit comments

Comments
 (0)