-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Description
Is there an existing issue for this?
- I have searched the existing issuesTo pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
Describe the bug
The contentType
parameter in Microsoft.AspNetCore.Mvc.ApiExplorer.IApiRequestFormatMetadataProvider.GetSupportedContentTypes
is annotated as non-nullable. However, null
is explicitly being passed from DefaultApiDescriptionProvider.GetSupportedFormats
here.
This causes crashes in our custom implementation of IApiRequestFormatMetadataProvider.GetSupportedContentTypes
, which doesn't expect a null
content type.
Please correct the nullability of this parameter, and potentially related places where applicable.
Expected Behavior
The contentType
parameter to be annotated as nullable.
Steps To Reproduce
Register a custom implementation of IInputFormatter
with the following implementation:
internal sealed class ExampleApiRequestFormatMetadataProvider : IInputFormatter, IApiRequestFormatMetadataProvider
{
public bool CanRead(InputFormatterContext context)
{
return false;
}
public Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
{
throw new UnreachableException();
}
public IReadOnlyList<string> GetSupportedContentTypes(string contentType, Type objectType)
{
ArgumentNullException.ThrowIfNull(contentType); // <--- crash here
ArgumentNullException.ThrowIfNull(objectType);
return [];
}
}
Load the OpenAPI document using Swashbuckle, for the following API controller:
[Route("[controller]")]
public sealed class ExampleController : ControllerBase
{
[HttpPut]
public IActionResult Put([FromBody] string name)
{
string result = $"Hi, {name}";
return Ok(result);
}
}
Exceptions (if any)
No response
.NET Version
.NET 8 and 9 on Windows / Visual Studio
Activity
bkoelman commentedon Jun 18, 2025
Jumping to the interface definition, it's actually documented as such:
bkoelman commentedon Jun 29, 2025
Method
IApiResponseTypeMetadataProvider.GetSupportedContentTypes
has the same problem. ItscontentType
parameter should also be nullable.captainsafia commentedon Jul 23, 2025
@bkoelman Happy to review a PR to update the nullability annotations and react to them here.
bkoelman commentedon Jul 24, 2025
Thanks. I'll try to find some time, though I wouldn't mind if someone else picked this up. Perhaps label with "help wanted" and "good first issue"?
dotnet-policy-service commentedon Jul 24, 2025
Looks like this issue has been identified as a candidate for community contribution. If you're considering sending a PR for this issue, look for the
Summary Comment
link in the issue description. That comment has been left by an engineer on our team to help you get started with handling this issue. You can learn more about our Help Wanted process here