Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: update template for spring boot 3 #313

Merged
merged 6 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ In order for the generator to know what names to use for some parameters it's ne

#### Supported parameters

|Name|Description|Required| Default |
|---|---|---|----------------------|
|disableEqualsHashCode|Disable generation of equals and hashCode methods for model classes.|No| `false` |
|inverseOperations|Generate an application that will publish messages to `publish` operation of channels and read messages from `subscribe` operation of channels. Literally this flag will simply swap `publish` and `subscribe` operations in the channels. <br> This flag will be useful when you want to generate a code of mock for your main application. Be aware, generation could be incomplete and manual changes will be required e.g. if bindings are defined only for case of main application.|No| `false` |
|javaPackage|The Java package of the generated classes. Alternatively you can set the specification extension `info.x-java-package`. If both extension and parameter are used, parameter has more priority.|No| `com.asyncapi` |
|listenerPollTimeout|Only for Kafka. Timeout in ms to use when polling the consumer.|No| `3000` |
|listenerConcurrency|Only for Kafka. Number of threads to run in the listener containers.|No| `3` |
|addTypeInfoHeader|Only for Kafka. Add type information to message header.|No| `true` |
|connectionTimeout|Only for MQTT. This value, measured in seconds, defines the maximum time interval the client will wait for the network connection to the MQTT server to be established. The default timeout is 30 seconds. A value of 0 disables timeout processing meaning the client will wait until the network connection is made successfully or fails.|No| `30` |
|disconnectionTimeout|Only for MQTT. The completion timeout in milliseconds when disconnecting. The default disconnect completion timeout is 5000 milliseconds.|No| `5000` |
|completionTimeout|Only for MQTT. The completion timeout in milliseconds for operations. The default completion timeout is 30000 milliseconds.|No| `30000` |
|mqttClientId| Only for MQTT. Provides the client identifier for the MQTT server. This parameter overrides the value of the clientId if it's set in the AsyncAPI file.If both aren't provided, a default value is set.|No| |
|asyncapiFileDir| Path where original AsyncAPI file will be stored.|No| `src/main/resources/api/` |
|Name|Description| Required | Default |
|---|---|----------|--------------------------|
|disableEqualsHashCode|Disable generation of equals and hashCode methods for model classes.| No | `false` |
|inverseOperations|Generate an application that will publish messages to `publish` operation of channels and read messages from `subscribe` operation of channels. Literally this flag will simply swap `publish` and `subscribe` operations in the channels. <br> This flag will be useful when you want to generate a code of mock for your main application. Be aware, generation could be incomplete and manual changes will be required e.g. if bindings are defined only for case of main application.| No | `false` |
|javaPackage|The Java package of the generated classes. Alternatively you can set the specification extension `info.x-java-package`. If both extension and parameter are used, parameter has more priority.| No | `com.asyncapi` |
|springBoot2|Generate template files for the Spring Boot version 2. For kafka protocol it will also force to use spring-kafka 2.9.9| No | `false` |
|listenerPollTimeout|Only for Kafka. Timeout in ms to use when polling the consumer.| No | `3000` |
|listenerConcurrency|Only for Kafka. Number of threads to run in the listener containers.| No | `3` |
|addTypeInfoHeader|Only for Kafka. Add type information to message header.| No | `true` |
|connectionTimeout|Only for MQTT. This value, measured in seconds, defines the maximum time interval the client will wait for the network connection to the MQTT server to be established. The default timeout is 30 seconds. A value of 0 disables timeout processing meaning the client will wait until the network connection is made successfully or fails.| No | `30` |
|disconnectionTimeout|Only for MQTT. The completion timeout in milliseconds when disconnecting. The default disconnect completion timeout is 5000 milliseconds.| No | `5000` |
|completionTimeout|Only for MQTT. The completion timeout in milliseconds for operations. The default completion timeout is 30000 milliseconds.| No | `30000` |
|mqttClientId| Only for MQTT. Provides the client identifier for the MQTT server. This parameter overrides the value of the clientId if it's set in the AsyncAPI file.If both aren't provided, a default value is set.| No | |
|asyncapiFileDir| Path where original AsyncAPI file will be stored.| No | `src/main/resources/api/` |
#### Examples

