Skip to content

Reading

Alex Wichmann edited this page Feb 23, 2023 · 15 revisions

The AsyncApiReader is a component in your .NET library that can be used to deserialize AsyncAPI specifications from either JSON or YAML formats. The deserialized AsyncAPI document can then be manipulated in memory as a C# object.

Basic usage

To use any of the AsyncApiReaders, you first need to create an instance.

There are 3 types of readers, which all have the same API

  1. AsyncApiStringReader
  2. AsyncApiStreamReader
  3. AsyncApiTextReader

To use any of the AsyncApiReaders, you first need to create an instance. 

``` csharp
var reader = new AsyncApiStringReader();
var doc = reader.Read(yaml, out var diagnostic);

You can override default reader settings by passing in an AsyncApiReaderSettings object.

or overriding settings

var settings = new AsyncApiReaderSettings();
var reader = new AsyncApiStringReader(settings);

Settings

The settings object holds a few different settings that can be applied.

ReferenceResolution

You can use ReferenceResolutionSetting.DoNotResolveReferences to enforce not resolving references during deserialization. For more information see Reference Resolution

Extension parsers

Extension parsers enable you to transform extensions, in any way you want. See example below.

Func<IAsyncApiAny, IAsyncApiExtension> isRequiredExtensionParser = (any) => 
{
    if (any.AnyType == AnyType.Object && any is AsyncApiObject value)
    {
        var hasValue = (value["hasValue"] as AsyncApiBoolean).Value;
        if (hasValue)
        {
        return new AsyncApiString(((AsyncApiString)value["myValue"]).Value);

        }
    }
    
    return new AsyncApiString("No value provided");
};

var settings = new AsyncApiReaderSettings
{
    ExtensionParsers = new Dictionary<string, Func<IAsyncApiAny, IAsyncApiExtension>>
    {
        { "x-isrequired", isRequiredExtensionParser }
    }
};
Clone this wiki locally