org.openapitools.codegen.languages.JavaVertXServerCodegen Maven / Gradle / Ivy
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openapitools.codegen.languages;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.PathItem.HttpMethod;
import io.swagger.v3.oas.models.media.Schema;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.utils.URLPathUtils;
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaVertXServerCodegen extends AbstractJavaCodegen {
protected String resourceFolder = "src/main/resources";
protected String rootPackage = "org.openapitools.server.api";
protected String apiVersion = "1.0.0-SNAPSHOT";
public static final String ROOT_PACKAGE = "rootPackage";
public static final String RX_INTERFACE_OPTION = "rxInterface";
public static final String VERTX_SWAGGER_ROUTER_VERSION_OPTION = "vertxSwaggerRouterVersion";
/**
* A Java Vert.X generator. It uses java8 date API. It can be configured with 2 CLI options :
*
* rxInterface : type Boolean if true, API interfaces are generated with RX and methods return
* Single and Comparable. default : false
*
* vertxSwaggerRouterVersion : type String Specify the version of the swagger router library
*/
public JavaVertXServerCodegen() {
super();
// set the output folder here
outputFolder = "generated-code" + File.separator + "javaVertXServer";
modelTemplateFiles.clear();
modelTemplateFiles.put("model.mustache", ".java");
apiTemplateFiles.clear();
apiTemplateFiles.put("api.mustache", ".java");
apiTemplateFiles.put("apiVerticle.mustache", "Verticle.java");
apiTemplateFiles.put("apiException.mustache", "Exception.java");
embeddedTemplateDir = templateDir = "JavaVertXServer";
apiPackage = rootPackage + ".verticle";
modelPackage = rootPackage + ".model";
additionalProperties.put(ROOT_PACKAGE, rootPackage);
groupId = "org.openapitools";
artifactId = "openapi-java-vertx-server";
artifactVersion = apiVersion;
this.setDateLibrary("java8");
cliOptions.add(CliOption.newBoolean(RX_INTERFACE_OPTION,
"When specified, API interfaces are generated with RX "
+ "and methods return Single<> and Comparable."));
cliOptions.add(CliOption.newString(VERTX_SWAGGER_ROUTER_VERSION_OPTION,
"Specify the version of the swagger router library"));
}
/**
* Configures the type of generator.
*
* @return the CodegenType for this generator
* @see org.openapitools.codegen.CodegenType
*/
public CodegenType getTag() {
return CodegenType.SERVER;
}
/**
* Configures a friendly name for the generator. This will be used by the generator to select
* the library with the -g flag.
*
* @return the friendly name for the generator
*/
public String getName() {
return "java-vertx";
}
/**
* Returns human-friendly help for the generator. Provide the consumer with help tips,
* parameters here
*
* @return A string value for the help message
*/
public String getHelp() {
return "Generates a java-Vert.X Server library.";
}
@Override
public void processOpts() {
super.processOpts();
apiTestTemplateFiles.clear();
importMapping.remove("JsonCreator");
importMapping.remove("com.fasterxml.jackson.annotation.JsonProperty");
importMapping.put("JsonInclude", "com.fasterxml.jackson.annotation.JsonInclude");
importMapping.put("JsonProperty", "com.fasterxml.jackson.annotation.JsonProperty");
importMapping.put("JsonValue", "com.fasterxml.jackson.annotation.JsonValue");
importMapping.put("MainApiException", rootPackage + ".MainApiException");
modelDocTemplateFiles.clear();
apiDocTemplateFiles.clear();
supportingFiles.clear();
supportingFiles.add(new SupportingFile("openapi.mustache", resourceFolder, "openapi.json"));
supportingFiles.add(new SupportingFile("MainApiVerticle.mustache",
sourceFolder + File.separator + rootPackage.replace(".", File.separator),
"MainApiVerticle.java"));
supportingFiles.add(new SupportingFile("MainApiException.mustache",
sourceFolder + File.separator + rootPackage.replace(".", File.separator),
"MainApiException.java"));
writeOptional(outputFolder, new SupportingFile("vertx-default-jul-logging.mustache",
resourceFolder, "vertx-default-jul-logging.properties"));
writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));
writeOptional(outputFolder, new SupportingFile("README.mustache", "", "README.md"));
}
@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property);
if (!model.isEnum) {
model.imports.add("JsonInclude");
model.imports.add("JsonProperty");
if (model.hasEnums) {
model.imports.add("JsonValue");
}
}
}
@Override
public Map postProcessOperationsWithModels(Map objs, List