The shortest possible syntax:
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
"description": "Only for Kafka. Add type information to the message header",
"default": "true",
"required": false
},
"springBoot2": {
"description": "Generate template files for the Spring Boot version 2. For kafka protocol it will also force to use spring-kafka 2.9.9",
"default": false,
"required": false
}
},
"generator": ">=1.8.27 <2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion partials/KafkaPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PublisherService {
public void {{channel.subscribe().id() | camelCase}}(Integer key, {{varName | upperFirst}} {{varName}}) {
Message<{{varName | upperFirst}}> message = MessageBuilder.withPayload({{varName}})
.setHeader(KafkaHeaders.TOPIC, "{{channelName}}")
.setHeader(KafkaHeaders.MESSAGE_KEY, key)
.setHeader(KafkaHeaders.{%- if params.springBoot2 %}MESSAGE_KEY{% else %}KEY{% endif -%}, key)
.build();
kafkaTemplate.send(message);
}
Expand Down
13 changes: 12 additions & 1 deletion template/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,26 @@ dependencies {
implementation('org.springframework.integration:spring-integration-mqtt')
{% endif -%}
{%- if asyncapi | isProtocol('kafka') %}
{%- if params.springBoot2 %}
implementation('org.springframework.kafka:spring-kafka:2.9.9')
testImplementation('org.springframework.kafka:spring-kafka-test:2.9.9')
{% else %}
implementation('org.springframework.kafka:spring-kafka')
testImplementation('org.springframework.kafka:spring-kafka-test')
{% endif -%}
testImplementation('junit:junit:4.12')
testCompile('org.testcontainers:kafka:1.16.3')
{% endif -%}
implementation('com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider')
implementation('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
implementation('javax.validation:validation-api')
{%- if params.springBoot2 %}
implementation('javax.validation:validation-api')
implementation('org.springframework.boot:spring-boot-starter-integration:2.7.12')
testImplementation('org.springframework.boot:spring-boot-starter-test:2.7.12')
{% else %}
implementation('jakarta.validation:jakarta.validation-api')
implementation('org.springframework.boot:spring-boot-starter-integration')
testImplementation('org.springframework.boot:spring-boot-starter-test')
{% endif -%}
testCompile('org.testcontainers:testcontainers:1.16.3')
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package {{ params['userJavaPackage'] }}.model;

{% if params.springBoot2 -%}
import javax.validation.Valid;

{% else %}
import jakarta.validation.Valid;
{%- endif %}
import java.util.Objects;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package {{ params['userJavaPackage'] }}.model;

{% if params.springBoot2 -%}
import javax.validation.constraints.*;
import javax.validation.Valid;
{% else %}
import jakarta.validation.constraints.*;
import jakarta.validation.Valid;
{%- endif %}

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class MessageHandlerService {
*/{% endif %}
@KafkaListener(topics = "{{channelName}}"{% if channel.publish().binding('kafka') %}, groupId = "{{channel.publish().binding('kafka').groupId}}"{% endif %})
public void {{channel.publish().id() | camelCase}}(@Payload {{typeName}} payload,
@Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) Integer key,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition,
@Header(KafkaHeaders.{%- if params.springBoot2 %}RECEIVED_MESSAGE_KEY{% else %}RECEIVED_KEY{% endif -%}) Integer key,
@Header(KafkaHeaders.{%- if params.springBoot2 %}RECEIVED_PARTITION_ID{% else %}RECEIVED_PARTITION{% endif -%}) int partition,
@Header(KafkaHeaders.RECEIVED_TIMESTAMP) long timestamp) {
LOGGER.info("Key: " + key + ", Payload: " + payload.toString() + ", Timestamp: " + timestamp + ", Partition: " + partition);
}
Expand Down
5 changes: 3 additions & 2 deletions tests/__snapshots__/additional-formats.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
exports[`template integration tests for additional formats of data types should generate DTO file with proper type classes 1`] = `
"package com.asyncapi.model;

import javax.validation.constraints.*;
import javax.validation.Valid;

import jakarta.validation.constraints.*;
import jakarta.validation.Valid;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
15 changes: 8 additions & 7 deletions tests/__snapshots__/kafka.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class PublisherService {
public void updateLightMeasurement(Integer key, LightMeasuredPayload lightMeasuredPayload) {
Message<LightMeasuredPayload> message = MessageBuilder.withPayload(lightMeasuredPayload)
.setHeader(KafkaHeaders.TOPIC, \\"event.lighting.measured\\")
.setHeader(KafkaHeaders.MESSAGE_KEY, key)
.setHeader(KafkaHeaders.KEY, key)
.build();
kafkaTemplate.send(message);
}
Expand Down Expand Up @@ -146,8 +146,8 @@ public class MessageHandlerService {

@KafkaListener(topics = \\"event.lighting.measured\\", groupId = \\"my-group\\")
public void readLightMeasurement(@Payload LightMeasuredPayload payload,
@Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) Integer key,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition,
@Header(KafkaHeaders.RECEIVED_KEY) Integer key,
@Header(KafkaHeaders.RECEIVED_PARTITION) int partition,
@Header(KafkaHeaders.RECEIVED_TIMESTAMP) long timestamp) {
LOGGER.info(\\"Key: \\" + key + \\", Payload: \\" + payload.toString() + \\", Timestamp: \\" + timestamp + \\", Partition: \\" + partition);
}
Expand All @@ -160,8 +160,9 @@ public class MessageHandlerService {
exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for provided kafka 4`] = `
"package com.asyncapi.model;

import javax.validation.constraints.*;
import javax.validation.Valid;

import jakarta.validation.constraints.*;
import jakarta.validation.Valid;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -248,8 +249,8 @@ public class LightMeasuredPayload {
exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for provided kafka 5`] = `
"package com.asyncapi.model;

import javax.validation.Valid;

import jakarta.validation.Valid;
import java.util.Objects;
import java.util.List;

Expand Down Expand Up @@ -481,7 +482,7 @@ dependencies {
testCompile('org.testcontainers:kafka:1.16.3')
implementation('com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider')
implementation('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
implementation('javax.validation:validation-api')
implementation('jakarta.validation:jakarta.validation-api')
implementation('org.springframework.boot:spring-boot-starter-integration')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testCompile('org.testcontainers:testcontainers:1.16.3')
Expand Down
32 changes: 18 additions & 14 deletions tests/__snapshots__/mqtt.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,9 @@ public class MessageHandlerService {
exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for basic example 4`] = `
"package com.asyncapi.model;

import javax.validation.constraints.*;
import javax.validation.Valid;

import jakarta.validation.constraints.*;
import jakarta.validation.Valid;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -325,8 +326,9 @@ public class DimLightPayload {
exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for basic example 5`] = `
"package com.asyncapi.model;

import javax.validation.constraints.*;
import javax.validation.Valid;

import jakarta.validation.constraints.*;
import jakarta.validation.Valid;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -413,8 +415,9 @@ public class LightMeasuredPayload {
exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for basic example 6`] = `
"package com.asyncapi.model;

import javax.validation.constraints.*;
import javax.validation.Valid;

import jakarta.validation.constraints.*;
import jakarta.validation.Valid;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -534,8 +537,8 @@ public class TurnOnOffPayload {
exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for basic example 7`] = `
"package com.asyncapi.model;

import javax.validation.Valid;

import jakarta.validation.Valid;
import java.util.Objects;
import java.util.List;

Expand Down Expand Up @@ -590,8 +593,8 @@ public class DimLight {
exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for basic example 8`] = `
"package com.asyncapi.model;

import javax.validation.Valid;

import jakarta.validation.Valid;
import java.util.Objects;
import java.util.List;

Expand Down Expand Up @@ -646,8 +649,8 @@ public class LightMeasured {
exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for basic example 9`] = `
"package com.asyncapi.model;

import javax.validation.Valid;

import jakarta.validation.Valid;
import java.util.Objects;
import java.util.List;

Expand Down Expand Up @@ -870,7 +873,7 @@ dependencies {
implementation('org.springframework.integration:spring-integration-mqtt')
implementation('com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider')
implementation('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
implementation('javax.validation:validation-api')
implementation('jakarta.validation:jakarta.validation-api')
implementation('org.springframework.boot:spring-boot-starter-integration')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testCompile('org.testcontainers:testcontainers:1.16.3')
Expand Down Expand Up @@ -1081,8 +1084,9 @@ public class MessageHandlerService {
exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for provided mqtt 4`] = `
"package com.asyncapi.model;

import javax.validation.constraints.*;
import javax.validation.Valid;

import jakarta.validation.constraints.*;
import jakarta.validation.Valid;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -1169,8 +1173,8 @@ public class LightMeasuredPayload {
exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for provided mqtt 5`] = `
"package com.asyncapi.model;

import javax.validation.Valid;

import jakarta.validation.Valid;
import java.util.Objects;
import java.util.List;

Expand Down Expand Up @@ -1349,7 +1353,7 @@ dependencies {
implementation('org.springframework.integration:spring-integration-mqtt')
implementation('com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider')
implementation('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
implementation('javax.validation:validation-api')
implementation('jakarta.validation:jakarta.validation-api')
implementation('org.springframework.boot:spring-boot-starter-integration')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testCompile('org.testcontainers:testcontainers:1.16.3')
Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/oneOf.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class MessageHandlerService {

@KafkaListener(topics = \\"song.released\\")
public void release(@Payload Object payload,
@Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) Integer key,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition,
@Header(KafkaHeaders.RECEIVED_KEY) Integer key,
@Header(KafkaHeaders.RECEIVED_PARTITION) int partition,
@Header(KafkaHeaders.RECEIVED_TIMESTAMP) long timestamp) {
LOGGER.info(\\"Key: \\" + key + \\", Payload: \\" + payload.toString() + \\", Timestamp: \\" + timestamp + \\", Partition: \\" + partition);
}
Expand Down