diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalatraServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalatraServerCodegen.java index e04d77683798..64451a8ab853 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalatraServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalatraServerCodegen.java @@ -138,6 +138,7 @@ public void processOpts() { 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("scalafmt.mustache", "", ".scalafmt.conf")); supportingFiles.add(new SupportingFile("sbt", "", "sbt")); additionalProperties.put("appName", appName); diff --git a/modules/openapi-generator/src/main/resources/scalatra/project/plugins.sbt b/modules/openapi-generator/src/main/resources/scalatra/project/plugins.sbt index 3c8855a09909..d3ee690946d1 100644 --- a/modules/openapi-generator/src/main/resources/scalatra/project/plugins.sbt +++ b/modules/openapi-generator/src/main/resources/scalatra/project/plugins.sbt @@ -1,3 +1,4 @@ addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6") addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "4.0.1") addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2") +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.6") diff --git a/modules/openapi-generator/src/main/resources/scalatra/scalafmt.mustache b/modules/openapi-generator/src/main/resources/scalatra/scalafmt.mustache new file mode 100644 index 000000000000..fa3c57ad8c66 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/scalatra/scalafmt.mustache @@ -0,0 +1,58 @@ +version=3.10.6 +runner.dialect = scala212 +project { + git = false + excludeFilters = [ + scalafmt-benchmarks/src/resources, + sbt-test + bin/issue + ] + layout = StandardConvention +} +align { + preset = none + stripMargin = true +} +binPack { + importSelectors = fold +} +newlines { + avoidForSimpleOverflow = all + ignoreInSyntax = false + source = fold +} +rewrite { + rules = [ + AvoidInfix, + Imports, + RedundantBraces, + RedundantParens, + SortModifiers, + ] + imports { + selectors = fold + removeRedundantSelectors = true + sort = ascii + groups = [ + ["org\\.scalafmt\\..*"], + ["scala\\.meta\\..*", "org\\.scalameta\\..*"], + ["sbt\\..*"], + ["java.?\\..*"], + ["scala\\..*"], + ["org\\..*"], + ["com\\..*"], + ] + } + redundantBraces { + preset = all + oneStatApply { + parensMaxSpan = 300 + bracesMinSpan = 300 + } + } + redundantParens { + preset = all + } + sortModifiers.preset = styleGuide + trailingCommas.style = "always" +} diff --git a/samples/server/petstore/scalatra/.openapi-generator/FILES b/samples/server/petstore/scalatra/.openapi-generator/FILES new file mode 100644 index 000000000000..6d1ae3169dee --- /dev/null +++ b/samples/server/petstore/scalatra/.openapi-generator/FILES @@ -0,0 +1,21 @@ +.gitignore +.scalafmt.conf +README.md +build.sbt +project/build.properties +project/plugins.sbt +sbt +src/main/resources/logback.xml +src/main/scala/org/openapitools/JettyMain.scala +src/main/scala/org/openapitools/ScalatraBootstrap.scala +src/main/scala/org/openapitools/app/ServletApp.scala +src/main/scala/org/openapitools/server/api/PetApi.scala +src/main/scala/org/openapitools/server/api/StoreApi.scala +src/main/scala/org/openapitools/server/api/UserApi.scala +src/main/scala/org/openapitools/server/model/ApiResponse.scala +src/main/scala/org/openapitools/server/model/Category.scala +src/main/scala/org/openapitools/server/model/Order.scala +src/main/scala/org/openapitools/server/model/Pet.scala +src/main/scala/org/openapitools/server/model/Tag.scala +src/main/scala/org/openapitools/server/model/User.scala +src/main/webapp/WEB-INF/web.xml diff --git a/samples/server/petstore/scalatra/.openapi-generator/VERSION b/samples/server/petstore/scalatra/.openapi-generator/VERSION index d99e7162d01f..f7962df3e243 100644 --- a/samples/server/petstore/scalatra/.openapi-generator/VERSION +++ b/samples/server/petstore/scalatra/.openapi-generator/VERSION @@ -1 +1 @@ -5.0.0-SNAPSHOT \ No newline at end of file +7.22.0-SNAPSHOT diff --git a/samples/server/petstore/scalatra/.scalafmt.conf b/samples/server/petstore/scalatra/.scalafmt.conf new file mode 100644 index 000000000000..fa3c57ad8c66 --- /dev/null +++ b/samples/server/petstore/scalatra/.scalafmt.conf @@ -0,0 +1,58 @@ +version=3.10.6 +runner.dialect = scala212 +project { + git = false + excludeFilters = [ + scalafmt-benchmarks/src/resources, + sbt-test + bin/issue + ] + layout = StandardConvention +} +align { + preset = none + stripMargin = true +} +binPack { + importSelectors = fold +} +newlines { + avoidForSimpleOverflow = all + ignoreInSyntax = false + source = fold +} +rewrite { + rules = [ + AvoidInfix, + Imports, + RedundantBraces, + RedundantParens, + SortModifiers, + ] + imports { + selectors = fold + removeRedundantSelectors = true + sort = ascii + groups = [ + ["org\\.scalafmt\\..*"], + ["scala\\.meta\\..*", "org\\.scalameta\\..*"], + ["sbt\\..*"], + ["java.?\\..*"], + ["scala\\..*"], + ["org\\..*"], + ["com\\..*"], + ] + } + redundantBraces { + preset = all + oneStatApply { + parensMaxSpan = 300 + bracesMinSpan = 300 + } + } + redundantParens { + preset = all + } + sortModifiers.preset = styleGuide + trailingCommas.style = "always" +} diff --git a/samples/server/petstore/scalatra/build.sbt b/samples/server/petstore/scalatra/build.sbt index cca81187eade..144054aa0453 100644 --- a/samples/server/petstore/scalatra/build.sbt +++ b/samples/server/petstore/scalatra/build.sbt @@ -1,6 +1,6 @@ organization := "org.openapitools" -name := "scalatra-sample" -version := "0.1.0-SNAPSHOT" +name := "org.openapitools-openapi-server" +version := "1.0.0" scalaVersion := "2.12.20" mainClass in assembly := Some("JettyMain") diff --git a/samples/server/petstore/scalatra/project/plugins.sbt b/samples/server/petstore/scalatra/project/plugins.sbt index 3c8855a09909..d3ee690946d1 100644 --- a/samples/server/petstore/scalatra/project/plugins.sbt +++ b/samples/server/petstore/scalatra/project/plugins.sbt @@ -1,3 +1,4 @@ addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6") addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "4.0.1") addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2") +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.6") diff --git a/samples/server/petstore/scalatra/src/main/scala/org/openapitools/JettyMain.scala b/samples/server/petstore/scalatra/src/main/scala/org/openapitools/JettyMain.scala new file mode 100644 index 000000000000..4bad3aa58914 --- /dev/null +++ b/samples/server/petstore/scalatra/src/main/scala/org/openapitools/JettyMain.scala @@ -0,0 +1,56 @@ +/** + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * Contact: team@openapitools.org + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + */ + +package org.openapitools +import org.eclipse.jetty.server._ +import org.eclipse.jetty.webapp.WebAppContext +import org.scalatra.servlet.ScalatraListener + +object JettyMain { + + object conf { + val port = sys.env.get("PORT") map (_.toInt) getOrElse (8080) + val stopTimeout = sys.env.get("STOP_TIMEOUT") map (_.toInt) getOrElse (5000) + val connectorIdleTimeout = sys.env.get("CONNECTOR_IDLE_TIMEOUT") map (_.toInt) getOrElse (90000) + val webapp = sys.env.get("PUBLIC") getOrElse "webapp" + val contextPath = sys.env.get("CONTEXT_PATH") getOrElse "/" + } + + def main(args: Array[String]) = { + val server: Server = new Server + println("starting jetty") + + server setStopTimeout conf.stopTimeout + //server setDumpAfterStart true + server setStopAtShutdown true + + val httpConfig = new HttpConfiguration() + httpConfig setSendDateHeader true + httpConfig setSendServerVersion false + + val connector = new NetworkTrafficServerConnector(server, new HttpConnectionFactory(httpConfig)) + connector setPort conf.port + connector setSoLingerTime 0 + connector setIdleTimeout conf.connectorIdleTimeout + server addConnector connector + + val webapp = conf.webapp + val webApp = new WebAppContext + webApp setContextPath conf.contextPath + webApp setResourceBase conf.webapp + webApp setEventListeners Array(new ScalatraListener) + webApp.setInitParameter(ScalatraListener.LifeCycleKey, classOf[ScalatraBootstrap].getName) + + server setHandler webApp + + server.start() + } +} diff --git a/samples/server/petstore/scalatra/src/main/scala/org/openapitools/ScalatraBootstrap.scala b/samples/server/petstore/scalatra/src/main/scala/org/openapitools/ScalatraBootstrap.scala new file mode 100644 index 000000000000..056fe14cd423 --- /dev/null +++ b/samples/server/petstore/scalatra/src/main/scala/org/openapitools/ScalatraBootstrap.scala @@ -0,0 +1,31 @@ +/** + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * Contact: team@openapitools.org + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + */ + +package org.openapitools +import org.openapitools.server.api._ +import org.openapitools.app.{ ResourcesApp, OpenAPIApp } +import javax.servlet.ServletContext +import org.scalatra.LifeCycle + +class ScalatraBootstrap extends LifeCycle { + implicit val openapi = new OpenAPIApp + + override def init(context: ServletContext) { + try { + context mount (new PetApi, "/v2/Pet/*") + context mount (new StoreApi, "/v2/Store/*") + context mount (new UserApi, "/v2/User/*") + context mount (new ResourcesApp, "/api-docs/*") + } catch { + case e: Throwable => e.printStackTrace() + } + } +} diff --git a/samples/server/petstore/scalatra/src/main/scala/org/openapitools/app/ServletApp.scala b/samples/server/petstore/scalatra/src/main/scala/org/openapitools/app/ServletApp.scala new file mode 100644 index 000000000000..3ef15a927948 --- /dev/null +++ b/samples/server/petstore/scalatra/src/main/scala/org/openapitools/app/ServletApp.scala @@ -0,0 +1,37 @@ +/** + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * Contact: team@openapitools.org + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + */ + + +package org.openapitools.app + +import org.scalatra.swagger.{ ApiInfo, SwaggerWithAuth, Swagger } +import org.scalatra.swagger.{ JacksonSwaggerBase, Swagger } +import org.scalatra.ScalatraServlet +import org.json4s.{ DefaultFormats, Formats } + +class ResourcesApp(implicit protected val swagger: OpenAPIApp) + extends ScalatraServlet with JacksonSwaggerBase { + before() { + response.headers += ("Access-Control-Allow-Origin" -> "*") + } +} + +class OpenAPIApp extends Swagger(apiInfo = OpenAPIInfo.apiInfo, apiVersion = "1.0.0", swaggerVersion = Swagger.SpecVersion) + +object OpenAPIInfo { + val apiInfo = ApiInfo( + """OpenAPI Petstore""", + """This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.""", + """http://org.openapitools""", + """team@openapitools.org""", + """Apache-2.0""", + """https://www.apache.org/licenses/LICENSE-2.0.html""") +} diff --git a/samples/server/petstore/scalatra/src/main/scala/org/openapitools/server/api/UserApi.scala b/samples/server/petstore/scalatra/src/main/scala/org/openapitools/server/api/UserApi.scala index 8cdc75071cf0..9e39d338ec4e 100644 --- a/samples/server/petstore/scalatra/src/main/scala/org/openapitools/server/api/UserApi.scala +++ b/samples/server/petstore/scalatra/src/main/scala/org/openapitools/server/api/UserApi.scala @@ -12,6 +12,7 @@ package org.openapitools.server.api +import java.time.OffsetDateTime import org.openapitools.server.model.User import java.io.File