diff --git a/plugins/jdl-to-asyncapi/README.md b/plugins/jdl-to-asyncapi/README.md index fa08b0d1..4c77d821 100644 --- a/plugins/jdl-to-asyncapi/README.md +++ b/plugins/jdl-to-asyncapi/README.md @@ -30,7 +30,7 @@ jbang zw -p io.zenwave360.sdk.plugins.JDLToAsyncAPIPlugin \ idType=integer \ idTypeFormat=int64 \ annotations=aggregate \ - payloadStyle=stateTransfer \ + payloadStyle=event \ targetFile=src/main/resources/model/asyncapi.yml ``` @@ -46,7 +46,7 @@ jbang zw -p io.zenwave360.sdk.plugins.JDLToAsyncAPIPlugin \ | `targetFolder` | Target folder for generated output | String | | | | `targetFile` | Target file | String | asyncapi.yml | | | `schemaFormat` | Schema format for messages' payload | SchemaFormat | schema | schema, avro | -| `payloadStyle` | Payload Style for messages' payload | PayloadStyle | entity | entity, stateTransfer | +| `payloadStyle` | Payload Style for messages' payload | PayloadStyle | entity | entity, event | | `idType` | JsonSchema type for id fields and parameters. | String | string | | | `idTypeFormat` | JsonSchema type format for id fields and parameters. | String | | | | `entities` | Entities to generate code for | List | [] | | diff --git a/plugins/jdl-to-asyncapi/src/main/java/io/zenwave360/sdk/plugins/JDLToAsyncAPIGenerator.java b/plugins/jdl-to-asyncapi/src/main/java/io/zenwave360/sdk/plugins/JDLToAsyncAPIGenerator.java index 65ded57c..3220e01a 100644 --- a/plugins/jdl-to-asyncapi/src/main/java/io/zenwave360/sdk/plugins/JDLToAsyncAPIGenerator.java +++ b/plugins/jdl-to-asyncapi/src/main/java/io/zenwave360/sdk/plugins/JDLToAsyncAPIGenerator.java @@ -33,7 +33,7 @@ enum SchemaFormat { } enum PayloadStyle { - entity, stateTransfer + entity, event } public String sourceProperty = "jdl"; @@ -184,7 +184,7 @@ protected List convertToAvro(JDLEntitiesToAvroConverter converte avroList.add(new TemplateOutput(String.format("%s/%s.avsc", targetFolder, name), avroJson, OutputFormatType.JSON.toString())); - if (payloadStyle == PayloadStyle.stateTransfer && (!skipOperations(entityOrEnum) || entityOrEnum.get("fields") == null)) { + if (payloadStyle == PayloadStyle.event && (!skipOperations(entityOrEnum) || entityOrEnum.get("fields") == null)) { // creating 'fake' jdl entities for message payloads for created/updated/deleted as { id: , payload: } Map fields = new HashMap<>(); @@ -252,13 +252,13 @@ protected boolean skipOperations(Map entity) { }); handlebarsEngine.getHandlebars().registerHelper("isStateTransferPayloadStyle", (context, options) -> { - return payloadStyle == PayloadStyle.stateTransfer; + return payloadStyle == PayloadStyle.event; }); handlebarsEngine.getHandlebars().registerHelper("payloadRef", (context, options) -> { Map entity = (Map) context; - String messageType = payloadStyle == PayloadStyle.stateTransfer ? options.param(0) : ""; - String payloadStyleSuffix = payloadStyle == PayloadStyle.stateTransfer ? "Payload" : ""; + String messageType = payloadStyle == PayloadStyle.event ? options.param(0) : ""; + String payloadStyleSuffix = payloadStyle == PayloadStyle.event ? "Payload" : ""; if (schemaFormat == SchemaFormat.avro) { return String.format("avro/%s%s%s.avsc", entity.get("className"), messageType, payloadStyleSuffix); } diff --git a/plugins/jdl-to-asyncapi/src/test/java/io/zenwave360/sdk/plugins/JDLToAsyncAPIGeneratorTest.java b/plugins/jdl-to-asyncapi/src/test/java/io/zenwave360/sdk/plugins/JDLToAsyncAPIGeneratorTest.java index 2e5ede86..94be5b7f 100644 --- a/plugins/jdl-to-asyncapi/src/test/java/io/zenwave360/sdk/plugins/JDLToAsyncAPIGeneratorTest.java +++ b/plugins/jdl-to-asyncapi/src/test/java/io/zenwave360/sdk/plugins/JDLToAsyncAPIGeneratorTest.java @@ -69,7 +69,7 @@ public void test_jdl_to_asyncapi_state_transfer_style() throws Exception { Map model = loadJDLModelFromResource("classpath:io/zenwave360/sdk/resources/jdl/orders-model.jdl"); JDLToAsyncAPIGenerator generator = new JDLToAsyncAPIGenerator(); generator.includeCommands = true; - generator.payloadStyle = JDLToAsyncAPIGenerator.PayloadStyle.stateTransfer; + generator.payloadStyle = JDLToAsyncAPIGenerator.PayloadStyle.event; generator.annotations = List.of("aggregate"); List outputTemplates = generator.generate(model); @@ -94,7 +94,7 @@ public void test_jdl_to_asyncapi_with_avro_state_transfer_style() throws Excepti JDLToAsyncAPIGenerator generator = new JDLToAsyncAPIGenerator(); generator.schemaFormat = JDLToAsyncAPIGenerator.SchemaFormat.avro; generator.includeCommands = true; - generator.payloadStyle = JDLToAsyncAPIGenerator.PayloadStyle.stateTransfer; + generator.payloadStyle = JDLToAsyncAPIGenerator.PayloadStyle.event; List outputTemplates = generator.generate(model); diff --git a/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/BusinessFlowTest.java.hbs b/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/BusinessFlowTest.java.hbs index e352138e..ab217b34 100644 --- a/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/BusinessFlowTest.java.hbs +++ b/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/BusinessFlowTest.java.hbs @@ -13,6 +13,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.test.web.reactive.server.WebTestClient; +import java.math.BigDecimal; + import static org.springframework.http.HttpMethod.*; /** diff --git a/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/OperationIT.java.hbs b/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/OperationIT.java.hbs index 1600c9a7..1e61faa8 100644 --- a/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/OperationIT.java.hbs +++ b/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/OperationIT.java.hbs @@ -16,6 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.test.web.reactive.server.WebTestClient; +import java.math.BigDecimal; + import static org.springframework.http.HttpMethod.*; /** diff --git a/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/ServiceIT.java.hbs b/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/ServiceIT.java.hbs index b898163e..345bcd1c 100644 --- a/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/ServiceIT.java.hbs +++ b/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/ServiceIT.java.hbs @@ -16,6 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.test.web.reactive.server.WebTestClient; +import java.math.BigDecimal; + import static org.springframework.http.HttpMethod.*; /** diff --git a/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/partials/requestSetup.hbs b/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/partials/requestSetup.hbs index 9835697c..1313dd6b 100644 --- a/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/partials/requestSetup.hbs +++ b/plugins/openapi-spring-webtestclient/src/main/resources/io/zenwave360/sdk/plugins/SpringWebTestClientGenerator/partials/requestSetup.hbs @@ -4,11 +4,11 @@ {{requestBodyVar}}.set{{asJavaTypeName @key}}({{{newPropertyObject property}}}); {{~#if (eq property.type 'object')}} {{~#each property.properties as |innerProperty|}} - {{requestBodyVar}}.get{{asJavaTypeName parentPropertyName}}().set{{asJavaTypeName @key}}(null); + {{requestBodyVar}}.get{{asJavaTypeName parentPropertyName}}().set{{asJavaTypeName @key}}({{{populateProperty innerProperty}}}); {{~/each}} {{~else if (eq property.type 'array')}} {{~#each property.items.properties as |innerProperty|}} - {{requestBodyVar}}.get{{asJavaTypeName parentPropertyName}}().get(0).set{{asJavaTypeName @key}}(null); + {{requestBodyVar}}.get{{asJavaTypeName parentPropertyName}}().get(0).set{{asJavaTypeName @key}}({{{populateProperty innerProperty}}}); {{~/each}} {{~else}} {{~/if}} diff --git a/plugins/openapi-spring-webtestclient/src/test/resources/pom.xml b/plugins/openapi-spring-webtestclient/src/test/resources/pom.xml index 89efcfd2..0450ef9b 100644 --- a/plugins/openapi-spring-webtestclient/src/test/resources/pom.xml +++ b/plugins/openapi-spring-webtestclient/src/test/resources/pom.xml @@ -131,6 +131,9 @@ ApiUtil.java + + Double=java.math.BigDecimal + none false diff --git a/zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/templating/CustomHandlebarsHelpers.java b/zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/templating/CustomHandlebarsHelpers.java index 613e7785..5296932b 100644 --- a/zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/templating/CustomHandlebarsHelpers.java +++ b/zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/templating/CustomHandlebarsHelpers.java @@ -4,6 +4,7 @@ import java.util.*; import java.util.stream.Collectors; +import io.zenwave360.sdk.parsers.JDLParser; import io.zenwave360.sdk.utils.JSONPath; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; @@ -12,8 +13,46 @@ import io.zenwave360.sdk.utils.NamingUtils; +import static org.apache.commons.lang3.StringUtils.capitalize; + public class CustomHandlebarsHelpers { + public static String populateProperty(Map property, Options options) { + String type = (String) property.get("type"); + String format = (String) property.get("format"); + if ("date".equals(format)) { + return "new Date()"; + } + if ("date-time".equals(format)) { + return "Instant.now()"; + } + if ("integer".equals(type) && "int32".equals(format)) { + return "1"; + } + if ("integer".equals(type) && "int64".equals(format)) { + return "1L"; + } + if ("number".equals(type)) { + return "BigDecimal.valueOf(0)"; + } + if ("boolean".equals(type)) { + return "true"; + } + if ("array".equals(type)) { + var items = (Map) property.get("items"); + var propertyName = (String) property.get("x--property-name"); + return "null"; + } + if (property.get("x--schema-name") != null) { + // root level #/component/schemas would be an entity or enum + String otherEntity = (String) property.get("x--schema-name"); + String propertyName = (String) property.get("x--property-name"); + return "new " + otherEntity + "()"; + } + return "\"aaa\""; + } + + public static Object jsonPath(Object entity, Options options) throws IOException { String jsonPath = StringUtils.join(options.params, ""); Object defaultValue = options.hash.get("default");