Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenApi: The IEndpointParameterMetadataProvider is not used for Controllers #58178

Closed
1 task done
DavidBal opened this issue Oct 1, 2024 · 1 comment
Closed
1 task done
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi

Comments

@DavidBal
Copy link

DavidBal commented Oct 1, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I want to generate a OpenApi json for my Controller based Api.

I have the following controller Code:

[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
    [HttpPost("test")]
    public async Task<IActionResult> PostTest([FromBody] XmlPlaceholder? xmlPlaceholder = null)
    {
        return Ok();
    }
}

The XmlPlaceholder class looks like this:

public class XmlPlaceholder : IEndpointParameterMetadataProvider
{
    public static void PopulateMetadata(ParameterInfo parameter, EndpointBuilder builder)
    {
        builder.Metadata.Add(new AcceptsMetadata(["application/xml", "text/xml"], typeof(XmlPlaceholder)));
    }
}

The generated OpenAPI json looks like this:

 "/api/Test/test": {
      "post": {
        "tags": [
          "Test"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/XmlPlaceholder"
              }
            },
            "text/json": {
              "schema": {
                "$ref": "#/components/schemas/XmlPlaceholder"
              }
            },
            "application/*+json": {
              "schema": {
                "$ref": "#/components/schemas/XmlPlaceholder"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }

Expected Behavior

I would expect the same behavior as for minimal apis.

There the metadata is used and produces the following output:

    "/minimalTest": {
      "post": {
        "tags": [
          "WebApplication2"
        ],
        "operationId": "Minimal Test",
        "requestBody": {
          "content": {
            "application/xml": {
              "schema": {
                "$ref": "#/components/schemas/XmlPlaceholder"
              }
            },
            "text/xml": {
              "schema": {
                "$ref": "#/components/schemas/XmlPlaceholder"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },

Steps To Reproduce

https://github.com/DavidBal/-Demo_IEndpointParameterMetadataProvider_for_Controller/tree/master

Exceptions (if any)

No response

.NET Version

9.0.100-rc.1.24452.12

Anything else?

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0-rc.1.24452.1" />
  </ItemGroup>

</Project>
@dotnet-issue-labeler dotnet-issue-labeler bot added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Oct 1, 2024
@BrennanConroy BrennanConroy added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Oct 1, 2024
@captainsafia
Copy link
Member

@DavidBal Thanks for reporting this issue! I believe this is a dupe of #44988. If you'd like to give that issue a thumbs up and add a comment with your scenario there that would help keep all the information in one place.

For now, closing this as a dupe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi
Projects
None yet
Development

No branches or pull requests

4 participants