Skip to content

Commit

Permalink
Fixes #200 Fixes #180 - added model generation only and changed name …
Browse files Browse the repository at this point in the history
…for code regeneration only
  • Loading branch information
ddobrin committed Jan 8, 2019
1 parent 7572d3a commit 6646c3d
Showing 1 changed file with 70 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@
*/
public class OpenApiGenerator implements Generator {
private Map<String, String> typeMapping = new HashMap<>();

// optional generation parameters. if not set, they use default values as
boolean prometheusMetrics =false;
boolean skipHealthCheck = false;
boolean skipServerInfo = false;
boolean specChangeCodeReGenOnly = false;
boolean regenerateCodeOnly = false;
boolean enableParamDescription = true;
boolean generateModelOnly = false;

public OpenApiGenerator() {
typeMapping.put("array", "java.util.List");
Expand Down Expand Up @@ -80,76 +83,88 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
String rootPackage = config.toString("rootPackage");
String modelPackage = config.toString("modelPackage");
String handlerPackage = config.toString("handlerPackage");

boolean overwriteHandler = config.toBoolean("overwriteHandler");
boolean overwriteHandlerTest = config.toBoolean("overwriteHandlerTest");
boolean overwriteModel = config.toBoolean("overwriteModel");
generateModelOnly = config.toBoolean("generateModelOnly");

boolean enableHttp = config.toBoolean("enableHttp");
String httpPort = config.toString("httpPort");
boolean enableHttps = config.toBoolean("enableHttps");
String httpsPort = config.toString("httpsPort");

boolean enableRegistry = config.toBoolean("enableRegistry");
boolean supportClient = config.toBoolean("supportClient");
String dockerOrganization = config.toString("dockerOrganization");

prometheusMetrics = config.toBoolean("prometheusMetrics");
skipHealthCheck = config.toBoolean("skipHealthCheck");
skipServerInfo = config.toBoolean("skipServerInfo");
specChangeCodeReGenOnly = config.toBoolean("specChangeCodeReGenOnly");
regenerateCodeOnly = config.toBoolean("specChangeCodeReGenOnly");
enableParamDescription = config.toBoolean("enableParamDescription");

String version = config.toString("version");
String serviceId = config.get("groupId") + "." + config.get("artifactId") + "-" + config.get("version");

if(dockerOrganization == null || dockerOrganization.length() == 0) dockerOrganization = "networknt";

if (!specChangeCodeReGenOnly) {
transfer(targetPath, "", "pom.xml", templates.rest.openapi.pom.template(config));
// There is only one port that should be exposed in Dockerfile, otherwise, the service
// discovery will be so confused. If https is enabled, expose the https port. Otherwise http port.
String expose = "";
if(enableHttps) {
expose = httpsPort;
} else {
expose = httpPort;
}

transfer(targetPath, "docker", "Dockerfile", templates.rest.dockerfile.template(config, expose));
transfer(targetPath, "docker", "Dockerfile-Redhat", templates.rest.dockerfileredhat.template(config, expose));
transfer(targetPath, "", "build.sh", templates.rest.buildSh.template(dockerOrganization, serviceId));
transfer(targetPath, "", ".gitignore", templates.rest.gitignore.template());
transfer(targetPath, "", "README.md", templates.rest.README.template());
transfer(targetPath, "", "LICENSE", templates.rest.LICENSE.template());
transfer(targetPath, "", ".classpath", templates.rest.classpath.template());
transfer(targetPath, "", ".project", templates.rest.project.template(config));

// config
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "service.yml", templates.rest.openapi.service.template(config));

transfer(targetPath, ("src.main.resources.config").replace(".", separator), "server.yml", templates.rest.server.template(serviceId, enableHttp, httpPort, enableHttps, httpsPort, enableRegistry, version));
transfer(targetPath, ("src.test.resources.config").replace(".", separator), "server.yml", templates.rest.server.template(serviceId, enableHttp, "49587", enableHttps, "49588", enableRegistry, version));

transfer(targetPath, ("src.main.resources.config").replace(".", separator), "secret.yml", templates.rest.secret.template());
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "openapi-security.yml", templates.rest.openapiSecurity.template());
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "openapi-validator.yml", templates.rest.openapiValidator.template());
if(supportClient) {
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "client.yml", templates.rest.clientYml.template());
} else {
transfer(targetPath, ("src.test.resources.config").replace(".", separator), "client.yml", templates.rest.clientYml.template());
}

