lerenn / asyncapi-codegen

An AsyncAPI Golang Code generator that generates all Go code from the broker to the application/user. Just plug your application to your favorite message broker!
Apache License 2.0
76 stars 21 forks source link

AsyncAPI v3: support proper Message references from the Operation #211

Closed wizzardich closed 2 months ago

wizzardich commented 2 months ago

Current Behaviour

Given the following configuration of the operation, channel and message objects:

asyncapi: 3.0.0
info:
  title: event
  version: 0.1.0
channels:
  eventSuccess:
    address: some.random.topic
    messages:
      eventSuccessMessage:
        $ref: '#/components/messages/EventSuccess'
operations:
  handleEventSuccess:
    action: 'receive'
    channel:
      $ref: '#/channels/eventSuccess'
    messages:
      - $ref: '#/channels/eventSuccess/messages/eventSuccessMessage'
components:
  messages:
    EventSuccess:
      payload:
        type: object
        properties:
          timestamp:
            type: integer
            description: The timestamp of the event

The execution of asyncapi-codegen fails while parsing the spec:

✘ asyncapi-codegen -i api/asyncapi/client/events.yml -o asyncapi.gen.go -g types
Error: error when using AsyncAPI: invalid reference: cannot cast "#/channels/eventSuccess/messages/eventSuccessMessage" into 'Message' (type is "*asyncapiv3.Channel")

According to the AsyncAPI v3 specification, the Operation object's messages field is supposed to reference a subset of channel's messages.

Currently the following would work with asyncapi-codegen:

asyncapi: 3.0.0
info:
  title: event
  version: 0.1.0
channels:
  eventSuccess:
    address: some.random.topic
    messages:
      eventSuccessMessage:
        $ref: '#/components/messages/EventSuccess'
operations:
  handleEventSuccess:
    action: 'receive'
    channel:
      $ref: '#/channels/eventSuccess'
    messages:
      - $ref: '#/components/messages/EventSuccess'
components:
  messages:
    EventSuccess:
      payload:
        type: object
        properties:
          timestamp:
            type: integer
            description: The timestamp of the event

But AsyncAPI parser that AsyncAPI Studio utilizes breaks on this with Operation message does not belong to the specified channel.. If I understand correctly, this is the spec's intention.

Expected Behaviour

The messags refs are resolved correctly according to the spec.

Notes

PS. Thanks again :)

lerenn commented 2 months ago

I see ! Well, I probably won't have time to tackle this today, but I'll do it in the next days (probably tomorrow).

Thanks again for taking the time to report this ! :)

lerenn commented 2 months ago

Also thanks for providing examples, it really helps me to reproduce and write non-regression tests to avoid any return of this bug !

wizzardich commented 2 months ago

I know the pain of trying to figure out the repro, no worries! Glad to hear that helps :)