forked from loafle/openapi-generator-original
updated to new scalatra allowable values
This commit is contained in:
@@ -52,11 +52,48 @@ object ScalatraServerGenerator extends BasicScalaGenerator {
|
||||
|
||||
mutable.map(k => {
|
||||
k._1 match {
|
||||
case "allParams" => {
|
||||
val paramList = k._2.asInstanceOf[List[_]]
|
||||
paramList.foreach(param => {
|
||||
val map = param.asInstanceOf[scala.collection.mutable.HashMap[String, AnyRef]]
|
||||
if(map.contains("required")) {
|
||||
if(map("required") == "false") map += "notRequired" -> "true"
|
||||
}
|
||||
if(map.contains("allowableValues")) {
|
||||
val allowableValues = map("allowableValues")
|
||||
val quote = map("swaggerDataType") match {
|
||||
case "string" => "\""
|
||||
case _ => ""
|
||||
}
|
||||
val pattern = "([A-Z]*)\\[(.*)\\]".r
|
||||
val str = allowableValues match {
|
||||
case pattern(valueType, values) => {
|
||||
valueType match {
|
||||
case "LIST" => {
|
||||
val l = values.split(",").toList
|
||||
Some("AllowableValues(" + l.mkString(quote, quote + "," + quote, quote + ")"))
|
||||
}
|
||||
case "RANGE" => {
|
||||
val r = values.split(",")
|
||||
Some("AllowableValues(Range(" + r(0) + "," + r(1) + ", 1))")
|
||||
}
|
||||
}
|
||||
}
|
||||
case _ => None
|
||||
}
|
||||
str match {
|
||||
case Some(s) => map += "allowableValues" -> s
|
||||
case _ =>
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// the scalatra templates like lower-case httpMethods
|
||||
case e: String if (e == "httpMethod") => mutable += "httpMethod" -> k._2.toString.toLowerCase
|
||||
case "httpMethod" => mutable += "httpMethod" -> k._2.toString.toLowerCase
|
||||
|
||||
// convert path into ruby-ish syntax without basePart (i.e. /pet.{format}/{petId} => /:petId
|
||||
case e: String if (e == "path") => {
|
||||
case "path" => {
|
||||
val path = {
|
||||
val arr = k._2.toString.split("/")
|
||||
if (arr.length >= 2) {
|
||||
@@ -66,7 +103,6 @@ object ScalatraServerGenerator extends BasicScalaGenerator {
|
||||
k._2.toString
|
||||
}
|
||||
// rip out the root path
|
||||
|
||||
mutable += "path" -> path.replaceAll("\\{", ":").replaceAll("\\}", "")
|
||||
}
|
||||
case _ =>
|
||||
|
||||
@@ -17,7 +17,7 @@ libraryDependencies ++= Seq(
|
||||
"org.scalatra" % "scalatra-swagger" % "2.2.0-SNAPSHOT",
|
||||
"org.scalatra" % "scalatra-json" % "2.2.0-SNAPSHOT",
|
||||
"org.json4s" %% "json4s-jackson" % "3.0.0-SNAPSHOT",
|
||||
"com.wordnik" % "swagger-core_2.9.1" % "1.1-SNAPSHOT",
|
||||
"com.wordnik" % "swagger-core_2.9.1" % "1.1.1-SNAPSHOT",
|
||||
"ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime",
|
||||
"org.eclipse.jetty" % "jetty-webapp" % "8.1.5.v20120716" % "container",
|
||||
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar")))
|
||||
|
||||
@@ -2,16 +2,20 @@ package apis
|
||||
|
||||
import com.wordnik.client.model.Pet
|
||||
import com.wordnik.swagger.core.ApiPropertiesReader
|
||||
|
||||
import org.scalatra.{ TypedParamSupport, ScalatraServlet }
|
||||
import org.scalatra.swagger._
|
||||
import org.scalatra.json._
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
import org.json4s.{ DefaultFormats, Formats }
|
||||
import org.json4s._
|
||||
import org.json4s.JsonDSL._
|
||||
import org.scalatra.json.{JValueResult, NativeJsonSupport}
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
|
||||
class PetApi (implicit val swagger: Swagger) extends ScalatraServlet with TypedParamSupport with JacksonJsonSupport with JValueResult with SwaggerSupport {
|
||||
class PetApi (implicit val swagger: Swagger) extends ScalatraServlet
|
||||
with TypedParamSupport
|
||||
with NativeJsonSupport
|
||||
with JValueResult
|
||||
with SwaggerSupport {
|
||||
protected implicit val jsonFormats: Formats = DefaultFormats
|
||||
|
||||
protected val applicationDescription: String = "PetApi"
|
||||
@@ -38,11 +42,14 @@ class PetApi (implicit val swagger: Swagger) extends ScalatraServlet with TypedP
|
||||
endpoint("{petId}"),
|
||||
notes("Returns a pet based on ID"),
|
||||
parameters(
|
||||
Parameter("petId", "ID of pet that needs to be fetched",
|
||||
Parameter(name = "petId",
|
||||
description = "ID of pet that needs to be fetched",
|
||||
dataType = DataType.String,
|
||||
defaultValue = None,
|
||||
paramType = ParamType.Path)
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
post("/",
|
||||
@@ -52,11 +59,13 @@ class PetApi (implicit val swagger: Swagger) extends ScalatraServlet with TypedP
|
||||
endpoint(""),
|
||||
notes(""),
|
||||
parameters(
|
||||
Parameter("body", "Pet object that needs to be added to the store",
|
||||
Parameter(name = "body",
|
||||
description = "Pet object that needs to be added to the store",
|
||||
dataType = DataType("Pet"),
|
||||
paramType = ParamType.Body)
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
put("/",
|
||||
@@ -66,11 +75,13 @@ class PetApi (implicit val swagger: Swagger) extends ScalatraServlet with TypedP
|
||||
endpoint(""),
|
||||
notes(""),
|
||||
parameters(
|
||||
Parameter("body", "Pet object that needs to be updated in the store",
|
||||
Parameter(name = "body",
|
||||
description = "Pet object that needs to be updated in the store",
|
||||
dataType = DataType("Pet"),
|
||||
paramType = ParamType.Body)
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
get("/findByStatus",
|
||||
@@ -80,14 +91,16 @@ class PetApi (implicit val swagger: Swagger) extends ScalatraServlet with TypedP
|
||||
endpoint("findByStatus"),
|
||||
notes("Multiple status values can be provided with comma seperated strings"),
|
||||
parameters(
|
||||
Parameter("status", "Status values that need to be considered for filter",
|
||||
Parameter(name = "status",
|
||||
description = "Status values that need to be considered for filter",
|
||||
paramType = ParamType.Query,
|
||||
required = true,
|
||||
allowMultiple = true,
|
||||
allowableValues = AllowableValues("LIST[available,pending,sold]"),defaultValue = Some("available"),
|
||||
allowableValues = AllowableValues("available","pending","sold"),defaultValue = Some("available"),
|
||||
dataType = DataType("String"))
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
get("/findByTags",
|
||||
@@ -97,14 +110,15 @@ class PetApi (implicit val swagger: Swagger) extends ScalatraServlet with TypedP
|
||||
endpoint("findByTags"),
|
||||
notes("Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing."),
|
||||
parameters(
|
||||
Parameter("tags", "Tags to filter by",
|
||||
Parameter(name = "tags",
|
||||
description = "Tags to filter by",
|
||||
paramType = ParamType.Query,
|
||||
required = true,
|
||||
allowMultiple = true,
|
||||
defaultValue = None,
|
||||
dataType = DataType("String"))
|
||||
|
||||
)) {
|
||||
}
|
||||
|
||||
// do something
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,20 @@ package apis
|
||||
|
||||
import com.wordnik.client.model.Order
|
||||
import com.wordnik.swagger.core.ApiPropertiesReader
|
||||
|
||||
import org.scalatra.{ TypedParamSupport, ScalatraServlet }
|
||||
import org.scalatra.swagger._
|
||||
import org.scalatra.json._
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
import org.json4s.{ DefaultFormats, Formats }
|
||||
import org.json4s._
|
||||
import org.json4s.JsonDSL._
|
||||
import org.scalatra.json.{JValueResult, NativeJsonSupport}
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
|
||||
class StoreApi (implicit val swagger: Swagger) extends ScalatraServlet with TypedParamSupport with JacksonJsonSupport with JValueResult with SwaggerSupport {
|
||||
class StoreApi (implicit val swagger: Swagger) extends ScalatraServlet
|
||||
with TypedParamSupport
|
||||
with NativeJsonSupport
|
||||
with JValueResult
|
||||
with SwaggerSupport {
|
||||
protected implicit val jsonFormats: Formats = DefaultFormats
|
||||
|
||||
protected val applicationDescription: String = "StoreApi"
|
||||
@@ -38,11 +42,14 @@ class StoreApi (implicit val swagger: Swagger) extends ScalatraServlet with Type
|
||||
endpoint("order/{orderId}"),
|
||||
notes("For valid response try integer IDs with value <= 5. Anything above 5 or nonintegers will generate API errors"),
|
||||
parameters(
|
||||
Parameter("orderId", "ID of pet that needs to be fetched",
|
||||
Parameter(name = "orderId",
|
||||
description = "ID of pet that needs to be fetched",
|
||||
dataType = DataType.String,
|
||||
defaultValue = None,
|
||||
paramType = ParamType.Path)
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
delete("/order/:orderId",
|
||||
@@ -52,11 +59,14 @@ class StoreApi (implicit val swagger: Swagger) extends ScalatraServlet with Type
|
||||
endpoint("order/{orderId}"),
|
||||
notes("For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors"),
|
||||
parameters(
|
||||
Parameter("orderId", "ID of the order that needs to be deleted",
|
||||
Parameter(name = "orderId",
|
||||
description = "ID of the order that needs to be deleted",
|
||||
dataType = DataType.String,
|
||||
defaultValue = None,
|
||||
paramType = ParamType.Path)
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
post("/order",
|
||||
@@ -66,11 +76,12 @@ class StoreApi (implicit val swagger: Swagger) extends ScalatraServlet with Type
|
||||
endpoint("order"),
|
||||
notes(""),
|
||||
parameters(
|
||||
Parameter("body", "order placed for purchasing the pet",
|
||||
Parameter(name = "body",
|
||||
description = "order placed for purchasing the pet",
|
||||
dataType = DataType("Order"),
|
||||
paramType = ParamType.Body)
|
||||
|
||||
)) {
|
||||
}
|
||||
|
||||
// do something
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,20 @@ package apis
|
||||
|
||||
import com.wordnik.client.model.User
|
||||
import com.wordnik.swagger.core.ApiPropertiesReader
|
||||
|
||||
import org.scalatra.{ TypedParamSupport, ScalatraServlet }
|
||||
import org.scalatra.swagger._
|
||||
import org.scalatra.json._
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
import org.json4s.{ DefaultFormats, Formats }
|
||||
import org.json4s._
|
||||
import org.json4s.JsonDSL._
|
||||
import org.scalatra.json.{JValueResult, NativeJsonSupport}
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
|
||||
class UserApi (implicit val swagger: Swagger) extends ScalatraServlet with TypedParamSupport with JacksonJsonSupport with JValueResult with SwaggerSupport {
|
||||
class UserApi (implicit val swagger: Swagger) extends ScalatraServlet
|
||||
with TypedParamSupport
|
||||
with NativeJsonSupport
|
||||
with JValueResult
|
||||
with SwaggerSupport {
|
||||
protected implicit val jsonFormats: Formats = DefaultFormats
|
||||
|
||||
protected val applicationDescription: String = "UserApi"
|
||||
@@ -38,11 +42,13 @@ class UserApi (implicit val swagger: Swagger) extends ScalatraServlet with Typed
|
||||
endpoint("createWithArray"),
|
||||
notes(""),
|
||||
parameters(
|
||||
Parameter("body", "List of user object",
|
||||
Parameter(name = "body",
|
||||
description = "List of user object",
|
||||
dataType = DataType("Array[User]"),
|
||||
paramType = ParamType.Body)
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
post("/",
|
||||
@@ -52,11 +58,13 @@ class UserApi (implicit val swagger: Swagger) extends ScalatraServlet with Typed
|
||||
endpoint(""),
|
||||
notes("This can only be done by the logged in user."),
|
||||
parameters(
|
||||
Parameter("body", "Created user object",
|
||||
Parameter(name = "body",
|
||||
description = "Created user object",
|
||||
dataType = DataType("User"),
|
||||
paramType = ParamType.Body)
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
post("/createWithList",
|
||||
@@ -66,11 +74,13 @@ class UserApi (implicit val swagger: Swagger) extends ScalatraServlet with Typed
|
||||
endpoint("createWithList"),
|
||||
notes(""),
|
||||
parameters(
|
||||
Parameter("body", "List of user object",
|
||||
Parameter(name = "body",
|
||||
description = "List of user object",
|
||||
dataType = DataType("List[User]"),
|
||||
paramType = ParamType.Body)
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
put("/:username",
|
||||
@@ -80,15 +90,18 @@ class UserApi (implicit val swagger: Swagger) extends ScalatraServlet with Typed
|
||||
endpoint("{username}"),
|
||||
notes("This can only be done by the logged in user."),
|
||||
parameters(
|
||||
Parameter("username", "name that need to be deleted",
|
||||
Parameter(name = "username",
|
||||
description = "name that need to be deleted",
|
||||
dataType = DataType.String,
|
||||
defaultValue = None,
|
||||
paramType = ParamType.Path)
|
||||
,
|
||||
Parameter("body", "Updated user object",
|
||||
,Parameter(name = "body",
|
||||
description = "Updated user object",
|
||||
dataType = DataType("User"),
|
||||
paramType = ParamType.Body)
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
delete("/:username",
|
||||
@@ -98,11 +111,14 @@ class UserApi (implicit val swagger: Swagger) extends ScalatraServlet with Typed
|
||||
endpoint("{username}"),
|
||||
notes("This can only be done by the logged in user."),
|
||||
parameters(
|
||||
Parameter("username", "The name that needs to be deleted",
|
||||
Parameter(name = "username",
|
||||
description = "The name that needs to be deleted",
|
||||
dataType = DataType.String,
|
||||
defaultValue = None,
|
||||
paramType = ParamType.Path)
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
get("/:username",
|
||||
@@ -112,11 +128,14 @@ class UserApi (implicit val swagger: Swagger) extends ScalatraServlet with Typed
|
||||
endpoint("{username}"),
|
||||
notes(""),
|
||||
parameters(
|
||||
Parameter("username", "The name that needs to be fetched. Use user1 for testing.",
|
||||
Parameter(name = "username",
|
||||
description = "The name that needs to be fetched. Use user1 for testing.",
|
||||
dataType = DataType.String,
|
||||
defaultValue = None,
|
||||
paramType = ParamType.Path)
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
get("/login",
|
||||
@@ -126,21 +145,23 @@ class UserApi (implicit val swagger: Swagger) extends ScalatraServlet with Typed
|
||||
endpoint("login"),
|
||||
notes(""),
|
||||
parameters(
|
||||
Parameter("username", "The user name for login",
|
||||
Parameter(name = "username",
|
||||
description = "The user name for login",
|
||||
paramType = ParamType.Query,
|
||||
required = true,
|
||||
allowMultiple = false,
|
||||
defaultValue = None,
|
||||
dataType = DataType("String"))
|
||||
,
|
||||
Parameter("password", "The password for login in clear text",
|
||||
,Parameter(name = "password",
|
||||
description = "The password for login in clear text",
|
||||
paramType = ParamType.Query,
|
||||
required = true,
|
||||
allowMultiple = false,
|
||||
defaultValue = None,
|
||||
dataType = DataType("String"))
|
||||
|
||||
)) {
|
||||
|
||||
// do something
|
||||
}
|
||||
|
||||
get("/logout",
|
||||
@@ -151,6 +172,7 @@ class UserApi (implicit val swagger: Swagger) extends ScalatraServlet with Typed
|
||||
notes(""),
|
||||
parameters(
|
||||
)) {
|
||||
}
|
||||
|
||||
// do something
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,16 +3,20 @@ package {{package}}
|
||||
{{#imports}}import {{import}}
|
||||
{{/imports}}
|
||||
import com.wordnik.swagger.core.ApiPropertiesReader
|
||||
|
||||
import org.scalatra.{ TypedParamSupport, ScalatraServlet }
|
||||
import org.scalatra.swagger._
|
||||
import org.scalatra.json._
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
import org.json4s.{ DefaultFormats, Formats }
|
||||
import org.json4s._
|
||||
import org.json4s.JsonDSL._
|
||||
import org.scalatra.json.{JValueResult, NativeJsonSupport}
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
|
||||
class {{className}} (implicit val swagger: Swagger) extends ScalatraServlet with TypedParamSupport with JacksonJsonSupport with JValueResult with SwaggerSupport {
|
||||
class {{className}} (implicit val swagger: Swagger) extends ScalatraServlet
|
||||
with TypedParamSupport
|
||||
with NativeJsonSupport
|
||||
with JValueResult
|
||||
with SwaggerSupport {
|
||||
protected implicit val jsonFormats: Formats = DefaultFormats
|
||||
|
||||
protected val applicationDescription: String = "{{className}}"
|
||||
@@ -31,9 +35,9 @@ class {{className}} (implicit val swagger: Swagger) extends ScalatraServlet with
|
||||
contentType = formats("json")
|
||||
response.headers += ("Access-Control-Allow-Origin" -> "*")
|
||||
}
|
||||
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
{{newline}}
|
||||
{{httpMethod}}("{{path}}",
|
||||
summary("{{{summary}}}"),
|
||||
nickname("{{nickname}}"),
|
||||
@@ -43,34 +47,40 @@ class {{className}} (implicit val swagger: Swagger) extends ScalatraServlet with
|
||||
parameters(
|
||||
{{#allParams}}
|
||||
{{#queryParameter}}
|
||||
Parameter("{{paramName}}", "{{{description}}}",
|
||||
Parameter(name = "{{paramName}}",
|
||||
description = "{{{description}}}",
|
||||
paramType = ParamType.Query,
|
||||
required = {{required}},
|
||||
allowMultiple = {{allowMultiple}},
|
||||
{{#allowableValues}}allowableValues = AllowableValues("{{{allowableValues}}}"),{{/allowableValues}}
|
||||
{{#allowableValues}}allowableValues = {{{allowableValues}}},{{newline}}{{/allowableValues}}
|
||||
defaultValue = {{#defaultValue}}Some({{{defaultValue}}}){{/defaultValue}}{{^defaultValue}}None{{/defaultValue}},
|
||||
dataType = DataType("{{dataType}}"))
|
||||
{{/queryParameter}}
|
||||
{{#pathParameter}}
|
||||
Parameter("{{paramName}}", "{{{description}}}",
|
||||
Parameter(name = "{{paramName}}",
|
||||
description = "{{{description}}}",
|
||||
dataType = DataType.String,
|
||||
{{#allowableValues}}allowableValues = {{{allowableValues}}},{{newline}}{{/allowableValues}}
|
||||
defaultValue = {{#defaultValue}}Some({{{defaultValue}}}){{/defaultValue}}{{^defaultValue}}None{{/defaultValue}},
|
||||
paramType = ParamType.Path)
|
||||
{{/pathParameter}}
|
||||
{{#headerParameter}}
|
||||
Parameter("{{paramName}}", "{{{description}}}",
|
||||
Parameter(name = "{{paramName}}",
|
||||
description = "{{{description}}}",
|
||||
dataType = DataType("{{dataType}}"),
|
||||
paramType = ParamType.Header)
|
||||
{{/headerParameter}}
|
||||
{{#bodyParameter}}
|
||||
Parameter("{{paramName}}", "{{{description}}}",
|
||||
Parameter(name = "{{paramName}}",
|
||||
description = "{{{description}}}",
|
||||
dataType = DataType("{{dataType}}"),
|
||||
paramType = ParamType.Body)
|
||||
{{/bodyParameter}}
|
||||
{{#hasMore}},{{/hasMore}}{{newline}}
|
||||
{{/allParams}}
|
||||
)) {
|
||||
}
|
||||
{{/bodyParameter}}{{#hasMore}},{{/hasMore}}
|
||||
{{/allParams}}
|
||||
)) {
|
||||
|
||||
{{/operation}}
|
||||
{{/operations}}
|
||||
// do something
|
||||
}
|
||||
{{/operation}}
|
||||
{{/operations}}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ libraryDependencies ++= Seq(
|
||||
"org.scalatra" % "scalatra-swagger" % "2.2.0-SNAPSHOT",
|
||||
"org.scalatra" % "scalatra-json" % "2.2.0-SNAPSHOT",
|
||||
"org.json4s" %% "json4s-jackson" % "3.0.0-SNAPSHOT",
|
||||
"com.wordnik" % "swagger-core_2.9.1" % "1.1-SNAPSHOT",
|
||||
"com.wordnik" % "swagger-core_2.9.1" % "1.1.1-SNAPSHOT",
|
||||
"ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime",
|
||||
"org.eclipse.jetty" % "jetty-webapp" % "8.1.5.v20120716" % "container",
|
||||
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar")))
|
||||
|
||||
Reference in New Issue
Block a user