aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Incorrect nullability on IApiRequestFormatMetadataProvider.GetSupportedContentTypes

Open bkoelman opened this issue 5 months ago • 1 comments

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?

Image

Image

bkoelman avatar Jun 18 '25 21:06 bkoelman

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.

bkoelman avatar Jun 18 '25 21:06 bkoelman

Method IApiResponseTypeMetadataProvider.GetSupportedContentTypes has the same problem. Its contentType parameter should also be nullable.

bkoelman avatar Jun 29 '25 00:06 bkoelman

@bkoelman Happy to review a PR to update the nullability annotations and react to them here.

captainsafia avatar Jul 23 '25 04:07 captainsafia

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"?

bkoelman avatar Jul 24 '25 16:07 bkoelman

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