Skip to content

Incorrect nullability on IApiRequestFormatMetadataProvider.GetSupportedContentTypes #62405

@bkoelman

Description

@bkoelman

Is there an existing issue for this?

  • 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

Activity

added
area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates
on Jun 18, 2025
bkoelman

bkoelman commented on Jun 18, 2025

@bkoelman
Author

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

bkoelman commented on Jun 29, 2025

@bkoelman
Author

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

captainsafia

captainsafia commented on Jul 23, 2025

@captainsafia
Member

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

added this to the Backlog milestone on Jul 23, 2025
bkoelman

bkoelman commented on Jul 24, 2025

@bkoelman
Author

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

dotnet-policy-service commented on Jul 24, 2025

@dotnet-policy-service
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapigood first issueGood for newcomers.help wantedUp for grabs. We would accept a PR to help resolve this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @martincostello@captainsafia@bkoelman

        Issue actions

          Incorrect nullability on IApiRequestFormatMetadataProvider.GetSupportedContentTypes · Issue #62405 · dotnet/aspnetcore