Skip to content

Commit a9bf6f9

Browse files
authored
Update optionality check in API descriptor
1 parent 676fd2f commit a9bf6f9

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/Http/Http.Extensions/src/RequestDelegateFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext
267267
// when RDF.Create is manually invoked.
268268
if (factoryContext.RouteParameters is { } routeParams)
269269
{
270-
271270
if (routeParams.Contains(parameter.Name, StringComparer.OrdinalIgnoreCase))
272271
{
273272
// We're in the fallback case and we have a parameter and route parameter match so don't fallback

src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint, string
143143

144144
// Determine the "requiredness" based on nullability, default value or if allowEmpty is set
145145
var nullability = NullabilityContext.Create(parameter);
146-
var isOptional = parameter.HasDefaultValue || nullability.ReadState == NullabilityState.Nullable || allowEmpty;
146+
var isOptional = parameter.HasDefaultValue || nullability.ReadState != NullabilityState.NotNull || allowEmpty;
147147

148148
return new ApiParameterDescription
149149
{

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,27 @@ public void AddsMetadataFromRouteEndpoint()
412412
Assert.True(apiExplorerSettings.IgnoreApi);
413413
}
414414

415+
[Fact]
416+
public void TestParameterIsRequiredForObliviousNullabilityContext()
417+
{
418+
// In an oblivious nullability context, reference type parameters without
419+
// annotations are optional. Value type parameters are always required.
420+
var apiDescription = GetApiDescription((string foo, int bar) => { });
421+
Assert.Equal(2, apiDescription.ParameterDescriptions.Count);
422+
423+
var fooParam = apiDescription.ParameterDescriptions[0];
424+
Assert.Equal(typeof(string), fooParam.Type);
425+
Assert.Equal(typeof(string), fooParam.ModelMetadata.ModelType);
426+
Assert.Equal(BindingSource.Query, fooParam.Source);
427+
Assert.False(fooParam.IsRequired);
428+
429+
var barParam = apiDescription.ParameterDescriptions[1];
430+
Assert.Equal(typeof(int), barParam.Type);
431+
Assert.Equal(typeof(int), barParam.ModelMetadata.ModelType);
432+
Assert.Equal(BindingSource.Query, barParam.Source);
433+
Assert.True(barParam.IsRequired);
434+
}
435+
415436
[Fact]
416437
public void RespectsProducesProblemExtensionMethod()
417438
{

0 commit comments

Comments
 (0)