Skip to content

MapOpenApi() Response.ContentType=null causes garbled characters in Chinese and other languages #56095

@vipwan

Description

@vipwan

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

image
@captainsafia ,

https://github.com/dotnet/aspnetcore/blob/main/src/OpenApi/src/Extensions/OpenApiEndpointRouteBuilderExtensions.cs
There doesn't seem to be set ContentType to the doc returned
I solved this issue temporarily using middleware, and of course it would be nice to fix this internally

internal class OpenApiDocMiddleware
{
    private readonly RequestDelegate _next;
    public OpenApiDocMiddleware(RequestDelegate next)
    {
        _next = next;
    }
    public async Task Invoke(HttpContext context)
    {
        if (context.GetEndpoint()?.Metadata.GetMetadata<IRouteDiagnosticsMetadata>() is { } route &&
            route.Route.EndsWith("{documentName}.json"))
        {
            context.Response.ContentType = "application/json";
        }
        await _next(context);
    }
}

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

9.0.0-preview.4.24267.6

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions