asyncapicloudevents

How to define a message fixed field value in AsyncAPI


We are looking at using CloudEvents as a message envelope for events generated in our system, and defining those messages using AsyncAPI.

We are using the CloudEvents type property to identify each event type, with a different data property schema for each type. Whilst I have worked out how to define the individual event structures using AsyncAPI, I can't identify how to set the type property to the value required for that event type.

What would seem a mis-use of the enum keyword is the only way so far that I've been able to associated the event type to the property, as shown in the example below. Is there an accepted pattern for achieving the definition of these fixed properties within the AsyncAPI specification?

channels:
  user:
    subscribe:
      message:
        payload:
          $ref: '#/components/schemas/firstEventPayload'
components:
  schemas:
    firstEventPayload:
      type: object
      allOf:
        - $ref: 'https://raw.githubusercontent.com/cloudevents/spec/v1.0.1/spec.json'
      properties:
        type:
          type: string
          enum: [test.cloud.event.new-user]
        data:
          type: object
          properties:
            userId:
              type: string
              format: uuid
            email:
              type: string
              format: email

Credit to developers.redhat.com for the cloudevent/spec $ref


Solution

  • In JSON Schema (AsyncAPI Schema is a superset of it) you can also use const like:

            type:
              type: string
              const: test.cloud.event.new-user