-
-
Notifications
You must be signed in to change notification settings - Fork 0
Extensions
Alex Wichmann edited this page Jun 11, 2025
·
2 revisions
document.Extensions = new Dictionary<string, IAsyncApiExtension>
{
["boolean"] = new AsyncApiAny(false),
["string"] = new AsyncApiAny("test"),
["decimal"] = new AsyncApiAny(2.2),
["int"] = new AsyncApiAny(1),
["array"] = new AsyncApiAny(new List<string>()),
["object"] = new AsyncApiAny(new { test = 123 }),
};
Extension parsers enable you to transform extensions, in any way you want. See example below.
asyncapi: 2.3.0
info:
title: test
version: 1.0.0
contact:
name: API Support
url: https://www.example.com/support
email: support@example.com
channels:
workspace:
x-someValue: onetwothreefour
Func<AsyncApiAny, IAsyncApiExtension> valueExtensionParser = (any) =>
{
if (any.TryGetValue<string>(out var value))
{
if (value == "onetwothreefour")
{
return new AsyncApiAny(1234);
}
}
return new AsyncApiAny("No value provided");
};
var settings = new AsyncApiReaderSettings
{
ExtensionParsers = new Dictionary<string, Func<AsyncApiAny, IAsyncApiExtension>>
{
{ "x-someValue", valueExtensionParser },
},
};
var reader = new AsyncApiStringReader(settings);
var doc = reader.Read(yaml, out var diagnostic);
Assert.AreEqual(AsyncApiAny.FromExtensionOrDefault<int>(doc.Channels["workspace"].Extensions["x-someValue"]), 1234); // True