Skip to content

Commit 2c72d3d

Browse files
committed
nullable
1 parent bcbc0c8 commit 2c72d3d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ private static ParameterDescriptor CreateParameterDescriptor(ParameterInfo param
218218
else if (parameter.ParameterType == typeof(string) || ParameterBindingMethodCache.HasTryParseMethod(parameter))
219219
{
220220
// complex types will display as strings since they use custom parsing via TryParse on a string
221-
var displayType = !parameter.ParameterType.IsPrimitive ? typeof(string) : parameter.ParameterType;
221+
var displayType = !parameter.ParameterType.IsPrimitive && Nullable.GetUnderlyingType(parameter.ParameterType)?.IsPrimitive != true
222+
? typeof(string) : parameter.ParameterType;
222223
// Path vs query cannot be determined by RequestDelegateFactory at startup currently because of the layering, but can be done here.
223224
if (parameter.Name is { } name && pattern.GetParameter(name) is not null)
224225
{

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ static void AssertPathParameter(ApiDescription apiDescription)
285285
}
286286

287287
[Fact]
288-
public void AddsFromRouteParameterAsPathWithPrimitiveTypeWithTryParse()
288+
public void AddsFromRouteParameterAsPathWithPrimitiveType()
289289
{
290290
static void AssertPathParameter(ApiDescription apiDescription)
291291
{
@@ -298,6 +298,20 @@ static void AssertPathParameter(ApiDescription apiDescription)
298298
AssertPathParameter(GetApiDescription((int foo) => { }, "/{foo}"));
299299
}
300300

301+
[Fact]
302+
public void AddsFromRouteParameterAsPathWithNullablePrimitiveType()
303+
{
304+
static void AssertPathParameter(ApiDescription apiDescription)
305+
{
306+
var param = Assert.Single(apiDescription.ParameterDescriptions);
307+
Assert.Equal(typeof(int?), param.Type);
308+
Assert.Equal(typeof(int?), param.ModelMetadata.ModelType);
309+
Assert.Equal(BindingSource.Path, param.Source);
310+
}
311+
312+
AssertPathParameter(GetApiDescription((int? foo) => { }, "/{foo}"));
313+
}
314+
301315
[Fact]
302316
public void AddsFromRouteParameterAsPathWithStructTypeWithTryParse()
303317
{

0 commit comments

Comments
 (0)