Skip to content

Instantly share code, notes, and snippets.

@VisualBean
Created April 7, 2023 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VisualBean/13ac0f0f94250f8ec7ef1042078039a1 to your computer and use it in GitHub Desktop.
Save VisualBean/13ac0f0f94250f8ec7ef1042078039a1 to your computer and use it in GitHub Desktop.
using LEGO.AsyncAPI;
using LEGO.AsyncAPI.Models;
// Build the document
var document = new AsyncApiDocument()
{
Info = new AsyncApiInfo
{
Title = "Example",
Version = "0.1.0"
}
};
// Add the channel
document.Channels["user/signedup"] = new AsyncApiChannel()
{
Subscribe = new AsyncApiOperation()
{
Message = new List<AsyncApiMessage>()
{
new AsyncApiMessage()
{
Description = "An event describing that a user just signed up.",
// The payload consists of one or more layers of JsonSchema why it can look a bit daunting
Payload = new AsyncApiSchema()
{
Type = new List<SchemaType>()
{
SchemaType.Object,
},
Properties = new Dictionary<string, AsyncApiSchema>()
{
{
"fullName", new AsyncApiSchema()
{
Type = new List<SchemaType>()
{
SchemaType.String,
}
}
},
{
"email", new AsyncApiSchema()
{
Type = new List<SchemaType>()
{
SchemaType.String
},
Format = "email",
}
},
{
"age", new AsyncApiSchema()
{
Type = new List<SchemaType>()
{
SchemaType.Integer,
},
Minimum = 18,
}
}
}
}
}
}
}
};
// Serialize the document to yaml
var serialized = document.Serialize(AsyncApiVersion.AsyncApi2_0, AsyncApiFormat.Yaml);
Console.WriteLine(serialized);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment