1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ #nullable enable
5
+
4
6
using System . ComponentModel ;
5
7
using System . Reflection ;
6
8
using System . Security . Claims ;
@@ -111,8 +113,6 @@ public void AddsMultipleRequestFormatsFromMetadataWithRequestTypeAndOptionalBody
111
113
Assert . False ( apiParameterDescription . IsRequired ) ;
112
114
}
113
115
114
- #nullable enable
115
-
116
116
[ Fact ]
117
117
public void AddsMultipleRequestFormatsFromMetadataWithRequiredBodyParameter ( )
118
118
{
@@ -128,8 +128,6 @@ public void AddsMultipleRequestFormatsFromMetadataWithRequiredBodyParameter()
128
128
Assert . True ( apiParameterDescription . IsRequired ) ;
129
129
}
130
130
131
- #nullable disable
132
-
133
131
[ Fact ]
134
132
public void AddsJsonResponseFormatWhenFromBodyInferred ( )
135
133
{
@@ -415,8 +413,6 @@ public void AddsDefaultValueFromParameters()
415
413
Assert . Equal ( 42 , param . DefaultValue ) ;
416
414
}
417
415
418
- #nullable enable
419
-
420
416
[ Fact ]
421
417
public void AddsMultipleParameters ( )
422
418
{
@@ -446,7 +442,6 @@ public void AddsMultipleParameters()
446
442
}
447
443
448
444
#nullable disable
449
-
450
445
[ Fact ]
451
446
public void AddsMultipleParametersFromParametersAttribute ( )
452
447
{
@@ -488,6 +483,7 @@ static void AssertParameters(ApiDescription apiDescription, string capturedName
488
483
AssertParameters ( GetApiDescription ( ( [ AsParameters ] ArgumentListRecordWithoutAttributes req ) => { } , "/{foo}" ) , "foo" ) ;
489
484
AssertParameters ( GetApiDescription ( ( [ AsParameters ] ArgumentListRecordWithoutAttributes req ) => { } , "/{Foo}" ) ) ;
490
485
}
486
+ #nullable enable
491
487
492
488
[ Fact ]
493
489
public void TestParameterIsRequired ( )
@@ -531,6 +527,7 @@ public void AddsMetadataFromRouteEndpoint()
531
527
Assert . True ( apiExplorerSettings . IgnoreApi ) ;
532
528
}
533
529
530
+ #nullable disable
534
531
[ Fact ]
535
532
public void TestParameterIsRequiredForObliviousNullabilityContext ( )
536
533
{
@@ -577,6 +574,7 @@ public void TestParameterAttributesCanBeInspected()
577
574
Assert . NotNull ( description ) ;
578
575
Assert . Equal ( "The name." , description . Description ) ;
579
576
}
577
+ #nullable enable
580
578
581
579
[ Fact ]
582
580
public void RespectsProducesProblemExtensionMethod ( )
@@ -773,11 +771,12 @@ public void HandleAcceptsMetadata()
773
771
} ) ;
774
772
}
775
773
774
+ #nullable disable
776
775
[ Fact ]
777
776
public void HandleAcceptsMetadataWithTypeParameter ( )
778
777
{
779
778
// Arrange
780
- var builder = new TestEndpointRouteBuilder ( new ApplicationBuilder ( null ) ) ;
779
+ var builder = new TestEndpointRouteBuilder ( new ApplicationBuilder ( TestServiceProvider . Instance ) ) ;
781
780
builder . MapPost ( "/api/todos" , ( InferredJsonClass inferredJsonClass ) => "" )
782
781
. Accepts ( typeof ( InferredJsonClass ) , "application/json" ) ;
783
782
var context = new ApiDescriptionProviderContext ( Array . Empty < ActionDescriptor > ( ) ) ;
@@ -800,6 +799,7 @@ public void HandleAcceptsMetadataWithTypeParameter()
800
799
Assert . Equal ( "inferredJsonClass" , bodyParameterDescription . Name ) ;
801
800
Assert . False ( bodyParameterDescription . IsRequired ) ;
802
801
}
802
+ #nullable enable
803
803
804
804
[ Fact ]
805
805
public void FavorsProducesMetadataOverAttribute ( )
@@ -832,15 +832,11 @@ public void FavorsProducesMetadataOverAttribute()
832
832
} ) ;
833
833
}
834
834
835
- #nullable enable
836
-
837
835
[ Fact ]
838
836
public void HandleDefaultIAcceptsMetadataForRequiredBodyParameter ( )
839
837
{
840
838
// 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 ) ) ;
844
840
builder . MapPost ( "/api/todos" , ( InferredJsonClass inferredJsonClass ) => "" ) ;
845
841
var context = new ApiDescriptionProviderContext ( Array . Empty < ActionDescriptor > ( ) ) ;
846
842
@@ -872,9 +868,7 @@ public void HandleDefaultIAcceptsMetadataForRequiredBodyParameter()
872
868
public void HandleDefaultIAcceptsMetadataForOptionalBodyParameter ( )
873
869
{
874
870
// 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 ) ) ;
878
872
builder . MapPost ( "/api/todos" , ( InferredJsonClass ? inferredJsonClass ) => "" ) ;
879
873
var context = new ApiDescriptionProviderContext ( Array . Empty < ActionDescriptor > ( ) ) ;
880
874
@@ -906,9 +900,7 @@ public void HandleDefaultIAcceptsMetadataForOptionalBodyParameter()
906
900
public void HandleIAcceptsMetadataWithConsumesAttributeAndInferredOptionalFromBodyType ( )
907
901
{
908
902
// 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 ) ) ;
912
904
builder . MapPost ( "/api/todos" , [ Consumes ( "application/xml" ) ] ( InferredJsonClass ? inferredJsonClass ) => "" ) ;
913
905
var context = new ApiDescriptionProviderContext ( Array . Empty < ActionDescriptor > ( ) ) ;
914
906
@@ -940,9 +932,7 @@ public void HandleIAcceptsMetadataWithConsumesAttributeAndInferredOptionalFromBo
940
932
public void HandleDefaultIAcceptsMetadataForRequiredFormFileParameter ( )
941
933
{
942
934
// 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 ) ) ;
946
936
builder . MapPost ( "/file/upload" , ( IFormFile formFile ) => "" ) ;
947
937
var context = new ApiDescriptionProviderContext ( Array . Empty < ActionDescriptor > ( ) ) ;
948
938
@@ -971,9 +961,7 @@ public void HandleDefaultIAcceptsMetadataForRequiredFormFileParameter()
971
961
public void HandleDefaultIAcceptsMetadataForOptionalFormFileParameter ( )
972
962
{
973
963
// 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 ) ) ;
977
965
builder . MapPost ( "/file/upload" , ( IFormFile ? inferredFormFile ) => "" ) ;
978
966
var context = new ApiDescriptionProviderContext ( Array . Empty < ActionDescriptor > ( ) ) ;
979
967
@@ -1002,9 +990,7 @@ public void HandleDefaultIAcceptsMetadataForOptionalFormFileParameter()
1002
990
public void AddsMultipartFormDataResponseFormatWhenFormFileSpecified ( )
1003
991
{
1004
992
// 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 ) ) ;
1008
994
builder . MapPost ( "/file/upload" , ( IFormFile file ) => Results . NoContent ( ) ) ;
1009
995
var context = new ApiDescriptionProviderContext ( Array . Empty < ActionDescriptor > ( ) ) ;
1010
996
@@ -1092,9 +1078,7 @@ public void AddsMultipartFormDataResponseFormatWhenFormFileCollectionSpecified()
1092
1078
static void AssertFormFileCollection ( Delegate handler , string expectedName )
1093
1079
{
1094
1080
// 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 ) ) ;
1098
1082
builder . MapPost ( "/file/upload" , handler ) ;
1099
1083
var context = new ApiDescriptionProviderContext ( Array . Empty < ActionDescriptor > ( ) ) ;
1100
1084
@@ -1119,8 +1103,6 @@ static void AssertFormFileCollection(Delegate handler, string expectedName)
1119
1103
}
1120
1104
}
1121
1105
1122
- #nullable restore
1123
-
1124
1106
[ Fact ]
1125
1107
public void ProducesRouteInfoOnlyForRouteParameters ( )
1126
1108
{
@@ -1287,9 +1269,9 @@ private static IEnumerable<string> GetSortedMediaTypes(ApiResponseType apiRespon
1287
1269
1288
1270
private static IList < ApiDescription > GetApiDescriptions (
1289
1271
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 )
1293
1275
{
1294
1276
var methodInfo = action . Method ;
1295
1277
var attributes = methodInfo . GetCustomAttributes ( ) ;
@@ -1318,9 +1300,9 @@ private static IList<ApiDescription> GetApiDescriptions(
1318
1300
new ServiceProviderIsService ( ) ) ;
1319
1301
1320
1302
private static TestEndpointRouteBuilder CreateBuilder ( ) =>
1321
- new TestEndpointRouteBuilder ( new ApplicationBuilder ( new TestServiceProvider ( ) ) ) ;
1303
+ new TestEndpointRouteBuilder ( new ApplicationBuilder ( TestServiceProvider . Instance ) ) ;
1322
1304
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 ) =>
1324
1306
Assert . Single ( GetApiDescriptions ( action , pattern , displayName : displayName , httpMethods : httpMethods ) ) ;
1325
1307
1326
1308
private static void TestAction ( )
@@ -1393,28 +1375,28 @@ public static bool TryParse(string value, out BindAsyncRecord result) =>
1393
1375
throw new NotImplementedException ( ) ;
1394
1376
}
1395
1377
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 ) ;
1397
1379
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 ) ;
1399
1381
1400
- private record ArgumentListRecordWithoutAttributes ( int Foo , int Bar , InferredJsonClass FromBody , HttpContext context ) ;
1382
+ private record ArgumentListRecordWithoutAttributes ( int Foo , int Bar , InferredJsonClass ? FromBody , HttpContext context ) ;
1401
1383
1402
1384
private record ArgumentListRecordWithoutPositionalParameters
1403
1385
{
1404
1386
[ FromRoute ]
1405
1387
public int Foo { get ; set ; }
1406
1388
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 ! ;
1409
1391
}
1410
1392
1411
1393
private class ArgumentListClass
1412
1394
{
1413
1395
[ FromRoute ]
1414
1396
public int Foo { get ; set ; }
1415
1397
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 ! ;
1418
1400
}
1419
1401
1420
1402
private class ArgumentListClassWithReadOnlyProperties : ArgumentListClass
@@ -1427,17 +1409,15 @@ private struct ArgumentListStruct
1427
1409
[ FromRoute ]
1428
1410
public int Foo { get ; set ; }
1429
1411
public int Bar { get ; set ; }
1430
- public InferredJsonClass FromBody { get ; set ; }
1412
+ public InferredJsonClass ? FromBody { get ; set ; }
1431
1413
public HttpContext Context { get ; set ; }
1432
1414
}
1433
1415
1434
1416
private class TestServiceProvider : IServiceProvider
1435
1417
{
1436
- public void Dispose ( )
1437
- {
1438
- }
1418
+ public static TestServiceProvider Instance { get ; } = new TestServiceProvider ( ) ;
1439
1419
1440
- public object GetService ( Type serviceType )
1420
+ public object ? GetService ( Type serviceType )
1441
1421
{
1442
1422
if ( serviceType == typeof ( IOptions < RouteHandlerOptions > ) )
1443
1423
{
0 commit comments