aspnetcore
aspnetcore copied to clipboard
Incorrect nullability on IApiRequestFormatMetadataProvider.GetSupportedContentTypes
Is there an existing issue for this?
- [x] I have searched the existing issues
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
Anything else?
Jumping to the interface definition, it's actually documented as such:
/// <param name="contentType">
/// The content type for which the supported content types are desired, or <c>null</c> if any content
/// type can be used.
Method IApiResponseTypeMetadataProvider.GetSupportedContentTypes has the same problem. Its contentType parameter should also be nullable.
@bkoelman Happy to review a PR to update the nullability annotations and react to them here.
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"?
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