Repository: dotnet/aspnetcore
File: src/OpenApi/src/Extensions/OpenApiEndpointRouteBuilderExtensions.cs (C#)

Code before:
                // The GetRequiredKeyedService() method is case-sensitive, which doesn't work well for OpenAPI document names here,
                // as the document name is also used as the route to retrieve the document, so we need to ensure this is lowercased to achieve consistency with ASP.NET Core routing.
                // The same goes for the document options below, which is also case-sensitive, and thus we need to pass in a case-insensitive document name.
                // See OpenApiServiceCollectionExtensions.cs for more info.
                var lowercasedDocumentName = documentName.ToLowerInvariant();

                // It would be ideal to use the `HttpResponseStreamWriter` to
                // asynchronously write to the response stream here but Microsoft.OpenApi
                // does not yet support async APIs on their writers.
                // See https://github.com/microsoft/OpenAPI.NET/issues/421 for more info.
                var documentService = context.RequestServices.GetKeyedService<OpenApiDocumentService>(lowercasedDocumentName);
                if (documentService is null)
                {
                    context.Response.StatusCode = StatusCodes.Status404NotFound;
                    context.Response.ContentType = "text/plain;charset=utf-8";
                    await context.Response.WriteAsync($"No OpenAPI document with the name '{lowercasedDocumentName}' was found.");
                }
                else
                {
                    var document = await documentService.GetOpenApiDocumentAsync(context.RequestServices, context.Request, context.RequestAborted);
                    var documentOptions = options.Get(lowercasedDocumentName);

                    using var writer = new Utf8BufferTextWriter();
                    writer.SetWriter(context.Response.BodyWriter);

                    if (UseYaml(pattern))
                    {
                        context.Response.ContentType = "text/plain+yaml;charset=utf-8";
                        await context.Response.StartAsync();
                        await document.SerializeAsync(new OpenApiYamlWriter(writer), documentOptions.OpenApiVersion, context.RequestAborted);
                    }
                    else
                    {
                        context.Response.ContentType = "application/json;charset=utf-8";
                        await context.Response.StartAsync();
                        await document.SerializeAsync(new OpenApiJsonWriter(writer), documentOptions.OpenApiVersion, context.RequestAborted);
                    }
                    await context.Response.BodyWriter.FlushAsync(context.RequestAborted);
                }
            }).ExcludeFromDescription();
    }

    private static bool UseYaml(string pattern) =>
        pattern.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
        pattern.EndsWith(".yml", StringComparison.OrdinalIgnoreCase);
}


Review comment (refactor):
[nitpick] It would be beneficial to add a brief inline comment explaining the reason for deferring the call to context.Response.StartAsync() until after setting contentType and openApiWriter, to help future maintainers understand the refactored flow.

Code after:
                // The GetRequiredKeyedService() method is case-sensitive, which doesn't work well for OpenAPI document names here,
                // as the document name is also used as the route to retrieve the document, so we need to ensure this is lowercased to achieve consistency with ASP.NET Core routing.
                // The same goes for the document options below, which is also case-sensitive, and thus we need to pass in a case-insensitive document name.
                // See OpenApiServiceCollectionExtensions.cs for more info.
                var lowercasedDocumentName = documentName.ToLowerInvariant();

                // It would be ideal to use the `HttpResponseStreamWriter` to
                // asynchronously write to the response stream here but Microsoft.OpenApi
                // does not yet support async APIs on their writers.
                // See https://github.com/microsoft/OpenAPI.NET/issues/421 for more info.
                var documentService = context.RequestServices.GetKeyedService<OpenApiDocumentService>(lowercasedDocumentName);
                if (documentService is null)
                {
                    context.Response.StatusCode = StatusCodes.Status404NotFound;
                    context.Response.ContentType = "text/plain;charset=utf-8";
                    await context.Response.WriteAsync($"No OpenAPI document with the name '{lowercasedDocumentName}' was found.");
                }
                else
                {
                    var document = await documentService.GetOpenApiDocumentAsync(context.RequestServices, context.Request, context.RequestAborted);
                    var documentOptions = options.Get(lowercasedDocumentName);

                    using var textWriter = new Utf8BufferTextWriter();
                    textWriter.SetWriter(context.Response.BodyWriter);

                    string contentType;
                    OpenApiWriterBase openApiWriter;

                    if (UseYaml(pattern))
                    {
                        contentType = "text/plain+yaml;charset=utf-8";
                        openApiWriter = new OpenApiYamlWriter(textWriter);
                    }
                    else
                    {
                        contentType = "application/json;charset=utf-8";
                        openApiWriter = new OpenApiJsonWriter(textWriter);
                    }

                    context.Response.ContentType = contentType;

                    await context.Response.StartAsync();
                    await document.SerializeAsync(openApiWriter, documentOptions.OpenApiVersion, context.RequestAborted);
                    await context.Response.BodyWriter.FlushAsync(context.RequestAborted);
                }
            }).ExcludeFromDescription();
    }

    private static bool UseYaml(string pattern) =>
        pattern.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
        pattern.EndsWith(".yml", StringComparison.OrdinalIgnoreCase);