Reuse (generated) entities between OpenAPI and AsyncAPI

48 Views Asked by At

I have a Quarkus / Gradle / Kotlin project with the following conditions:

  • I have to use an OpenAPI spec for all API endpoints (and let the interfaces generate with the OpenAPI generator in JAX-RS style)
  • I have to use an AsyncAPI spec for sending messages to a Kafka topic.
  • I have to use Avro as the messaging format.
  • I have to persist the entity in a MySQL database (and therefore currently use a separate panache entity)

Scenario: I have an Entity "Animal(name, age)" that I receive via a post endpoint (specified in the OpenAPI spec). This animal has then to be persisted in the database and has to be sent to a Kafka topic (with an Avro Schema).

My code is already working, but I have some architectural "pain points":

  • Problem 1)
    Currently I have three entities for the Animal class:
    -> The one for the rest endpoint is generated by the OpenAPI generator from the spec.
    -> The one for messaging is generated by the Quarkus / Kafka generator from the Avro AVSC file that is referenced in the AsyncAPI spec.
    -> The one for persistence is a self written Jakarta Entity which is used in the PanacheRepository.
    So:
    This means I always have to convert the incoming animal to the DB entity and to the messaging entity! Is there any way to use / let generate the same entity for all three cases (or at least for messaging and rest)?

  • Problem 2) I can reference the Avro AVSC file in the AsyncAPI spec ... but not in the OpenAPI spec. Which means that there I also have to write the Entity twice. Is there any way to reuse the same entity in both the AsyncAPI and the OpenAPI spec?

I was searching for converters from JSON Schema to Avro (so that I could reuse the entity in OpenAPI and AsyncAPI spec), but:

  1. They all did not work well
  2. It also does not solve my problem of the multiple different generated representations of my entity.
0

There are 0 best solutions below