Skip to content

Incorrect nullability on IApiRequestFormatMetadataProvider.GetSupportedContentTypes #62405

Open
@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions