Skip to content

Commit

Permalink
[JavaVertx]Add code gen server Draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyro-Ing committed May 20, 2022
1 parent 56bf094 commit 48b3fc5
Show file tree
Hide file tree
Showing 21 changed files with 2,285 additions and 0 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ io.swagger.codegen.v3.generators.java.JavaJerseyServerCodegen
io.swagger.codegen.v3.generators.java.JavaJerseyDIServerCodegen
io.swagger.codegen.v3.generators.java.JavaResteasyEapServerCodegen
io.swagger.codegen.v3.generators.java.JavaResteasyServerCodegen
io.swagger.codegen.v3.generators.java.JavaVertXServerCodegen
io.swagger.codegen.v3.generators.java.MicronautCodegen
io.swagger.codegen.v3.generators.java.SpringCodegen
io.swagger.codegen.v3.generators.nodejs.NodeJSServerCodegen
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package {{modelPackage}};

{{#java8}}
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
{{/java8}}
{{#threetenbp}}
import org.threeten.bp.Instant;
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.ZonedDateTime;
{{/threetenbp}}
{{#joda}}
import org.joda.time.LocalDate;
import org.joda.time.DateTime;
{{/joda}}

public class DataObjectMapper {
{{#java8}}
public static String serializeOffsetDateTime(OffsetDateTime value) {
return value.toString();
}

public static String serializeLocalDateTime(LocalDateTime value) {
return value.toString();
}

public static String serializeLocalDate(LocalDate value) {
return value.toString();
}
{{/java8}}
{{#threetenbp}}
public static String serializeThreetenbpInstant(org.threeten.bp.Instant value) {
return value.toString();
}

public static String serializeThreetenbpOffsetDateTime(org.threeten.bp.OffsetDateTime value) {
return value.toString();
}

public static String serializeThreetenbpZonedDateTime(org.threeten.bp.ZonedDateTime value) {
return value.toString();
}
{{/threetenbp}}
{{#joda}}
public static String serializeJodaLocalDate(org.joda.time.LocalDate value) {
return value.toString();
}

public static String serializeJodaDateTime(org.joda.time.DateTime value) {
return value.toString();
}
{{/joda}}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package {{rootPackage}};

{{#rxInterface}}
import io.reactivex.Completable;
import io.vertx.reactivex.core.AbstractVerticle;
{{/rxInterface}}
{{^rxInterface}}
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
{{/rxInterface}}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MainApiVerticle extends AbstractVerticle {
static final Logger LOGGER = LoggerFactory.getLogger(MainApiVerticle.class);
@Override
{{^rxInterface}}public void start(Promise<Void> startPromise) throws Exception {
vertx.deployVerticle("{{verticlePackage}}.{{title}}Verticle")
.onFailure(error -> {
LOGGER.error("{{title}}Verticle : Deployment failed");
startPromise.fail(error);
})
.onSuccess(server -> {
LOGGER.info("{{title}}Verticle : Deployed");
startPromise.complete();
});
}{{/rxInterface}}
{{#rxInterface}}public Completable rxStart() {
return vertx.rxDeployVerticle("{{verticlePackage}}.{{title}}Verticle")
.doOnError(error -> LOGGER.error("{{title}}Verticle : Deployment failed"))
.doOnSuccess(server -> LOGGER.info("{{title}}Verticle : Deployed"))
.ignoreElement();
}{{/rxInterface}}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Project generated on : {{generatedDate}}
31 changes: 31 additions & 0 deletions src/main/resources/handlebars/JavaVertXServer/api.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package {{package}};

{{#imports}}import {{import}};
{{/imports}}

import io.vertx.core.Future;
import io.vertx.ext.web.validation.RequestParameter;
import io.vertx.ext.web.api.service.ServiceRequest;
import io.vertx.ext.web.api.service.ServiceResponse;
{{#mountFromExtensions}}
import io.vertx.ext.web.api.service.WebApiServiceGen;
{{/mountFromExtensions}}

import java.util.List;
import java.util.Map;

{{#mountFromExtensions}}@WebApiServiceGen{{/mountFromExtensions}}
public interface {{classname}} {
{{#operations}}{{#operation}}{{#@first}} String WEBSERVICE_ADDRESS_{{#lambda.uppercase}}{{classname}}{{/lambda.uppercase}} = "{{#mountFromExtensions}}{{#vendorExtensions}}{{x-event-bus-address}}{{/vendorExtensions}}{{/mountFromExtensions}}{{#mountFromInterface}}{{baseName}}.address{{/mountFromInterface}}";{{/@first}}{{/operation}}{{/operations}}
{{#operations}}{{#operation}} String OPERATION_ID_{{#lambda.uppercase}}{{operationId}}{{/lambda.uppercase}} = "{{operationId}}";
{{/operation}}{{/operations}}

{{#operations}}
{{#operation}}
{{#contents}}
Future<ServiceResponse> {{#vendorExtensions}}{{x-serviceid}}{{/vendorExtensions}}({{#useDataObject}}{{#parameters}}{{^isBodyParam}}{{^isEnum}}{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{dataType}}}{{/datatypeWithEnum}}{{/isEnum}}{{#isEnum}}{{{dataType}}}{{/isEnum}} {{paramName}}{{/isBodyParam}}{{#isBodyParam}}{{^isBinary}}{{^parent}}{{^children}}{{{dataType}}} body{{/children}}{{/parent}}{{/isBinary}}{{/isBodyParam}}{{#hasMore}}{{^isBinary}}, {{/isBinary}}{{/hasMore}}{{^hasMore}}{{^isBinary}}, {{/isBinary}}{{/hasMore}}{{/parameters}}{{/useDataObject}}{{^useDataObject}}RequestParameter body, {{/useDataObject}}ServiceRequest request);
{{/contents}}
{{/operation}}
{{/operations}}
}
94 changes: 94 additions & 0 deletions src/main/resources/handlebars/JavaVertXServer/apiVerticle.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package {{verticlePackage}};

{{#mountFromInterface}}{{#apiInfo}}{{#apis}}import {{{package}}}.{{{classFilename}}};
import static {{{package}}}.{{{classFilename}}}.WEBSERVICE_ADDRESS_{{#lambda.uppercase}}{{{classFilename}}}{{/lambda.uppercase}};
{{/apis}}{{/apiInfo}}{{/mountFromInterface}}

{{#rxInterface}}
import io.reactivex.Completable;
import io.vertx.reactivex.core.AbstractVerticle;
import io.vertx.reactivex.core.eventbus.MessageConsumer;
import io.vertx.reactivex.core.http.HttpServer;
import io.vertx.reactivex.ext.web.Router;
import io.vertx.reactivex.ext.web.openapi.RouterBuilder;
{{/rxInterface}}
{{^rxInterface}}
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.openapi.RouterBuilder;
{{/rxInterface}}
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.openapi.RouterBuilderOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class {{title}}Verticle extends AbstractVerticle {
static final Logger LOGGER = LoggerFactory.getLogger({{title}}Verticle.class);

MessageConsumer<JsonObject> consumer;
HttpServer server;

@Override
{{^rxInterface}}public void start(Promise<Void> startPromise) throws Exception {
RouterBuilder.create(this.vertx, "{{specLocation}}")
.flatMap(routerBuilder -> { {{/rxInterface}}
{{#rxInterface}}public Completable rxStart() {
return RouterBuilder.rxCreate(this.vertx, "{{specLocation}}")
.map(routerBuilder -> { {{/rxInterface}}
RouterBuilderOptions factoryOptions = new RouterBuilderOptions()
.setRequireSecurityHandlers(false)
.setMountResponseContentTypeHandler(true);
routerBuilder.setOptions(factoryOptions);

{{#mountFromExtensions}} routerBuilder.mountServicesFromExtensions();{{/mountFromExtensions}}
{{#mountFromInterface}}{{#apiInfo}}{{#apis}} routerBuilder.mountServiceInterface({{{classFilename}}}.class, WEBSERVICE_ADDRESS_{{#lambda.uppercase}}{{{classFilename}}}{{/lambda.uppercase}});
{{/apis}}{{/apiInfo}}{{/mountFromInterface}}

return {{#rxInterface}}routerBuilder.createRouter(){{/rxInterface}}{{^rxInterface}}Future.succeededFuture(routerBuilder.createRouter()){{/rxInterface}};
})
.flatMap(openapiRouter -> {
Router router = Router.router(vertx);
server = vertx.createHttpServer(new HttpServerOptions().setPort({{serverPort}}).setHost("localhost"))
.requestHandler(router);

router.mountSubRouter("/", openapiRouter);

router.route().last().handler(context ->
context.response()
.setStatusCode(404)
.end(new JsonObject()
.put("message", "Resource not found")
.encode())
);

{{#rxInterface}}return server.rxListen()
.doOnSuccess(server -> LOGGER.info("SwaggerPetstoreVerticle started on port " + server.actualPort()));
})
.ignoreElement();
}

@Override
public Completable rxStop() {
return this.consumer.rxUnregister()
.andThen(this.server.rxClose());
}{{/rxInterface}}
{{^rxInterface}}return server.listen()
.onSuccess(server -> LOGGER.info("{{title}}Verticle started on port " + server.actualPort()));
})
.onSuccess(server -> startPromise.complete())
.onFailure(startPromise::fail);
}

@Override
public void stop() {
this.server.close();
this.consumer.unregister();
}{{/rxInterface}}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{#required}}
@NotNull
{{/required}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}}
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}}
@Valid{{/isPrimitiveType}}{{/isNotContainer}}
{{>beanValidationCore}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{#pattern}}@Pattern(regexp="{{{pattern}}}"{{#vendorExtensions.x-pattern-message}}, message="{{vendorExtensions.x-pattern-message}}"{{/vendorExtensions.x-pattern-message}}) {{/pattern}}{{!
minLength && maxLength set
}}{{#minLength}}{{#maxLength}}@Size(min={{minLength}},max={{maxLength}}) {{/maxLength}}{{/minLength}}{{!
minLength set, maxLength not
}}{{#minLength}}{{^maxLength}}@Size(min={{minLength}}) {{/maxLength}}{{/minLength}}{{!
minLength not set, maxLength set
}}{{^minLength}}{{#maxLength}}@Size(max={{maxLength}}) {{/maxLength}}{{/minLength}}{{!
@Size: minItems && maxItems set
}}{{#minItems}}{{#maxItems}}@Size(min={{minItems}},max={{maxItems}}) {{/maxItems}}{{/minItems}}{{!
@Size: minItems set, maxItems not
}}{{#minItems}}{{^maxItems}}@Size(min={{minItems}}) {{/maxItems}}{{/minItems}}{{!
@Size: minItems not set && maxItems set
}}{{^minItems}}{{#maxItems}}@Size(max={{maxItems}}) {{/maxItems}}{{/minItems}}{{!
check for integer or long / all others=decimal type with @Decimal*
isInteger set
}}{{#isInteger}}{{#minimum}}@Min({{minimum}}){{/minimum}}{{#maximum}} @Max({{maximum}}) {{/maximum}}{{/isInteger}}{{!
isLong set
}}{{#isLong}}{{#minimum}}@Min({{minimum}}L){{/minimum}}{{#maximum}} @Max({{maximum}}L) {{/maximum}}{{/isLong}}{{!
Not Integer, not Long => we have a decimal value!
}}{{^isInteger}}{{^isLong}}{{#minimum}}@DecimalMin("{{minimum}}"){{/minimum}}{{#maximum}} @DecimalMax("{{maximum}}") {{/maximum}}{{/isLong}}{{/isInteger}}
35 changes: 35 additions & 0 deletions src/main/resources/handlebars/JavaVertXServer/enumClass.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
*/
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} {
{{#allowableValues}}{{#enumVars}}{{{name}}}({{{value}}}){{^@last}},
{{/@last}}{{#@last}};{{/@last}}{{/enumVars}}{{/allowableValues}}

private {{{datatype}}} value;

{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}({{{datatype}}} value) {
this.value = value;
}

public {{{datatype}}} getValue() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue({{{datatype}}} value) {
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
if (b.value.equals(value)) {
return b;
}
}
{{^errorOnUnknownEnum}}return null;{{/errorOnUnknownEnum}}{{#errorOnUnknownEnum}}throw new IllegalArgumentException("Unexpected value '" + value + "' for '{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}' enum.");{{/errorOnUnknownEnum}}
}
{{#useDataObject}}
public static {{{datatype}}} serialize({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} enumValue) {
return enumValue.getValue();
}{{/useDataObject}}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{description}}{{/description}}
*/
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} {
{{#allowableValues}}{{#enumVars}}{{{name}}}({{{value}}}){{^@last}},
{{/@last}}{{#@last}};{{/@last}}{{/enumVars}}{{/allowableValues}}

private {{{dataType}}} value;

{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}({{{dataType}}} value) {
this.value = value;
}

public {{{dataType}}} getValue() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue({{{dataType}}} value) {
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
if (b.value.equals(value)) {
return b;
}
}
{{^errorOnUnknownEnum}}return null;{{/errorOnUnknownEnum}}{{#errorOnUnknownEnum}}throw new IllegalArgumentException("Unexpected value '" + value + "' for '{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}' enum.");{{/errorOnUnknownEnum}}
}
{{#useDataObject}}
public static {{{dataType}}} serialize({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} enumValue) {
return enumValue.getValue();
}{{/useDataObject}}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{^hideGenerationTimestamp}}
@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}")
{{/hideGenerationTimestamp}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{#models}}
{{#model}}
{{#isEnum}}
{{modelPackage}}.{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.serializer={{modelPackage}}.{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}#serialize
{{modelPackage}}.{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.deserializer={{modelPackage}}.{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}#fromValue
{{/isEnum}}
{{^isEnum}}
{{#vars}}
{{#isEnum}}
{{modelPackage}}.{{classname}}.{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.serializer={{modelPackage}}.{{classname}}.{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}#serialize
{{modelPackage}}.{{classname}}.{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.deserializer={{modelPackage}}.{{classname}}.{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}#fromValue
{{/isEnum}}
{{/vars}}
{{/isEnum}}
{{/model}}
{{/models}}
{{#java8}}
java.time.LocalDate.serializer={{modelPackage}}.DataObjectMapper#serializeLocalDate
java.time.LocalDate.deserializer=java.time.LocalDate#parse
java.time.LocalDateTime.serializer={{modelPackage}}.DataObjectMapper#serializeLocalDateTime
java.time.LocalDateTime.deserializer=java.time.LocalDateTime#parse
java.time.OffsetDateTime.serializer={{modelPackage}}.DataObjectMapper#serializeOffsetDateTime
java.time.OffsetDateTime.deserializer=java.time.OffsetDateTime#parse
{{/java8}}
{{#threetenbp}}
org.threeten.bp.Instant.serializer={{modelPackage}}.DataObjectMapper#serializeThreetenbpInstant
org.threeten.bp.Instant.deserializer=org.threeten.bp.Instant#parse
org.threeten.bp.OffsetDateTime.serializer={{modelPackage}}.DataObjectMapper#serializeThreetenbpOffsetDateTime
org.threeten.bp.OffsetDateTime.deserializer=org.threeten.bp.OffsetDateTime#parse
org.threeten.bp.ZonedDateTime.serializer={{modelPackage}}.DataObjectMapper#serializeThreetenbpZonedDateTime
org.threeten.bp.ZonedDateTime.deserializer=org.threeten.bp.ZonedDateTime#parse
{{/threetenbp}}
{{#joda}}
org.joda.time.LocalDate.serializer={{modelPackage}}.DataObjectMapper#serializeJodaLocalDate
org.joda.time.LocalDate.deserializer=org.joda.time.LocalDate#parse
org.joda.time.DateTime.serializer={{modelPackage}}.DataObjectMapper#serializeJodaDateTime
org.joda.time.DateTime.deserializer=org.joda.time.DateTime#parse
{{/joda}}
Loading

0 comments on commit 48b3fc5

Please sign in to comment.