org.openapitools.codegen.languages.ScalaPlayFrameworkServerCodegen Maven / Gradle / Ivy
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
*
* 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
*
* https://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 com.google.common.collect.ImmutableMap.Builder;
import com.samskivert.mustache.Mustache.Lambda;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.features.*;
import org.openapitools.codegen.templating.mustache.IndentedLambda;
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static org.apache.commons.lang3.StringUtils.rightPad;
import static org.openapitools.codegen.utils.OnceLogger.once;
import static org.openapitools.codegen.utils.StringUtils.camelize;
public class ScalaPlayFrameworkServerCodegen extends AbstractScalaCodegen implements CodegenConfig {
public static final String TITLE = "title";
public static final String SKIP_STUBS = "skipStubs";
public static final String SUPPORT_ASYNC = "supportAsync";
public static final String GENERATE_CUSTOM_EXCEPTIONS = "generateCustomExceptions";
public static final String USE_SWAGGER_UI = "useSwaggerUI";
public static final String ROUTES_FILE_NAME = "routesFileName";
public static final String BASE_PACKAGE = "basePackage";
static Logger LOGGER = LoggerFactory.getLogger(ScalaPlayFrameworkServerCodegen.class);
protected boolean skipStubs = false;
protected boolean supportAsync = false;
protected boolean generateCustomExceptions = true;
protected boolean useSwaggerUI = true;
protected String routesFileName = "routes";
protected String basePackage = "org.openapitools";
public ScalaPlayFrameworkServerCodegen() {
super();
modifyFeatureSet(features -> features
.includeDocumentationFeatures(DocumentationFeature.Readme)
.wireFormatFeatures(EnumSet.of(WireFormatFeature.JSON, WireFormatFeature.XML, WireFormatFeature.Custom))
.securityFeatures(EnumSet.noneOf(SecurityFeature.class))
.excludeGlobalFeatures(
GlobalFeature.XMLStructureDefinitions,
GlobalFeature.Callbacks,
GlobalFeature.LinkObjects,
GlobalFeature.ParameterStyling
)
.includeSchemaSupportFeatures(
SchemaSupportFeature.Polymorphism
)
.excludeParameterFeatures(
ParameterFeature.Cookie
)
);
outputFolder = "generated-code" + File.separator + "scala-play-server";
modelTemplateFiles.put("model.mustache", ".scala");
apiTemplateFiles.put("api.mustache", ".scala");
embeddedTemplateDir = templateDir = "scala-play-server";
hideGenerationTimestamp = false;
sourceFolder = "app";
apiPackage = "api";
modelPackage = "model";
instantiationTypes.put("map", "Map");
instantiationTypes.put("array", "List");
typeMapping.put("DateTime", "OffsetDateTime");
typeMapping.put("Date", "LocalDate");
typeMapping.put("Integer", "Int");
typeMapping.put("binary", "Array[Byte]");
typeMapping.put("ByteArray", "Array[Byte]");
typeMapping.put("object", "JsObject");
typeMapping.put("file", "TemporaryFile");
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
importMapping.put("LocalDate", "java.time.LocalDate");
importMapping.remove("BigDecimal");
importMapping.put("TemporaryFile", "play.api.libs.Files.TemporaryFile");
cliOptions.add(new CliOption(ROUTES_FILE_NAME, "Name of the routes file to generate.").defaultValue(routesFileName));
cliOptions.add(new CliOption(BASE_PACKAGE, "Base package in which supporting classes are generated.").defaultValue(basePackage));
addCliOptionWithDefault(SKIP_STUBS, "If set, skips generation of stub classes.", skipStubs);
addCliOptionWithDefault(SUPPORT_ASYNC, "If set, wraps API return types with Futures and generates async actions.", supportAsync);
addCliOptionWithDefault(GENERATE_CUSTOM_EXCEPTIONS, "If set, generates custom exception types.", generateCustomExceptions);
addCliOptionWithDefault(USE_SWAGGER_UI, "Add a route to /api which show your documentation in swagger-ui. Will also import needed dependencies", useSwaggerUI);
}
public CodegenType getTag() {
return CodegenType.SERVER;
}
public String getName() {
return "scala-play-server";
}
public String getHelp() {
return "Generates a Scala server application (beta) with Play Framework.";
}
public void setSupportAsync(boolean supportAsync) {
this.supportAsync = supportAsync;
}
public void setSkipStubs(boolean skipStubs) {
this.skipStubs = skipStubs;
}
public void setGenerateCustomExceptions(boolean generateCustomExceptions) {
this.generateCustomExceptions = generateCustomExceptions;
}
public void setRoutesFileName(String routesFileName) {
this.routesFileName = routesFileName;
}
public void setBasePackage(String basePackage) {
this.basePackage = basePackage;
}
public void setUseSwaggerUI(boolean useSwaggerUI) {
this.useSwaggerUI = useSwaggerUI;
}
@Override
public void processOpts() {
super.processOpts();
if (additionalProperties.containsKey(SKIP_STUBS)) {
this.setSkipStubs(convertPropertyToBoolean(SKIP_STUBS));
}
writePropertyBack(SKIP_STUBS, skipStubs);
if (additionalProperties.containsKey(SUPPORT_ASYNC)) {
this.setSupportAsync(convertPropertyToBoolean(SUPPORT_ASYNC));
}
writePropertyBack(SUPPORT_ASYNC, supportAsync);
if (additionalProperties.containsKey(GENERATE_CUSTOM_EXCEPTIONS)) {
this.setGenerateCustomExceptions(convertPropertyToBoolean(GENERATE_CUSTOM_EXCEPTIONS));
}
writePropertyBack(GENERATE_CUSTOM_EXCEPTIONS, generateCustomExceptions);
if (additionalProperties.containsKey(USE_SWAGGER_UI)) {
this.setUseSwaggerUI(convertPropertyToBoolean(USE_SWAGGER_UI));
}
writePropertyBack(USE_SWAGGER_UI, useSwaggerUI);
if (additionalProperties.containsKey(ROUTES_FILE_NAME)) {
this.setRoutesFileName((String) additionalProperties.get(ROUTES_FILE_NAME));
} else {
additionalProperties.put(ROUTES_FILE_NAME, routesFileName);
}
if (additionalProperties.containsKey(BASE_PACKAGE)) {
this.setBasePackage((String) additionalProperties.get(BASE_PACKAGE));
} else {
additionalProperties.put(BASE_PACKAGE, basePackage);
}
apiTemplateFiles.remove("api.mustache");
if (!skipStubs) {
apiTemplateFiles.put("app/apiImplStubs.scala.mustache", "Impl.scala");
}
apiTemplateFiles.put("app/apiTrait.scala.mustache", ".scala");
apiTemplateFiles.put("app/apiController.scala.mustache", "Controller.scala");
supportingFiles.add(new SupportingFile("README.md.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("build.sbt.mustache", "", "build.sbt"));
supportingFiles.add(new SupportingFile("conf/application.conf.mustache", "conf", "application.conf"));
supportingFiles.add(new SupportingFile("conf/logback.xml.mustache", "conf", "logback.xml"));
supportingFiles.add(new SupportingFile("project/build.properties.mustache", "project", "build.properties"));
supportingFiles.add(new SupportingFile("project/plugins.sbt.mustache", "project", "plugins.sbt"));
supportingFiles.add(new SupportingFile("conf/routes.mustache", "conf", routesFileName));
supportingFiles.add(new SupportingFile("app/module.scala.mustache", getBasePackagePath(), "Module.scala"));
supportingFiles.add(new SupportingFile("app/errorHandler.scala.mustache", getBasePackagePath(), "ErrorHandler.scala"));
if (generateCustomExceptions) {
supportingFiles.add(new SupportingFile("app/exceptions.scala.mustache", getBasePackagePath(), "OpenApiExceptions.scala"));
}
if (this.useSwaggerUI) {
//App/Controllers
supportingFiles.add(new SupportingFile("public/openapi.json.mustache", "public", "openapi.json"));
supportingFiles.add(new SupportingFile("app/apiDocController.scala.mustache", String.format(Locale.ROOT, "app/%s", apiPackage.replace(".", File.separator)), "ApiDocController.scala"));
}
}
@Override
protected Builder addMustacheLambdas() {
return super.addMustacheLambdas()
.put("indented_4", new IndentedLambda(4, " "));
}
@SuppressWarnings("unchecked")
@Override
public Map postProcessOperationsWithModels(Map objs, List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy