Skip to content

Message Payloads

Alex Wichmann edited this page Jun 3, 2024 · 4 revisions

AsyncAPI supports 2 types of message payloads

  1. Schema Object
  2. Avro 1.9.0

Schema Object

Usage

new AsyncApiSchema()
{
    Title = "title1",
    AllOf = new List<AsyncApiSchema>
    {
        new AsyncApiSchema
        {
            Title = "title2",
            Properties = new Dictionary<string, AsyncApiSchema>
            {
                ["property1"] = new AsyncApiSchema
                {
                    Type = SchemaType.Integer,
                },
                ["property2"] = new AsyncApiSchema
                {
                    Type = SchemaType.String,
                    MaxLength = 15,
                },
            },
        },
        new AsyncApiSchema
        {
            Title = "title3",
            Properties = new Dictionary<string, AsyncApiSchema>
            {
                ["property3"] = new AsyncApiSchema
                {
                    Properties = new Dictionary<string, AsyncApiSchema>
                    {
                        ["property4"] = new AsyncApiSchema
                        {
                            Type = SchemaType.Boolean ,
                        },
                    },
                },
                ["property5"] = new AsyncApiSchema
                {
                    Type = SchemaType.String,
                    MinLength = 2,
                },
            },
            Nullable = true,
        },
    },
    Nullable = true,
    ExternalDocs = new AsyncApiExternalDocumentation
    {
        Url = new Uri("http://example.com/externalDocs"),
    },
};

Avro

The Avro types are implemented through a common base class AvroSchema As avro supports adding custom properties to the schemas as "metadata", these when deserialized, will be added to the Metadata Dictionary which exists on all implemented Avro types

Supported types:

  • Record
  • Fixed
  • Enum
  • Union
  • Map
  • Array
  • Primitive
  • Field Note, all above types are prefixed "Avro" within the dotnet classes.

Usage

new AvroRecord
{
    Name = "User",
    Namespace = "com.example",
    Fields = new List<AvroField>
    {
        new AvroField
        {
            Name = "username",
            Type = AvroPrimitiveType.String,
            Doc = "The username of the user.",
            Default = new AsyncApiAny("guest"),
            Order = AvroFieldOrder.Ascending,
        },
        new AvroField
        {
            Name = "status",
            Type = new AvroEnum
            {
                Name = "Status",
                Symbols = new List<string> { "ACTIVE", "INACTIVE", "BANNED" },
            },
            Doc = "The status of the user.",
        },
        new AvroField
        {
            Name = "emails",
            Type = new AvroArray
            {
                Items = AvroPrimitiveType.String,
            },
            Doc = "A list of email addresses.",
        },
        new AvroField
        {
            Name = "metadata",
            Type = new AvroMap
            {
                Values = AvroPrimitiveType.String,
            },
            Doc = "Metadata associated with the user.",
        },
        new AvroField
        {
            Name = "address",
            Type = new AvroRecord
            {
                Name = "Address",
                Fields = new List<AvroField>
                {
                    new AvroField { Name = "street", Type = AvroPrimitiveType.String },
                    new AvroField { Name = "city", Type = AvroPrimitiveType.String },
                    new AvroField { Name = "zipcode", Type = AvroPrimitiveType.String },
                },
            },
            Doc = "The address of the user.",
        },
        new AvroField
        {
            Name = "profilePicture",
            Type = new AvroFixed
            {
                Name = "ProfilePicture",
                Size = 256,
            },
            Doc = "A fixed-size profile picture.",
        },
        new AvroField
        {
            Name = "contact",
            Type = new AvroUnion
            {
                Types = new List<AvroSchema>
                {
                    AvroPrimitiveType.Null,
                    new AvroRecord
                    {
                        Name = "PhoneNumber",
                        Fields = new List<AvroField>
                        {
                            new AvroField { Name = "countryCode", Type = AvroPrimitiveType.Int },
                            new AvroField { Name = "number", Type = AvroPrimitiveType.String },
                        },
                    },
                },
            },
            Doc = "The contact information of the user, which can be either null or a phone number.",
        },
    },
};
Clone this wiki locally