@@ -76,28 +76,31 @@ static void AssertJsonRequestFormat(ApiDescription apiDescription)
76
76
[ Fact ]
77
77
public void AddsRequestFormatFromMetadata ( )
78
78
{
79
- static void AssertustomRequestFormat ( ApiDescription apiDescription )
79
+ static void AssertCustomRequestFormat ( ApiDescription apiDescription )
80
80
{
81
81
var requestFormat = Assert . Single ( apiDescription . SupportedRequestFormats ) ;
82
82
Assert . Equal ( "application/custom" , requestFormat . MediaType ) ;
83
83
Assert . Null ( requestFormat . Formatter ) ;
84
84
}
85
85
86
- AssertustomRequestFormat ( GetApiDescription (
86
+ AssertCustomRequestFormat ( GetApiDescription (
87
87
[ Consumes ( "application/custom" ) ]
88
- ( InferredJsonClass fromBody ) => { } ) ) ;
88
+ ( InferredJsonClass fromBody ) =>
89
+ { } ) ) ;
89
90
90
- AssertustomRequestFormat ( GetApiDescription (
91
+ AssertCustomRequestFormat ( GetApiDescription (
91
92
[ Consumes ( "application/custom" ) ]
92
- ( [ FromBody ] int fromBody ) => { } ) ) ;
93
+ ( [ FromBody ] int fromBody ) =>
94
+ { } ) ) ;
93
95
}
94
96
95
97
[ Fact ]
96
98
public void AddsMultipleRequestFormatsFromMetadata ( )
97
99
{
98
100
var apiDescription = GetApiDescription (
99
101
[ Consumes ( "application/custom0" , "application/custom1" ) ]
100
- ( InferredJsonClass fromBody ) => { } ) ;
102
+ ( InferredJsonClass fromBody ) =>
103
+ { } ) ;
101
104
102
105
Assert . Equal ( 2 , apiDescription . SupportedRequestFormats . Count ) ;
103
106
@@ -167,8 +170,8 @@ public void AddsResponseFormatFromMetadata()
167
170
{
168
171
var apiDescription = GetApiDescription (
169
172
[ ProducesResponseType ( typeof ( TimeSpan ) , StatusCodes . Status201Created ) ]
170
- [ Produces ( "application/custom" ) ]
171
- ( ) => new InferredJsonClass ( ) ) ;
173
+ [ Produces ( "application/custom" ) ]
174
+ ( ) => new InferredJsonClass ( ) ) ;
172
175
173
176
var responseType = Assert . Single ( apiDescription . SupportedResponseTypes ) ;
174
177
@@ -185,8 +188,8 @@ public void AddsMultipleResponseFormatsFromMetadataWithPoco()
185
188
{
186
189
var apiDescription = GetApiDescription (
187
190
[ ProducesResponseType ( typeof ( TimeSpan ) , StatusCodes . Status201Created ) ]
188
- [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
189
- ( ) => new InferredJsonClass ( ) ) ;
191
+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
192
+ ( ) => new InferredJsonClass ( ) ) ;
190
193
191
194
Assert . Equal ( 2 , apiDescription . SupportedResponseTypes . Count ) ;
192
195
@@ -214,8 +217,8 @@ public void AddsMultipleResponseFormatsFromMetadataWithIResult()
214
217
{
215
218
var apiDescription = GetApiDescription (
216
219
[ ProducesResponseType ( typeof ( InferredJsonClass ) , StatusCodes . Status201Created ) ]
217
- [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
218
- ( ) => Results . Ok ( new InferredJsonClass ( ) ) ) ;
220
+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
221
+ ( ) => Results . Ok ( new InferredJsonClass ( ) ) ) ;
219
222
220
223
Assert . Equal ( 2 , apiDescription . SupportedResponseTypes . Count ) ;
221
224
@@ -324,18 +327,68 @@ public void AddsMultipleParameters()
324
327
Assert . Equal ( typeof ( int ) , fooParam . Type ) ;
325
328
Assert . Equal ( typeof ( int ) , fooParam . ModelMetadata . ModelType ) ;
326
329
Assert . Equal ( BindingSource . Path , fooParam . Source ) ;
330
+ Assert . True ( fooParam . IsRequired ) ;
327
331
328
332
var barParam = apiDescription . ParameterDescriptions [ 1 ] ;
329
333
Assert . Equal ( typeof ( int ) , barParam . Type ) ;
330
334
Assert . Equal ( typeof ( int ) , barParam . ModelMetadata . ModelType ) ;
331
335
Assert . Equal ( BindingSource . Query , barParam . Source ) ;
336
+ Assert . True ( barParam . IsRequired ) ;
332
337
333
338
var fromBodyParam = apiDescription . ParameterDescriptions [ 2 ] ;
334
339
Assert . Equal ( typeof ( InferredJsonClass ) , fromBodyParam . Type ) ;
335
340
Assert . Equal ( typeof ( InferredJsonClass ) , fromBodyParam . ModelMetadata . ModelType ) ;
336
341
Assert . Equal ( BindingSource . Body , fromBodyParam . Source ) ;
342
+ Assert . True ( fromBodyParam . IsRequired ) ;
337
343
}
338
344
345
+ [ Fact ]
346
+ public void TestParameterIsRequired ( )
347
+ {
348
+ var apiDescription = GetApiDescription ( ( [ FromRoute ] int foo , int ? bar ) => { } ) ;
349
+ Assert . Equal ( 2 , apiDescription . ParameterDescriptions . Count ) ;
350
+
351
+ var fooParam = apiDescription . ParameterDescriptions [ 0 ] ;
352
+ Assert . Equal ( typeof ( int ) , fooParam . Type ) ;
353
+ Assert . Equal ( typeof ( int ) , fooParam . ModelMetadata . ModelType ) ;
354
+ Assert . Equal ( BindingSource . Path , fooParam . Source ) ;
355
+ Assert . True ( fooParam . IsRequired ) ;
356
+
357
+ var barParam = apiDescription . ParameterDescriptions [ 1 ] ;
358
+ Assert . Equal ( typeof ( int ? ) , barParam . Type ) ;
359
+ Assert . Equal ( typeof ( int ? ) , barParam . ModelMetadata . ModelType ) ;
360
+ Assert . Equal ( BindingSource . Query , barParam . Source ) ;
361
+ Assert . False ( barParam . IsRequired ) ;
362
+ }
363
+
364
+ #nullable enable
365
+
366
+ [ Fact ]
367
+ public void TestIsRequiredFromBody ( )
368
+ {
369
+ var apiDescription0 = GetApiDescription ( ( [ FromBody ( EmptyBodyBehavior = EmptyBodyBehavior . Allow ) ] InferredJsonClass fromBody ) => { } ) ;
370
+ var apiDescription1 = GetApiDescription ( ( InferredJsonClass ? fromBody ) => { } ) ;
371
+ Assert . Equal ( 1 , apiDescription0 . ParameterDescriptions . Count ) ;
372
+ Assert . Equal ( 1 , apiDescription1 . ParameterDescriptions . Count ) ;
373
+
374
+ var fromBodyParam0 = apiDescription0 . ParameterDescriptions [ 0 ] ;
375
+ Assert . Equal ( typeof ( InferredJsonClass ) , fromBodyParam0 . Type ) ;
376
+ Assert . Equal ( typeof ( InferredJsonClass ) , fromBodyParam0 . ModelMetadata . ModelType ) ;
377
+ Assert . Equal ( BindingSource . Body , fromBodyParam0 . Source ) ;
378
+ Assert . False ( fromBodyParam0 . IsRequired ) ;
379
+
380
+ var fromBodyParam1 = apiDescription1 . ParameterDescriptions [ 0 ] ;
381
+ Assert . Equal ( typeof ( InferredJsonClass ) , fromBodyParam1 . Type ) ;
382
+ Assert . Equal ( typeof ( InferredJsonClass ) , fromBodyParam1 . ModelMetadata . ModelType ) ;
383
+ Assert . Equal ( BindingSource . Body , fromBodyParam1 . Source ) ;
384
+ Assert . False ( fromBodyParam1 . IsRequired ) ;
385
+ }
386
+
387
+ // This is necessary for TestIsRequiredFromBody to pass until https://github.com/dotnet/roslyn/issues/55254 is resolved.
388
+ private object RandomMethod ( ) => throw new NotImplementedException ( ) ;
389
+
390
+ #nullable disable
391
+
339
392
[ Fact ]
340
393
public void AddsDisplayNameFromRouteEndpoint ( )
341
394
{
0 commit comments