asyncapi / nodejs-template

This template generates a server using your AsyncAPI document. It supports multiple different protocols, like Kafka or MQTT. It is designed in the way that generated code is a library and with it's API you can start the server, send messages or register a middleware for listening incoming messages. Runtime message validation included.
40 stars 29 forks source link

Using oneOf, generator failing looking for ((channel["publish"])["message"])["name"] which is undefined or falsey #62

Closed DavidBiesack closed 2 years ago

DavidBiesack commented 3 years ago

Describe the bug

When a channel uses oneOf instead of a single message, the generator fails with Error: Unable to call the return value of (the return value of (channel["publish"])["message"])["name"], which is undefined or falsey

How to Reproduce

Copy the sample from https://www.asyncapi.com/docs/tutorials/streetlights but use oneOf (as a precursor to publishing multiple events to that channel).

Source:

asyncapi: '2.0.0'
info:
  title: Streetlights API
  version: '1.0.0'
  description: |
    The Smartylighting Streetlights API allows you
    to remotely manage the city lights.
  license:
    name: Apache 2.0
    url: 'https://www.apache.org/licenses/LICENSE-2.0'
servers:
  mosquitto:
    url: mqtt://test.mosquitto.org
    protocol: mqtt
channels:
  light/measured:
    publish:
      summary: Inform about environmental lighting conditions for a particular streetlight.
      operationId: onLightMeasured
      message:
        oneOf:
          - name: LightMeasured
            payload:
              type: object
              properties:
                id:
                  type: integer
                  minimum: 0
                  description: Id of the streetlight.
                lumens:
                  type: integer
                  minimum: 0
                  description: Light intensity measured in lumens.
                sentAt:
                  type: string
                  format: date-time
                  description: Date and time when the message was sent.

run:

ag --output __sdk asyncapi.yaml @asyncapi/nodejs-template -p server=mosquitto

Something went wrong:
Template render error: (~/.nvm/versions/node/v12.21.0/lib/node_modules/@asyncapi/nodejs-template/template/src/api/routes/$$channel$$.js) [Line 13, Column 81]
  Error: Unable to call `the return value of (the return value of (channel["publish"])["message"])["name"]`, which is undefined or falsey
    at Object._prettifyError (~/dev/asyncapi/async-events-api-def/node_modules/nunjucks/src/lib.js:36:11)
    at ~/dev/asyncapi/async-events-api-def/node_modules/nunjucks/src/environment.js:563:19
    at Template.root [as rootRenderFunc] (eval at _compile (~/dev/asyncapi/async-events-api-def/node_modules/nunjucks/src/environment.js:633:18), <anonymous>:60:3)
    at Template.render (~/dev/asyncapi/async-events-api-def/node_modules/nunjucks/src/environment.js:552:10)
    at Environment.renderString (~/dev/asyncapi/async-events-api-def/node_modules/nunjucks/src/environment.js:380:17)
    at ~/dev/asyncapi/async-events-api-def/node_modules/@asyncapi/generator/lib/renderer/nunjucks.js:32:14
    at new Promise (<anonymous>)
    at nunjucksExport.renderNunjucks (~/dev/asyncapi/async-events-api-def/node_modules/@asyncapi/generator/lib/renderer/nunjucks.js:31:10)
    at Generator.renderFile (~/dev/asyncapi/async-events-api-def/node_modules/@asyncapi/generator/lib/generator.js:688:12)
    at Generator.renderAndWriteToFile (~/dev/asyncapi/async-events-api-def/node_modules/@asyncapi/generator/lib/generator.js:602:27)

Expected behavior

iterator over all the messages in the oneOf

derberg commented 3 years ago

Looks like the issue is with the fact that template is not supporting oneOf end expects a single message on a operation -> https://github.com/asyncapi/nodejs-template/blob/master/template/src/api/routes/%24%24channel%24%24.js#L14

DavidBiesack commented 3 years ago

@derberg template/src/api/handlers/$$channel$$.js uses channel.publish().message(0) --- should src/api/routes/$$channel$$.js use channel.publish().message(0) and channel.subscribe().message(0) as well, instead of message()? (I tried that patch in my fork of the repo and it appears to work - let me know and I can submit a PR)

Or should the message() function detect when the message is an oneOf, and default to message(0) if no index is passed? I don't know enough about the generator to know where that message() function is defined.

However, I don't see how nodejs-template really deals with the arrays if it only processes message(0) and not message(1) through message(n-1) - thoughts on this?

derberg commented 3 years ago

@DavidBiesack these are the functions related to message that you can use: https://github.com/asyncapi/parser-js/blob/master/lib/models/operation.js

message should have index if oneOf is used. So best is always to check with hasMultipleMessages if oneOf is available and then work it out with messages that returns an array of messages, and then process one by one.

DavidBiesack commented 3 years ago

Hmm, nodejs-template does not use hasMultipleMessages anywhere ☹️ I'll have to see how others do it. What is the most "mature" (complete) generator plugin?

DavidBiesack commented 3 years ago

https://github.com/search?q=org%3Aasyncapi+hasMultipleMessages&type=code shows html-template and markdown-template use it (so there is an example) but none of the real code generators.

DavidBiesack commented 3 years ago

Without really knowing what nodejs-template is trying to do, I'm not comfortable trying to extend it to use hasMultipleMessages

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity :sleeping: It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation. Thank you for your contributions :heart:

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity :sleeping: It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation. Thank you for your contributions :heart:

DavidBiesack commented 3 years ago

I submitted a feature request JS Parser. https://github.com/asyncapi/parser-js/issues/372 for returning one single JSON schema for generators to access and let the JSON Schema validation handle the oneOf processing. I updated the code with a TODO note to update when that feature is implemented.

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

DavidBiesack commented 2 years ago

@derberg is it possible to merge the or and close this?

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

derberg commented 2 years ago

@DavidBiesack just approved PR, please have a look 🙏🏼

asyncapi-bot commented 2 years ago

:tada: This issue has been resolved in version 0.12.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: