Skip to content

[API Proposal]: Add MediaTypeNames.Application.Yaml #105809

@martincostello

Description

@martincostello

Background and motivation

RFC 9512 registered application/yaml as the media type for YAML in February 2024.

It would be useful if a constant for it were added to the MediaTypeNames.Application class alongside other similar constant values, such as for JSON and XML.

application/gzip was recently added by #103575 to resolve #95446 so I assume it's not locked-down for additions like similar types in ASP.NET Core are.

API Proposal

namespace System.Net.Mime;

public static partial class MediaTypeNames
{
    public static partial class Application
    {
+       /// <summary>Specifies that the <see cref="MediaTypeNames.Application"/> data is in YAML format.</summary>
+       public const string Yaml = "application/yaml";
    }
}

API Usage

public async Task WriteDocumentAsync(
    OpenApiDocument document,
    OpenApiSpecVersion version,
    HttpContext httpContext)
{
    using var stream = new MemoryStream();
    using var streamWriter = new StreamWriter(stream);
    var yamlWriter = new OpenApiYamlWriter(streamWriter);

    document.Serialize(yamlWriter, version);

    httpContext.Response.ContentType = MediaTypeNames.Application.Yaml;

    await httpContext.Response.BodyWriter.WriteAsync(stream.ToArray(), httpContext.RequestAborted);
    await httpContext.Response.BodyWriter.FlushAsync(httpContext.RequestAborted);
}

Alternative Designs

None.

Risks

None.

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