Skip to content

With OpenApiDocumentTransformerContext modified, the openapi document requested for the first time is incorrect. #63473

@oaly2000

Description

@oaly2000

.NET 9 & .NET 10 have the same behavior.

repro:

using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.ModelBinding;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenApi(options => options.AddDocumentTransformer((document, context, cancellationToken) =>
{
    EmptyModelMetadataProvider provider = new();

    foreach (var group in context.DescriptionGroups)
    {
        foreach (var item in group.Items)
        {
            foreach (var responseType in item.SupportedResponseTypes)
            {
                if (responseType.Type == typeof(Wrapped))
                {
                    responseType.Type = typeof(string);
                    responseType.ApiResponseFormats = [new ApiResponseFormat { MediaType = "text/plain" }];
                }
            }
        }
    }

    return Task.CompletedTask;
}));

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
}

app.MapGet("/", () => new Wrapped("Hello, World!")).AddEndpointFilter<EndpointFilter>();

app.Run();

record Wrapped(string Value);

class EndpointFilter : IEndpointFilter
{
    public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
    {
        var v = await next.Invoke(context);
        return v is Wrapped wrapped ? wrapped.Value : v;
    }
}

the first time:

        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Wrapped"
                }
              }
            }
          }

the rest:

        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }

Is this a bug or is my usage incorrect ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions