asyncapi / java-spring-template

Java Spring template for the AsyncAPI Generator
Apache License 2.0
66 stars 54 forks source link

Generating code from streetlights sample results in error #75

Closed niketica closed 4 years ago

niketica commented 4 years ago

Description: I am new to asyncapi and I tried to play around with generating code from a yaml spec. After trying to get started with generating code using the java-spring-template I came across an obstacle. Using the streetlights sample (and other samples) generating code results in the error below:

Something went wrong:
Template render error: 
(/home/osboxes/bin/nodejs/node-v12.18.3-linux-x64/lib/node_modules/@asyncapi/generator/node_modules/@asyncapi/java-spring-template/template/src/test/java/com/asyncapi/SimpleKafkaTest.java) [Line 13, Column 64]
  Error: Unable to call `the return value of (the return value of (channel["subscribe"])["message"])["payload"]`, which is undefined or falsey
    at Object._prettifyError (/home/osboxes/bin/nodejs/node-v12.18.3-linux-x64/lib/node_modules/@asyncapi/generator/node_modules/nunjucks/src/lib.js:36:11)
    at /home/osboxes/bin/nodejs/node-v12.18.3-linux-x64/lib/node_modules/@asyncapi/generator/node_modules/nunjucks/src/environment.js:561:19
    at Template.root [as rootRenderFunc] (eval at _compile (/home/osboxes/bin/nodejs/node-v12.18.3-linux-x64/lib/node_modules/@asyncapi/generator/node_modules/nunjucks/src/environment.js:631:18), <anonymous>:649:3)
    at Template.render (/home/osboxes/bin/nodejs/node-v12.18.3-linux-x64/lib/node_modules/@asyncapi/generator/node_modules/nunjucks/src/environment.js:550:10)
    at Environment.renderString (/home/osboxes/bin/nodejs/node-v12.18.3-linux-x64/lib/node_modules/@asyncapi/generator/node_modules/nunjucks/src/environment.js:378:17)
    at /home/osboxes/bin/nodejs/node-v12.18.3-linux-x64/lib/node_modules/@asyncapi/generator/lib/generator.js:610:21
    at new Promise (<anonymous>)
    at Generator.renderString (/home/osboxes/bin/nodejs/node-v12.18.3-linux-x64/lib/node_modules/@asyncapi/generator/lib/generator.js:609:12)
    at Generator.renderFile (/home/osboxes/bin/nodejs/node-v12.18.3-linux-x64/lib/node_modules/@asyncapi/generator/lib/generator.js:633:17)
    at async Generator.generateFile (/home/osboxes/bin/nodejs/node-v12.18.3-linux-x64/lib/node_modules/@asyncapi/generator/lib/generator.js:574:27)

Steps to reproduce the issue:

Expectation: A clarification/solution as to what is wrong. Honestly, I used streetlights as an example, but I expect java-spring-template to create working java code for all samples out of the box in 2.0.0 examples.

github-actions[bot] commented 4 years ago

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

aanno commented 4 years ago

Same problem here with the following example spec:

asyncapi: '2.0.0'
info:
  title: Mercure Hub Example
  version: '1.0.0'
  description: This example demonstrates how to define a Mercure hub.

# While not mandatory, it's a best practice to use formats with hypermedia capabilities such as JSON-LD, Atom or HTML with the Mercure protocol
defaultContentType: application/ld+json

servers:
  production:
    url: https://demo.mercure.rocks/.well-known/mercure
    protocol: mercure

channels:
  'https://example.com/books/{id}':
    description: Every time a resource of type `http://schema.org/Book` is created or modified, a JSON-LD representation of the new version of this resource must be pushed in this Mercure topic.
    parameters:
      id:
        schema:
          type: integer
    subscribe:
      message:
        $ref: '#/components/messages/book'      
    publish:
      message:
        $ref: '#/components/messages/book'

components:
  messages:
    book:
      summary: The content of a book resource.
      externalDocs:
        url: https://schema.org/Book
      payload:
        type: object
        properties:
          '@id':
            type: string
            format: iri-reference
          '@type':
            type: string
            format: iri-reference
          name:
            type: string
          isbn:
            type: string
          abstract:
            type: string

leads to

Something went wrong:
Template render error: (/home/tpasch/scm/github/generator/node_modules/@asyncapi/java-spring-template/template/src/test/java/com/asyncapi/SimpleKafkaTest.java) [Line 13, Column 64]
  Error: Unable to call `the return value of (the return value of (channel["subscribe"])["message"])["payload"]`, which is undefined or falsey
    at Object._prettifyError (/home/tpasch/scm/github/generator/node_modules/nunjucks/src/lib.js:36:11)
    at /home/tpasch/scm/github/generator/node_modules/nunjucks/src/environment.js:561:19
    at Template.root [as rootRenderFunc] (eval at _compile (/home/tpasch/scm/github/generator/node_modules/nunjucks/src/environment.js:631:18), <anonymous>:649:3)
    at Template.render (/home/tpasch/scm/github/generator/node_modules/nunjucks/src/environment.js:550:10)
    at Environment.renderString (/home/tpasch/scm/github/generator/node_modules/nunjucks/src/environment.js:378:17)
    at /home/tpasch/scm/github/generator/lib/generator.js:611:21
    at new Promise (<anonymous>)
    at Generator.renderString (/home/tpasch/scm/github/generator/lib/generator.js:609:12)

I tracked this down to generator.js function renderString for @asyncapi/java-spring-template/template/src/test/java/com/asyncapi/SimpleKafkaTest.java. Offending template line is within

    @Test
    public void {{channel.publish().id() | camelCase}}ConsumerTest() throws InterruptedException {
        Integer key = 1;
        {{channel.publish().message().payload().uid() | camelCase | upperFirst}} payload = new {{channel.publish().message().payload().uid() | camelCase | upperFirst}}();

        ProducerRecord<Integer, Object> producerRecord = new ProducerRecord<>({{channel.publish().id() | upper-}}_TOPIC, key, payload);
        producer.send(producerRecord);
        producer.flush();
        Thread.sleep(1_000);
    }

A fix like https://github.com/asyncapi/java-spring-cloud-stream-template/pull/49 is needed...

pieterdb commented 4 years ago

Trying out AsyncApi for the first time. version 2.0, with the java-spring-template and the associated Streetlights mqtt example at https://github.com/asyncapi/java-spring-template

The command ag throws error on code generation.

stacktrace follows:


Something went wrong: Template render error: (C:\Users\pieterdb\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\@asyncapi\java-spring-template\template\src\test\java\com\asynca pi\SimpleKafkaTest.java) [Line 13, Column 64] Error: Unable to call the return value of (the return value of (channel["subscribe"])["message"])["payload"], which is undefined or falsey at Object._prettifyError (C:\Users\pieterdb\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\lib.js:36:11) at C:\Users\pieterdb\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:561:19 at Template.root [as rootRenderFunc] (eval at _compile (C:\Users\pieterdb\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js: 631:18), :649:3) at Template.render (C:\Users\pieterdb\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:550:10) at Environment.renderString (C:\Users\pieterdb\AppData\Roaming\npm\node_modules\@asyncapi\generator\node_modules\nunjucks\src\environment.js:378:17) at C:\Users\pieterdb\AppData\Roaming\npm\node_modules\@asyncapi\generator\lib\generator.js:610:21 at new Promise () at Generator.renderString (C:\Users\pieterdb\AppData\Roaming\npm\node_modules\@asyncapi\generator\lib\generator.js:609:12) at Generator.renderFile (C:\Users\pieterdb\AppData\Roaming\npm\node_modules\@asyncapi\generator\lib\generator.js:633:17) at async Generator.generateFile (C:\Users\pieterdb\AppData\Roaming\npm\node_modules\@asyncapi\generator\lib\generator.js:574:27)


derberg commented 4 years ago

It looks like there is an issue with the latest release candidates, if you would switch to generator rc-4 all should work fine. Super temporary solution.

@aanno mercurial protocol is not supported in this template atm -> https://github.com/asyncapi/java-spring-template/blob/master/package.json#L67. I also left a comment under your PR

derberg commented 4 years ago

I just found a core of the problem, we introduced a bug here -> https://github.com/asyncapi/parser-js/pull/106/files#diff-b108a8f0ce5b69a1b0fb07f666657d3fR33 and now message() requires index parameter as mandatory, thus all those issues. Will work on a PR to fix it and then once new Parser is released, will bump Generator to next release candidate.

🤞

derberg commented 4 years ago

PR is ready https://github.com/asyncapi/parser-js/pull/132

derberg commented 4 years ago

@niketica @aanno @pieterdb generator with fix for this problem is out -> https://github.com/asyncapi/generator/releases/tag/v1.0.0-rc.7 give it a try. It for sure fixed the problem described in this issue. Other issues should go to new issues.

@niketica

I expect java-spring-template to create working java code for all samples out of the box in 2.0.0 examples

This is not so easy as in the REST APIs world I'm afraid. Event-driven architectures are a complex beast with many brokers, protocols and I bet different implementations per programming language. Even if there is some MQTT sample in 2.0.0 examples you cannot be 100% sure it will work with this example for 2 main reasons:

AsyncAPI community is large but the part that contributes code is very small, so if you can jump in and help that would be lovely!