Generating AsynchAPI specifications with ChatGPT
Wojciech Bulaty
Helping teams accelerate digital transformation and create automated tests faster with API and system simulation
We are working on an example to demonstrate the capabilities of our tool Traffic Parrot for creating API simulators (mocks) that do not get out of date. We are working on an example that covers communication across multiple protocols (#gRPC #Protobuf, #JSON #GraphQL, #RabbitMQ #Protobuf).
I have never used the new #AsyncAPI specification to describe asynchronous APIs. So, I decided to use it to document how the demo sample RabbitMQ Protobuf API would behave.
I have used #ChatGPT to generate the sample #AsynchAPI service for me!
Me: Create an AsyncApi specification for RabbitMQ service for placing shop orders
ChatGPT: Sure! Here's an example AsyncAPI specification for a RabbitMQ service that allows clients to place shop orders:
asyncapi: '2.0.0
info:
? title: RabbitMQ Shop Orders Service
? version: '1.0.0'
? description: A RabbitMQ service for placing shop orders
servers:
? rabbitmq:
? ? url: amqp://guest:guest@localhost:5672
? ? protocol: amqp
? ? description: RabbitMQ server
channels:
? order_placed:
? ? description: Channel for placing shop orders
? ? subscribe:
? ? ? operationId: placeOrder
? ? ? message:
? ? ? ? name: OrderPlacedMessage
? ? ? ? payload:
? ? ? ? ? type: object
? ? ? ? ? properties:
? ? ? ? ? ? order_id:
? ? ? ? ? ? ? type: string
? ? ? ? ? ? ? description: The ID of the order
? ? ? ? ? ? customer_id:
? ? ? ? ? ? ? type: string
? ? ? ? ? ? ? description: The ID of the customer who placed the order
? ? ? ? ? ? products:
? ? ? ? ? ? ? type: array
? ? ? ? ? ? ? items:
? ? ? ? ? ? ? ? type: object
? ? ? ? ? ? ? ? properties:
? ? ? ? ? ? ? ? ? product_id:
? ? ? ? ? ? ? ? ? ? type: string
? ? ? ? ? ? ? ? ? ? description: The ID of the product being ordered
? ? ? ? ? ? ? ? ? quantity:
? ? ? ? ? ? ? ? ? ? type: integer
? ? ? ? ? ? ? ? ? ? description: The quantity of the product being ordered
? ? ? ? ? ? total:
? ? ? ? ? ? ? type: number
? ? ? ? ? ? ? description: The total cost of the order
? ? ? ? ? ? status:
? ? ? ? ? ? ? type: string
? ? ? ? ? ? ? description: The status of the order
? ? ? ? headers:
? ? ? ? ? type: object
? ? ? ? ? properties:
? ? ? ? ? ? correlation_id:
? ? ? ? ? ? ? type: string
? ? ? ? ? ? ? description: The correlation ID of the request message
? ? ? ? ? ? reply_to:
? ? ? ? ? ? ? type: string
? ? ? ? ? ? ? description: The reply-to queue for the response message
? ? bindings:
? ? ? amqp:
? ? ? ? queue:
? ? ? ? ? name: order_placed
? ? ? ? ? durable: true
? ? ? ? ? autoDelete: false
Not too bad to start with, but I would like to follow a request and response pattern.
领英推荐
Me: I would like a publish endpoint to send the orders to and then a response subscribe endpoint where the order confirmations are sent. Also, the message format is protocol buffers (Protobuf).
ChatGPT: Sure, here's an updated AsyncAPI specification that includes a publish endpoint for sending orders and a synchronous response subscribe endpoint for receiving order confirmations. It also specifies that the message format is Protocol Buffers (Protobuf):
asyncapi: '2.0.0
info:
? title: RabbitMQ Shop Orders Service
? version: '1.0.0'
? description: A RabbitMQ service for placing shop orders
servers:
? rabbitmq:
? ? url: amqp://guest:guest@localhost:5672
? ? protocol: amqp
? ? description: RabbitMQ server
channels:
? order_placed:
? ? description: Channel for placing shop orders
? ? publish:
? ? ? operationId: placeOrder
? ? ? message:
? ? ? ? $ref: '#/components/messages/OrderPlacedMessage'
? ? ? bindings:
? ? ? ? amqp:
? ? ? ? ? exchange:
? ? ? ? ? ? name: order_exchange
? ? ? ? ? ? type: fanout
? order_confirmed:
? ? description: Channel for receiving order confirmations
? ? subscribe:
? ? ? operationId: confirmOrder
? ? ? message:
? ? ? ? $ref: '#/components/messages/OrderConfirmationMessage'
? ? ? bindings:
? ? ? ? amqp:
? ? ? ? ? queue:
? ? ? ? ? ? name: order_confirmed
? ? ? ? ? ? durable: true
? ? ? ? ? ? autoDelete: false
? ? ? ? ? exchange:
? ? ? ? ? ? name: order_exchange
? ? ? ? ? ? type: fanout
? ? ? ? ? routingKey: confirmation
components:
? messages:
? ? OrderPlacedMessage:
? ? ? contentType: application/protobuf
? ? ? payload:
? ? ? ? type: object
? ? ? ? properties:
? ? ? ? ? order_id:
? ? ? ? ? ? type: string
? ? ? ? ? ? description: The ID of the order
? ? ? ? ? customer_id:
? ? ? ? ? ? type: string
? ? ? ? ? ? description: The ID of the customer who placed the order
? ? ? ? ? products:
? ? ? ? ? ? type: array
? ? ? ? ? ? items:
? ? ? ? ? ? ? type: object
? ? ? ? ? ? ? properties:
? ? ? ? ? ? ? ? product_id:
? ? ? ? ? ? ? ? ? type: string
? ? ? ? ? ? ? ? ? description: The ID of the product being ordered
? ? ? ? ? ? ? ? quantity:
? ? ? ? ? ? ? ? ? type: integer
? ? ? ? ? ? ? ? ? description: The quantity of the product being ordered
? ? ? ? ? total:
? ? ? ? ? ? type: number
? ? ? ? ? ? description: The total cost of the order
? ? ? ? required:
? ? ? ? ? - order_id
? ? ? ? ? - customer_id
? ? ? ? ? - products
? ? ? ? ? - total
? ? OrderConfirmationMessage:
? ? ? contentType: application/protobuf
? ? ? payload:
? ? ? ? type: object
? ? ? ? properties:
? ? ? ? ? order_id:
? ? ? ? ? ? type: string
? ? ? ? ? ? description: The ID of the order
? ? ? ? ? status:
? ? ? ? ? ? type: string
? ? ? ? ? ? description: The status of the order
? ? ? ? required:
? ? ? ? ? - order_id
? ? ? ? ? - status
Not too bad!
This still needs to be polished to be production-ready and needs a minor cleanup to be ready even for our demo application. Still, starting by using ChatGPT this time was faster than creating this template myself.
P.S. I generated the image for this article you have seen in the header using?DALL-E 2 from OpenAPI?by asking it for "An AI entity pair programming?with a software developer, cartoon style". It's pretty cool!
Please note that it is not recommended to paste your company's private proprietary code into ChatGPT for security reasons unless you have a good reason and permission to do so!