Codebelt

Codebelt.Extensions.YamlDotNet.App

A metadata-only convenience package for the full Codebelt YamlDotNet stack in .NET applications.

.NET 10.0 / .NET 9.0 MIT v10.1.4 8,924 downloads

Overview

Codebelt.Extensions.YamlDotNet.App is a metadata-only convenience package for applications that want the repository's YAML stack through one package reference. Its project file references Codebelt.Extensions.YamlDotNet, Codebelt.Extensions.AspNetCore.Text.Yaml, and Codebelt.Extensions.AspNetCore.Mvc.Formatters.Text.Yaml, so installation brings in the core formatter plus the ASP.NET Core and MVC integration points.

The package does not ship public types of its own. Consumer-facing APIs come from the referenced packages, which cover object serialization, exception-response formatting, and MVC input and output formatters.

Key APIs

Codebelt.Extensions.YamlDotNet.App does not add public types or extension methods. The consumer APIs come from Codebelt.Extensions.YamlDotNet such as YamlFormatter, Codebelt.Extensions.AspNetCore.Text.Yaml such as AddMinimalYamlOptions and AddYamlExceptionResponseFormatter, and Codebelt.Extensions.AspNetCore.Mvc.Formatters.Text.Yaml such as AddYamlFormatters.

Basic usage

Codebelt.Extensions.AspNetCore.Mvc.Formatters.Text.Yaml

using System.Linq;
using Codebelt.Extensions.AspNetCore.Mvc.Formatters.Text.Yaml;
using Codebelt.Extensions.Xunit;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Xunit;

namespace Contoso.Api.Tests;

public class WebApiYamlFormattersTest : Test
{
    public WebApiYamlFormattersTest(ITestOutputHelper output) : base(output)
    {
    }

    [Fact]
    public void AddYamlFormatters_ShouldRegisterMvcOptionsSetup()
    {
        var services = new ServiceCollection();

        services.AddMvcCore()
            .AddYamlFormatters();

        var yamlSetupCount = services.Count(sd =>
            sd.ServiceType == typeof(IConfigureOptions<MvcOptions>) &&
            sd.ImplementationType == typeof(YamlSerializationMvcOptionsSetup));

        TestOutput.WriteLine($"YamlSerializationMvcOptionsSetup registrations: {yamlSetupCount}");

        Assert.Equal(1, yamlSetupCount);
    }
}

Codebelt.Extensions.AspNetCore.Text.Yaml

using System.Linq;
using Codebelt.Extensions.AspNetCore.Text.Yaml;
using Codebelt.Extensions.Xunit;
using Codebelt.Extensions.YamlDotNet.Formatters;
using Cuemon.AspNetCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Contoso.Api.Tests;

public class MinimalYamlErrorsTest : Test
{
    public MinimalYamlErrorsTest(ITestOutputHelper output) : base(output)
    {
    }

    [Fact]
    public void AddMinimalYamlOptions_ShouldRegisterYamlExceptionFormatter()
    {
        var services = new ServiceCollection();
        services.AddOptions();
        services.AddMinimalYamlOptions();

        var formatterCount = services.Count(sd =>
            sd.ServiceType == typeof(HttpExceptionDescriptorResponseFormatter<YamlFormatterOptions>));

        TestOutput.WriteLine($"YAML exception formatter registrations: {formatterCount}");

        Assert.Equal(1, formatterCount);
    }
}

Codebelt.Extensions.YamlDotNet

using System.IO;
using System.Text;
using Codebelt.Extensions.Xunit;
using Codebelt.Extensions.YamlDotNet.Formatters;
using Xunit;

namespace Contoso.Serialization.Tests;

public class DeploymentManifestYamlTest : Test
{
    public DeploymentManifestYamlTest(ITestOutputHelper output) : base(output)
    {
    }

    [Fact]
    public void SerializeObject_ShouldRoundtripManifest()
    {
        var manifest = new DeploymentManifest
        {
            Application = "billing",
            ReplicaCount = 3
        };

        using var stream = YamlFormatter.SerializeObject(manifest);
        using var reader = new StreamReader(stream, Encoding.UTF8);
        var yaml = reader.ReadToEnd();
        TestOutput.WriteLine(yaml);

        using var input = new MemoryStream(Encoding.UTF8.GetBytes(yaml));
        var roundtripped = YamlFormatter.DeserializeObject<DeploymentManifest>(input);

        Assert.Contains("application: billing", yaml);
        Assert.Equal("billing", roundtripped.Application);
        Assert.Equal(3, roundtripped.ReplicaCount);
    }

    private sealed class DeploymentManifest
    {
        public string Application { get; set; } = "";

        public int ReplicaCount { get; set; }
    }
}

This package gives you one NuGet reference, but the APIs shown above are implemented by the referenced packages rather than by Codebelt.Extensions.YamlDotNet.App itself.

Installation

dotnet add package Codebelt.Extensions.YamlDotNet.App

Usage guidance

Choose Codebelt.Extensions.YamlDotNet.App when an application needs the full set of YAML integrations from this repository: the core formatter APIs, ASP.NET Core exception-response formatting, and MVC YAML formatters. If you only need serialization or one web integration surface, reference the narrower package directly so the dependency list matches the feature set you actually use.

Family packages