Table of Contents

Class YamlConverterExtensions

Namespace
Codebelt.Extensions.AspNetCore.Text.Yaml.Converters
Assembly
Codebelt.Extensions.AspNetCore.Text.Yaml.dll

Extension methods for the YamlConverter class.

public static class YamlConverterExtensions
Inheritance
YamlConverterExtensions

Examples

The following example demonstrates how to register converters for ASP.NET Core exception types with a YAML formatter.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Codebelt.Extensions.AspNetCore.Text.Yaml.Converters;
using Codebelt.Extensions.YamlDotNet;
using Codebelt.Extensions.YamlDotNet.Converters;
using Codebelt.Extensions.YamlDotNet.Formatters;
using Cuemon.Diagnostics;
using Microsoft.AspNetCore.Mvc;

namespace ExampleNamespace;

class Program
{
    static void Main()
    {
        var converters = new List<YamlConverter>();
        
        // Register the ProblemDetails converter
        converters.AddProblemDetailsConverter();
        
        // Register the HttpExceptionDescriptor converter with custom sensitivity settings
        converters.AddHttpExceptionDescriptorConverter(options =>
        {
            options.SensitivityDetails = FaultSensitivityDetails.StackTrace | FaultSensitivityDetails.Data;
        });
        
        var formatter = new YamlFormatter(new YamlFormatterOptions { Settings = new YamlSerializerOptions { Converters = converters } });
        
        var problemDetails = new ProblemDetails
        {
            Type = "https://example.com/errors/validation",
            Title = "Validation Error",
            Status = 400,
            Detail = "One or more validation errors occurred."
        };
        
        var yaml = formatter.Serialize(problemDetails, typeof(ProblemDetails));
        using (var reader = new StreamReader(yaml))
        {
            Console.WriteLine(reader.ReadToEnd());
        }
    }
}

Methods

AddHttpExceptionDescriptorConverter(ICollection<YamlConverter>, Action<ExceptionDescriptorOptions>)

Adds an HttpExceptionDescriptor YAML converter to the list.

public static ICollection<YamlConverter> AddHttpExceptionDescriptorConverter(this ICollection<YamlConverter> converters, Action<ExceptionDescriptorOptions> setup = null)

Parameters

converters ICollection<YamlConverter>

The ICollection{YamlConverter} to extend.

setup Action<ExceptionDescriptorOptions>

The ExceptionDescriptorOptions which may be configured.

Returns

ICollection<YamlConverter>

A reference to converters after the operation has completed.

AddProblemDetailsConverter(ICollection<YamlConverter>)

Adds a ProblemDetails YAML converter to the collection.

public static ICollection<YamlConverter> AddProblemDetailsConverter(this ICollection<YamlConverter> converters)

Parameters

converters ICollection<YamlConverter>

The collection of YamlConverter to extend.

Returns

ICollection<YamlConverter>

A reference to converters so that additional calls can be chained.