transfer(targetPath, ("src.main.resources.config").replace(".", separator), "primary.crt", templates.rest.primaryCrt.template());
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "secondary.crt", templates.rest.secondaryCrt.template());

// mask
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "mask.yml", templates.rest.maskYml.template());
// logging
transfer(targetPath, ("src.main.resources").replace(".", separator), "logback.xml", templates.rest.logback.template());
transfer(targetPath, ("src.test.resources").replace(".", separator), "logback-test.xml", templates.rest.logback.template());
}

// get the list of operations for this model
List<Map<String, Object>> operationList = getOperationList(model);
// routing
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "handler.yml", templates.rest.openapi.handlerYml.template(serviceId, handlerPackage, operationList, prometheusMetrics, !skipHealthCheck, !skipServerInfo));


// bypass project generation if the mode is the only one requested to be built
if(!generateModelOnly) {
// if set to true, regenerate the code only (handlers, model and the handler.yml, potentially affected by operation changes
if (!regenerateCodeOnly) {
// generate configurations, project, masks, certs, etc
transfer(targetPath, "", "pom.xml", templates.rest.openapi.pom.template(config));
// There is only one port that should be exposed in Dockerfile, otherwise, the service
// discovery will be so confused. If https is enabled, expose the https port. Otherwise http port.
String expose = "";
if(enableHttps) {
expose = httpsPort;
} else {
expose = httpPort;
}

transfer(targetPath, "docker", "Dockerfile", templates.rest.dockerfile.template(config, expose));
transfer(targetPath, "docker", "Dockerfile-Redhat", templates.rest.dockerfileredhat.template(config, expose));
transfer(targetPath, "", "build.sh", templates.rest.buildSh.template(dockerOrganization, serviceId));
transfer(targetPath, "", ".gitignore", templates.rest.gitignore.template());
transfer(targetPath, "", "README.md", templates.rest.README.template());
transfer(targetPath, "", "LICENSE", templates.rest.LICENSE.template());
transfer(targetPath, "", ".classpath", templates.rest.classpath.template());
transfer(targetPath, "", ".project", templates.rest.project.template(config));

// config
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "service.yml", templates.rest.openapi.service.template(config));

transfer(targetPath, ("src.main.resources.config").replace(".", separator), "server.yml", templates.rest.server.template(serviceId, enableHttp, httpPort, enableHttps, httpsPort, enableRegistry, version));
transfer(targetPath, ("src.test.resources.config").replace(".", separator), "server.yml", templates.rest.server.template(serviceId, enableHttp, "49587", enableHttps, "49588", enableRegistry, version));

transfer(targetPath, ("src.main.resources.config").replace(".", separator), "secret.yml", templates.rest.secret.template());
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "openapi-security.yml", templates.rest.openapiSecurity.template());
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "openapi-validator.yml", templates.rest.openapiValidator.template());
if(supportClient) {
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "client.yml", templates.rest.clientYml.template());
} else {
transfer(targetPath, ("src.test.resources.config").replace(".", separator), "client.yml", templates.rest.clientYml.template());
}

transfer(targetPath, ("src.main.resources.config").replace(".", separator), "primary.crt", templates.rest.primaryCrt.template());
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "secondary.crt", templates.rest.secondaryCrt.template());

// mask
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "mask.yml", templates.rest.maskYml.template());
// logging
transfer(targetPath, ("src.main.resources").replace(".", separator), "logback.xml", templates.rest.logback.template());
transfer(targetPath, ("src.test.resources").replace(".", separator), "logback-test.xml", templates.rest.logback.template());

// routing handler
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "handler.yml", templates.rest.openapi.handlerYml.template(serviceId, handlerPackage, operationList, prometheusMetrics, !skipHealthCheck, !skipServerInfo));
}
}

// model
Any anyComponents;
if(model instanceof Any) {
Expand Down Expand Up @@ -177,7 +192,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
String enums = null;
boolean isEnum = false;
boolean isEnumClass = false;
Map<String, Any> properties = null;
// Map<String, Any> properties = null;
List<Any> required = null;

// iterate through each schema in the components
Expand Down Expand Up @@ -238,6 +253,10 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
}
}

// exit after generating the model if the consumer needs only the model classes
if(generateModelOnly)
return;

// handler
for(Map<String, Object> op : operationList){
String className = op.get("handlerName").toString();
Expand Down

0 comments on commit 6646c3d

Please sign in to comment.