Skip to content

Commit

Permalink
[Java][library: vertx] Add default value and required parameter suppo…
Browse files Browse the repository at this point in the history
…rt to vertx server temp… (#7410)

* Add default value and required parameter support to vertx server templates. Fix for #7409.

* Added missing serviceId declaration to workaround #allParams section clearing the vendorExtensions map
  • Loading branch information
ccozzolino authored and wing328 committed Jan 20, 2018
1 parent ef832e7 commit 59ff4c1
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,61 @@ public class {{classname}}Verticle extends AbstractVerticle {
//Consumer for {{#vendorExtensions}}{{x-serviceid}}{{/vendorExtensions}}
vertx.eventBus().<JsonObject> consumer({{#vendorExtensions}}{{x-serviceid-varname}}{{/vendorExtensions}}).handler(message -> {
try {
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "{{#vendorExtensions}}{{x-serviceid}}{{/vendorExtensions}}";
{{#hasParams}}
{{#allParams}}
{{#isListContainer}}
{{{dataType}}} {{paramName}} = Json.mapper.readValue(message.body().getJsonArray("{{baseName}}").encode(),
Json.mapper.getTypeFactory().constructCollectionType(List.class, {{{baseType}}}.class));
JsonArray {{paramName}}Param = message.body().getJsonArray("{{baseName}}");
{{#required}}
if({{paramName}}Param == null) {
manageError(message, new MainApiException(400, "{{baseName}} is required"), serviceId);
return;
}
{{{dataType}}} {{paramName}} = Json.mapper.readValue({{paramName}}Param.encode(),
Json.mapper.getTypeFactory().constructCollectionType(List.class, {{{baseType}}}.class));
{{/required}}
{{^required}}
{{{dataType}}} {{paramName}} = ({{paramName}}Param == null) ? {{#defaultValue}}{{defaultValue}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}} : Json.mapper.readValue({{paramName}}Param.encode(),
Json.mapper.getTypeFactory().constructCollectionType(List.class, {{{baseType}}}.class));
{{/required}}
{{/isListContainer}}
{{^isListContainer}}
{{#isPrimitiveType}}
{{#isString}}
{{{dataType}}} {{paramName}} = message.body().getString("{{baseName}}");
String {{paramName}}Param = message.body().getString("{{baseName}}");
{{#required}}
if({{paramName}}Param == null) {
manageError(message, new MainApiException(400, "{{baseName}} is required"), serviceId);
return;
}
{{{dataType}}} {{paramName}} = {{paramName}}Param;
{{/required}}
{{^required}}
{{{dataType}}} {{paramName}} = ({{paramName}}Param == null) ? {{#defaultValue}}{{defaultValue}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}} : {{paramName}}Param;
{{/required}}
{{/isString}}
{{^isString}}
{{{dataType}}} {{paramName}} = Json.mapper.readValue(message.body().getString("{{baseName}}"), {{{dataType}}}.class);
String {{paramName}}Param = message.body().getString("{{baseName}}");
{{#required}}
if({{paramName}}Param == null) {
manageError(message, new MainApiException(400, "{{baseName}} is required"), serviceId);
return;
}
{{{dataType}}} {{paramName}} = Json.mapper.readValue({{paramName}}Param, {{{dataType}}}.class);
{{/required}}
{{^required}}
{{{dataType}}} {{paramName}} = ({{paramName}}Param == null) ? {{#defaultValue}}{{defaultValue}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}} : Json.mapper.readValue({{paramName}}Param, {{{dataType}}}.class);
{{/required}}
{{/isString}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{{dataType}}} {{paramName}} = Json.mapper.readValue(message.body().getJsonObject("{{baseName}}").encode(), {{{dataType}}}.class);
JsonObject {{paramName}}Param = message.body().getJsonObject("{{baseName}}");
if ({{paramName}}Param == null) {
manageError(message, new MainApiException(400, "{{baseName}} is required"), serviceId);
return;
}
{{{dataType}}} {{paramName}} = Json.mapper.readValue({{paramName}}Param.encode(), {{{dataType}}}.class);
{{/isPrimitiveType}}
{{/isListContainer}}
{{/allParams}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.1-SNAPSHOT
2.3.1
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ public void start() throws Exception {
//Consumer for addPet
vertx.eventBus().<JsonObject> consumer(ADDPET_SERVICE_ID).handler(message -> {
try {
Pet body = Json.mapper.readValue(message.body().getJsonObject("body").encode(), Pet.class);
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "addPet";
JsonObject bodyParam = message.body().getJsonObject("body");
if (bodyParam == null) {
manageError(message, new MainApiException(400, "body is required"), serviceId);
return;
}
Pet body = Json.mapper.readValue(bodyParam.encode(), Pet.class);
service.addPet(body, result -> {
if (result.succeeded())
message.reply(null);
Expand All @@ -64,8 +71,16 @@ public void start() throws Exception {
//Consumer for deletePet
vertx.eventBus().<JsonObject> consumer(DELETEPET_SERVICE_ID).handler(message -> {
try {
Long petId = Json.mapper.readValue(message.body().getString("petId"), Long.class);
String apiKey = message.body().getString("api_key");
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "deletePet";
String petIdParam = message.body().getString("petId");
if(petIdParam == null) {
manageError(message, new MainApiException(400, "petId is required"), serviceId);
return;
}
Long petId = Json.mapper.readValue(petIdParam, Long.class);
String apiKeyParam = message.body().getString("api_key");
String apiKey = (apiKeyParam == null) ? null : apiKeyParam;
service.deletePet(petId, apiKey, result -> {
if (result.succeeded())
message.reply(null);
Expand All @@ -83,8 +98,15 @@ public void start() throws Exception {
//Consumer for findPetsByStatus
vertx.eventBus().<JsonObject> consumer(FINDPETSBYSTATUS_SERVICE_ID).handler(message -> {
try {
List<String> status = Json.mapper.readValue(message.body().getJsonArray("status").encode(),
Json.mapper.getTypeFactory().constructCollectionType(List.class, String.class));
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "findPetsByStatus";
JsonArray statusParam = message.body().getJsonArray("status");
if(statusParam == null) {
manageError(message, new MainApiException(400, "status is required"), serviceId);
return;
}
List<String> status = Json.mapper.readValue(statusParam.encode(),
Json.mapper.getTypeFactory().constructCollectionType(List.class, String.class));
service.findPetsByStatus(status, result -> {
if (result.succeeded())
message.reply(new JsonArray(Json.encode(result.result())).encodePrettily());
Expand All @@ -102,8 +124,15 @@ public void start() throws Exception {
//Consumer for findPetsByTags
vertx.eventBus().<JsonObject> consumer(FINDPETSBYTAGS_SERVICE_ID).handler(message -> {
try {
List<String> tags = Json.mapper.readValue(message.body().getJsonArray("tags").encode(),
Json.mapper.getTypeFactory().constructCollectionType(List.class, String.class));
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "findPetsByTags";
JsonArray tagsParam = message.body().getJsonArray("tags");
if(tagsParam == null) {
manageError(message, new MainApiException(400, "tags is required"), serviceId);
return;
}
List<String> tags = Json.mapper.readValue(tagsParam.encode(),
Json.mapper.getTypeFactory().constructCollectionType(List.class, String.class));
service.findPetsByTags(tags, result -> {
if (result.succeeded())
message.reply(new JsonArray(Json.encode(result.result())).encodePrettily());
Expand All @@ -121,7 +150,14 @@ public void start() throws Exception {
//Consumer for getPetById
vertx.eventBus().<JsonObject> consumer(GETPETBYID_SERVICE_ID).handler(message -> {
try {
Long petId = Json.mapper.readValue(message.body().getString("petId"), Long.class);
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "getPetById";
String petIdParam = message.body().getString("petId");
if(petIdParam == null) {
manageError(message, new MainApiException(400, "petId is required"), serviceId);
return;
}
Long petId = Json.mapper.readValue(petIdParam, Long.class);
service.getPetById(petId, result -> {
if (result.succeeded())
message.reply(new JsonObject(Json.encode(result.result())).encodePrettily());
Expand All @@ -139,7 +175,14 @@ public void start() throws Exception {
//Consumer for updatePet
vertx.eventBus().<JsonObject> consumer(UPDATEPET_SERVICE_ID).handler(message -> {
try {
Pet body = Json.mapper.readValue(message.body().getJsonObject("body").encode(), Pet.class);
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "updatePet";
JsonObject bodyParam = message.body().getJsonObject("body");
if (bodyParam == null) {
manageError(message, new MainApiException(400, "body is required"), serviceId);
return;
}
Pet body = Json.mapper.readValue(bodyParam.encode(), Pet.class);
service.updatePet(body, result -> {
if (result.succeeded())
message.reply(null);
Expand All @@ -157,9 +200,18 @@ public void start() throws Exception {
//Consumer for updatePetWithForm
vertx.eventBus().<JsonObject> consumer(UPDATEPETWITHFORM_SERVICE_ID).handler(message -> {
try {
Long petId = Json.mapper.readValue(message.body().getString("petId"), Long.class);
String name = message.body().getString("name");
String status = message.body().getString("status");
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "updatePetWithForm";
String petIdParam = message.body().getString("petId");
if(petIdParam == null) {
manageError(message, new MainApiException(400, "petId is required"), serviceId);
return;
}
Long petId = Json.mapper.readValue(petIdParam, Long.class);
String nameParam = message.body().getString("name");
String name = (nameParam == null) ? null : nameParam;
String statusParam = message.body().getString("status");
String status = (statusParam == null) ? null : statusParam;
service.updatePetWithForm(petId, name, status, result -> {
if (result.succeeded())
message.reply(null);
Expand All @@ -177,9 +229,22 @@ public void start() throws Exception {
//Consumer for uploadFile
vertx.eventBus().<JsonObject> consumer(UPLOADFILE_SERVICE_ID).handler(message -> {
try {
Long petId = Json.mapper.readValue(message.body().getString("petId"), Long.class);
String additionalMetadata = message.body().getString("additionalMetadata");
File file = Json.mapper.readValue(message.body().getJsonObject("file").encode(), File.class);
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "uploadFile";
String petIdParam = message.body().getString("petId");
if(petIdParam == null) {
manageError(message, new MainApiException(400, "petId is required"), serviceId);
return;
}
Long petId = Json.mapper.readValue(petIdParam, Long.class);
String additionalMetadataParam = message.body().getString("additionalMetadata");
String additionalMetadata = (additionalMetadataParam == null) ? null : additionalMetadataParam;
JsonObject fileParam = message.body().getJsonObject("file");
if (fileParam == null) {
manageError(message, new MainApiException(400, "file is required"), serviceId);
return;
}
File file = Json.mapper.readValue(fileParam.encode(), File.class);
service.uploadFile(petId, additionalMetadata, file, result -> {
if (result.succeeded())
message.reply(new JsonObject(Json.encode(result.result())).encodePrettily());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ public void start() throws Exception {
//Consumer for deleteOrder
vertx.eventBus().<JsonObject> consumer(DELETEORDER_SERVICE_ID).handler(message -> {
try {
String orderId = message.body().getString("orderId");
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "deleteOrder";
String orderIdParam = message.body().getString("orderId");
if(orderIdParam == null) {
manageError(message, new MainApiException(400, "orderId is required"), serviceId);
return;
}
String orderId = orderIdParam;
service.deleteOrder(orderId, result -> {
if (result.succeeded())
message.reply(null);
Expand All @@ -58,6 +65,8 @@ public void start() throws Exception {
//Consumer for getInventory
vertx.eventBus().<JsonObject> consumer(GETINVENTORY_SERVICE_ID).handler(message -> {
try {
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "getInventory";
service.getInventory(result -> {
if (result.succeeded())
message.reply(new JsonObject(Json.encode(result.result())).encodePrettily());
Expand All @@ -75,7 +84,14 @@ public void start() throws Exception {
//Consumer for getOrderById
vertx.eventBus().<JsonObject> consumer(GETORDERBYID_SERVICE_ID).handler(message -> {
try {
Long orderId = Json.mapper.readValue(message.body().getString("orderId"), Long.class);
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "getOrderById";
String orderIdParam = message.body().getString("orderId");
if(orderIdParam == null) {
manageError(message, new MainApiException(400, "orderId is required"), serviceId);
return;
}
Long orderId = Json.mapper.readValue(orderIdParam, Long.class);
service.getOrderById(orderId, result -> {
if (result.succeeded())
message.reply(new JsonObject(Json.encode(result.result())).encodePrettily());
Expand All @@ -93,7 +109,14 @@ public void start() throws Exception {
//Consumer for placeOrder
vertx.eventBus().<JsonObject> consumer(PLACEORDER_SERVICE_ID).handler(message -> {
try {
Order body = Json.mapper.readValue(message.body().getJsonObject("body").encode(), Order.class);
// Workaround for #allParams section clearing the vendorExtensions map
String serviceId = "placeOrder";
JsonObject bodyParam = message.body().getJsonObject("body");
if (bodyParam == null) {
manageError(message, new MainApiException(400, "body is required"), serviceId);
return;
}
Order body = Json.mapper.readValue(bodyParam.encode(), Order.class);
service.placeOrder(body, result -> {
if (result.succeeded())
message.reply(new JsonObject(Json.encode(result.result())).encodePrettily());
Expand Down
Loading

0 comments on commit 59ff4c1

Please sign in to comment.