Enhance scalatra server codegen to include openapi info and package dir (#11385)

* enhance scalatra server codegen to include openapi info and package dir

* enhance scalatra server codegen to include openapi info and package dir

Co-authored-by: Ravi Nallappan <ravi_nallappan@persistent.com>
This commit is contained in:
ravinallappan
2022-02-27 16:03:01 +08:00
committed by GitHub
parent 319ccbd138
commit a1da4c9c5e
7 changed files with 60 additions and 30 deletions

View File

@@ -259,7 +259,7 @@ public class DefaultCodegen implements CodegenConfig {
protected String enumUnknownDefaultCaseName = "unknown_default_open_api";
// make openapi available to all methods
protected OpenAPI openAPI;
protected OpenAPI openAPI = null;
// A cache to efficiently lookup a Schema instance based on the return value of `toModelName()`.
private Map<String, Schema> modelNameToSchemaCache;

View File

@@ -42,6 +42,13 @@ public abstract class AbstractScalaCodegen extends DefaultCodegen {
protected String modelPropertyNaming = CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase.name();
protected String invokerPackage = "org.openapitools.client";
protected String sourceFolder = "src/main/scala";
protected String appName = "OpenAPI Sample";
protected String appDescription = "A sample openapi server";
protected String infoUrl = "http://org.openapitools" ;
protected String infoEmail = "team@openapitools.org" ;
protected String licenseInfo = "All rights reserved";
protected String licenseUrl = "http://apache.org/licenses/LICENSE-2.0.html";
protected String apiVersion = "1.0" ;
protected boolean stripPackageName = true;
protected String dateLibrary = DateLibraries.java8.name();
@@ -170,6 +177,15 @@ public abstract class AbstractScalaCodegen extends DefaultCodegen {
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
}
this.appName = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getTitle()).filter(t -> t != null).orElse(this.appName) ;
this.appDescription = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getDescription()).filter(d -> d != null).orElse(this.appDescription) ;
this.infoUrl = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getContact()).filter(c -> c != null).map(c -> c.getUrl()).filter(u -> u != null).orElse(this.infoUrl) ;
this.infoEmail = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getContact()).filter(c -> c != null).map(c -> c.getEmail()).filter(v -> v != null).orElse(this.infoEmail) ;
this.licenseInfo = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getLicense()).filter(l -> l != null).map(l -> l.getName()).filter(n -> n != null).orElse(this.licenseInfo) ;
this.licenseUrl = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getLicense()).filter(l -> l != null).map(l -> l.getUrl()).filter(u -> u != null).orElse(this.licenseUrl) ;
this.apiVersion = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getVersion()).filter(v -> v != null).orElse(this.apiVersion) ;
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
}

View File

@@ -20,6 +20,7 @@ package org.openapitools.codegen.languages;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.features.*;
import java.io.File;
import java.util.*;
public class ScalatraServerCodegen extends AbstractScalaCodegen implements CodegenConfig {
@@ -95,30 +96,7 @@ public class ScalatraServerCodegen extends AbstractScalaCodegen implements Codeg
typeMapping.put("number", "Double");
typeMapping.put("decimal", "BigDecimal");
additionalProperties.put("appName", "OpenAPI Sample");
additionalProperties.put("appDescription", "A sample openapi server");
additionalProperties.put("infoUrl", "http://org.openapitools");
additionalProperties.put("infoEmail", "team@openapitools.org");
additionalProperties.put("licenseInfo", "All rights reserved");
additionalProperties.put("licenseUrl", "http://apache.org/licenses/LICENSE-2.0.html");
additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
additionalProperties.put(CodegenConstants.GROUP_ID, groupId);
additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId);
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("build.sbt", "", "build.sbt"));
supportingFiles.add(new SupportingFile("web.xml", "/src/main/webapp/WEB-INF", "web.xml"));
supportingFiles.add(new SupportingFile("logback.xml", "/src/main/resources", "logback.xml"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
supportingFiles.add(new SupportingFile("JettyMain.mustache", sourceFolder, "JettyMain.scala"));
supportingFiles.add(new SupportingFile("Bootstrap.mustache", sourceFolder, "ScalatraBootstrap.scala"));
supportingFiles.add(new SupportingFile("ServletApp.mustache", sourceFolder, "ServletApp.scala"));
supportingFiles.add(new SupportingFile("project/build.properties", "project", "build.properties"));
supportingFiles.add(new SupportingFile("project/plugins.sbt", "project", "plugins.sbt"));
supportingFiles.add(new SupportingFile("sbt", "", "sbt"));
importMapping = new HashMap<>();
importMapping = new HashMap<String, String>();
importMapping.put("UUID", "java.util.UUID");
importMapping.put("URI", "java.net.URI");
importMapping.put("File", "java.io.File");
@@ -141,6 +119,37 @@ public class ScalatraServerCodegen extends AbstractScalaCodegen implements Codeg
instantiationTypes.put("set", "Set");
}
@Override
public void processOpts() {
super.processOpts();
String appPackage = invokerPackage + ".app";
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("build.mustache", "", "build.sbt"));
supportingFiles.add(new SupportingFile("web.xml", "/src/main/webapp/WEB-INF", "web.xml"));
supportingFiles.add(new SupportingFile("logback.xml", "/src/main/resources", "logback.xml"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
supportingFiles.add(new SupportingFile("JettyMain.mustache", sourceFolderByPackage(invokerPackage), "JettyMain.scala"));
supportingFiles.add(new SupportingFile("Bootstrap.mustache", sourceFolderByPackage(invokerPackage), "ScalatraBootstrap.scala"));
supportingFiles.add(new SupportingFile("ServletApp.mustache", sourceFolderByPackage(appPackage), "ServletApp.scala"));
supportingFiles.add(new SupportingFile("project/build.properties", "project", "build.properties"));
supportingFiles.add(new SupportingFile("project/plugins.sbt", "project", "plugins.sbt"));
supportingFiles.add(new SupportingFile("sbt", "", "sbt"));
additionalProperties.put("appName", appName);
additionalProperties.put("appDescription", appDescription);
additionalProperties.put("infoUrl", infoUrl);
additionalProperties.put("infoEmail", infoEmail);
additionalProperties.put("apiVersion", apiVersion) ;
additionalProperties.put("licenseInfo", licenseInfo);
additionalProperties.put("licenseUrl", licenseUrl);
additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
additionalProperties.put(CodegenConstants.GROUP_ID, groupId);
additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId);
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
}
@Override
public CodegenType getTag() {
return CodegenType.SERVER;
@@ -187,4 +196,7 @@ public class ScalatraServerCodegen extends AbstractScalaCodegen implements Codeg
return objs;
}
private String sourceFolderByPackage(String packageName) {
return sourceFolder + File.separator + packageName.replaceAll("[.]", File.separator);
}
}

View File

@@ -1,5 +1,5 @@
{{>licenseInfo}}
package {{invokerPackage}}
import {{apiPackage}}._
import {{invokerPackage}}.app.{ ResourcesApp, OpenAPIApp }
import javax.servlet.ServletContext

View File

@@ -1,4 +1,5 @@
{{>licenseInfo}}
package {{invokerPackage}}
import org.eclipse.jetty.server._
import org.eclipse.jetty.webapp.WebAppContext
import org.scalatra.servlet.ScalatraListener
@@ -36,6 +37,7 @@ object JettyMain {
webApp setContextPath conf.contextPath
webApp setResourceBase conf.webapp
webApp setEventListeners Array(new ScalatraListener)
webApp.setInitParameter(ScalatraListener.LifeCycleKey, classOf[ScalatraBootstrap].getName)
server setHandler webApp

View File

@@ -14,7 +14,7 @@ class ResourcesApp(implicit protected val swagger: OpenAPIApp)
}
}
class OpenAPIApp extends Swagger(apiInfo = OpenAPIInfo.apiInfo, apiVersion = "1.0", swaggerVersion = Swagger.SpecVersion)
class OpenAPIApp extends Swagger(apiInfo = OpenAPIInfo.apiInfo, apiVersion = "{{apiVersion}}", swaggerVersion = Swagger.SpecVersion)
object OpenAPIInfo {
val apiInfo = ApiInfo(

View File

@@ -1,6 +1,6 @@
organization := "org.openapitools"
name := "scalatra-sample"
version := "0.1.0-SNAPSHOT"
organization := "{{groupId}}"
name := "{{groupId}}-{{artifactId}}"
version := "{{artifactVersion}}"
scalaVersion := "2.12.4"
mainClass in assembly := Some("JettyMain")