From c321c710d78e7b00aa45248036b200c4fbfaff9b Mon Sep 17 00:00:00 2001 From: Semen Date: Sun, 26 Apr 2020 20:49:13 +0300 Subject: [PATCH] feat: add descriptions as javadocs over generated code (#18) * feat: add descriptions as javadocs over generated code * fix: generation for mqtt and amqp (#14) * fix api calls for mqtt and amqp * fix port variables * feat: add annotations to the model generation (#12) * WIP, update to spec 2.0 * WIP, update to spec 2.0, remove definition of topic via extension * working version for api 2.0 * add model generation * Add support of Kafka generation * update readme * fix value ref, add jackson lib * add validation annotations to schema * fix no type case * add annotation for jackson * add description and examples to javadoc * Update readme, move missed features block * switch publish to subscribe and vice versa, because of wrong understanding of spec * Fix consumed key, add key to publish operation * move jackson and validation dependencies from kafka block * fix README formatting * mark annotations as done in README * add support of multi-line description * fix json in examples * chore(release): 0.5.0 (#15) chore(release): 0.5.0 * fix api calls for mqtt and amqp * fix port variables * fix yet another api operation call Co-authored-by: asyncapi-bot <61865014+asyncapi-bot@users.noreply.github.com> * chore(release): 0.5.1 (#19) chore(release): 0.5.1 * feat: generalize Kafka properties (#16) * feat: generalize kafka properties. use spring-kafka props inst.of custom, move props to template params * fix wrong param name in example * fix: generation for mqtt and amqp (#14) * fix api calls for mqtt and amqp * fix port variables * feat: add annotations to the model generation (#12) * WIP, update to spec 2.0 * WIP, update to spec 2.0, remove definition of topic via extension * working version for api 2.0 * add model generation * Add support of Kafka generation * update readme * fix value ref, add jackson lib * add validation annotations to schema * fix no type case * add annotation for jackson * add description and examples to javadoc * Update readme, move missed features block * switch publish to subscribe and vice versa, because of wrong understanding of spec * Fix consumed key, add key to publish operation * move jackson and validation dependencies from kafka block * fix README formatting * mark annotations as done in README * add support of multi-line description * fix json in examples * chore(release): 0.5.0 (#15) chore(release): 0.5.0 * fix api calls for mqtt and amqp * fix port variables * fix yet another api operation call Co-authored-by: asyncapi-bot <61865014+asyncapi-bot@users.noreply.github.com> * chore(release): 0.5.1 (#19) chore(release): 0.5.1 * feat: generalize kafka properties. use spring-kafka props inst.of custom, move props to template params * fix wrong param name in example Co-authored-by: asyncapi-bot <61865014+asyncapi-bot@users.noreply.github.com> * chore(release): 0.6.0 (#20) chore(release): 0.6.0 * feat: add descriptions as javadocs over generated code Co-authored-by: asyncapi-bot <61865014+asyncapi-bot@users.noreply.github.com> --- filters/all.js | 2 +- partials/CommonPublisher.java | 4 ++++ partials/KafkaPublisher.java | 4 ++++ .../com/asyncapi/service/MessageHandlerService.java | 9 +++++++++ template/src/main/resources/application.yml | 10 +++++++--- 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/filters/all.js b/filters/all.js index 44f2223cf..778bdb48f 100644 --- a/filters/all.js +++ b/filters/all.js @@ -66,7 +66,7 @@ module.exports = ({ Nunjucks }) => { Nunjucks.addFilter('splitByLines', (str) => { if (str) { - return str.split(/\r?\n|\r/); + return str.split(/\r?\n|\r/).filter((s) => s !== ""); } else { return ""; } diff --git a/partials/CommonPublisher.java b/partials/CommonPublisher.java index 58c4d9b61..ec96fef50 100644 --- a/partials/CommonPublisher.java +++ b/partials/CommonPublisher.java @@ -9,6 +9,10 @@ public interface PublisherService { {% for channelName, channel in asyncapi.channels() %} {% if channel.hasSubscribe() %} + {% if channel.description() or channel.subscribe().description() %}/**{% for line in channel.description() | splitByLines %} + * {{line | safe}}{% endfor %}{% for line in channel.subscribe().description() | splitByLines %} + * {{line | safe}}{% endfor %} + */{% endif %} @Gateway(requestChannel = "{{channelName | camelCase}}OutboundChannel") void {{channel.subscribe().id() | camelCase}}(String data); {% endif %} diff --git a/partials/KafkaPublisher.java b/partials/KafkaPublisher.java index 6e65c2a0e..55884df73 100644 --- a/partials/KafkaPublisher.java +++ b/partials/KafkaPublisher.java @@ -20,6 +20,10 @@ public class PublisherService { private KafkaTemplate kafkaTemplate; {% for channelName, channel in asyncapi.channels() %} {%- if channel.hasSubscribe() %} {% set varName = channel.subscribe().message().payload().uid() | camelCase %} + {% if channel.description() or channel.subscribe().description() %}/**{% for line in channel.description() | splitByLines %} + * {{line | safe}}{% endfor %}{% for line in channel.subscribe().description() | splitByLines %} + * {{line | safe}}{% endfor %} + */{% endif %} public void {{channel.subscribe().id() | camelCase}}(Integer key, {{varName | upperFirst}} {{varName}}) { Message<{{varName | upperFirst}}> message = MessageBuilder.withPayload({{varName}}) .setHeader(KafkaHeaders.TOPIC, "{{channelName}}") diff --git a/template/src/main/java/com/asyncapi/service/MessageHandlerService.java b/template/src/main/java/com/asyncapi/service/MessageHandlerService.java index 213f4db17..5fb289368 100644 --- a/template/src/main/java/com/asyncapi/service/MessageHandlerService.java +++ b/template/src/main/java/com/asyncapi/service/MessageHandlerService.java @@ -28,6 +28,11 @@ public class MessageHandlerService { {% if asyncapi | isProtocol('kafka') %} {% for channelName, channel in asyncapi.channels() %} {%- if channel.hasPublish() %} + + {% if channel.description() or channel.publish().description() %}/**{% for line in channel.description() | splitByLines %} + * {{line | safe}}{% endfor %}{% for line in channel.publish().description() | splitByLines %} + * {{line | safe}}{% endfor %} + */{% endif %} @KafkaListener(topics = "{{channelName}}"{% if channel.publish().binding('kafka') %}, groupId = "{{channel.publish().binding('kafka').groupId}}"{% endif %}) public void {{channel.publish().id() | camelCase}}(@Payload {{channel.publish().message().payload().uid() | camelCase | upperFirst}} payload, @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) Integer key, @@ -40,6 +45,10 @@ public class MessageHandlerService { {% else %} {% for channelName, channel in asyncapi.channels() %} {% if channel.hasPublish() %} + {% if channel.description() or channel.publish().description() %}/**{% for line in channel.description() | splitByLines %} + * {{line | safe}}{% endfor %}{% for line in channel.publish().description() | splitByLines %} + * {{line | safe}}{% endfor %} + */{% endif %} public void handle{{channelName | upperFirst}}(Message message) { System.out.println("handler {{channelName}}"); System.out.println(message.getPayload()); diff --git a/template/src/main/resources/application.yml b/template/src/main/resources/application.yml index 6f62594b0..6a0dfec98 100644 --- a/template/src/main/resources/application.yml +++ b/template/src/main/resources/application.yml @@ -10,7 +10,8 @@ {%- endfor -%} {%- for serverName, server in asyncapi.servers() %}{% if server.protocol() == 'amqp' %} amqp: - broker: + broker: {% for line in server.description() | splitByLines %} + # {{line | safe}}{% endfor %} host: {{server.url() | replace(':{port}', '') }} port: {% if server.variable('port') %}{{server.variable('port').defaultValue()}}{% endif %} username: @@ -30,7 +31,8 @@ amqp: {% endif %} {% if server.protocol() == 'mqtt' %} mqtt: - broker: + broker: {% for line in server.description() | splitByLines %} + # {{line | safe}}{% endfor %} host: tcp://{{server.url() | replace(':{port}', '')}} port: {% if server.variable('port') %}{{server.variable('port').defaultValue()}}{% endif %} username: @@ -42,7 +44,9 @@ mqtt: {% endif %}{% endfor %} {%- if asyncapi | isProtocol('kafka') %} spring: - kafka: + kafka: {% for serverName, server in asyncapi.servers() %} + # - {{serverName}} in {{server.url()}} : {% for line in server.description() | splitByLines %} + # {{line | safe}}{% endfor %}{% endfor %} bootstrap-servers: {% for serverName, server in asyncapi.servers() %}{% if server.variable('port') %}{{server.url() | replace('{port}', server.variable('port').defaultValue())}}{% else %}{{server.url()}}{% endif %}{% if not loop.last %},{% endif %}{% endfor %} {%- if hasPublish %} listener: