[Scalatra] replace {} with : in scalatra path (#3694)

* replace {} with : in scalatra path

* remove unused var in scalatra code gen
This commit is contained in:
wing328 2016-09-02 00:25:07 +08:00 committed by GitHub
parent 5467c41dad
commit 11ae12b09d
13 changed files with 282 additions and 181 deletions

View File

@ -4,6 +4,7 @@ import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConfig; import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.CodegenOperation; import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.CodegenType; import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile; import io.swagger.codegen.SupportingFile;
@ -161,8 +162,29 @@ public class ScalatraServerCodegen extends DefaultCodegen implements CodegenConf
Map<String, Object> operations = (Map<String, Object>) objs.get("operations"); Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation"); List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation op : operationList) { for (CodegenOperation op : operationList) {
// force http method to lower case
op.httpMethod = op.httpMethod.toLowerCase(); op.httpMethod = op.httpMethod.toLowerCase();
String[] items = op.path.split("/", -1);
String scalaPath = "";
int pathParamIndex = 0;
for (int i = 0; i < items.length; ++i) {
if (items[i].matches("^\\{(.*)\\}$")) { // wrap in {}
scalaPath = scalaPath + ":" + items[i].replace("{", "").replace("}", "");
pathParamIndex++;
} else {
scalaPath = scalaPath + items[i];
} }
if (i != items.length -1) {
scalaPath = scalaPath + "/";
}
}
op.vendorExtensions.put("x-scalatra-path", scalaPath);
}
return objs; return objs;
} }

View File

@ -38,7 +38,7 @@ class {{classname}} (implicit val swagger: Swagger) extends ScalatraServlet
parameters({{#allParams}}{{>queryParam}}{{>pathParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) parameters({{#allParams}}{{>queryParam}}{{>pathParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
) )
{{httpMethod}}("{{path}}",operation({{nickname}}Operation)) { {{httpMethod}}("{{{vendorExtensions.x-scalatra-path}}}",operation({{nickname}}Operation)) {
{{#allParams}} {{#allParams}}
{{#isFile}}val {{paramName}} = fileParams("{{paramName}}"){{/isFile}} {{#isFile}}val {{paramName}} = fileParams("{{paramName}}"){{/isFile}}
{{^isFile}}{{#isPathParam}} {{^isFile}}{{#isPathParam}}

View File

@ -25,8 +25,8 @@
package com.wordnik.client.api package com.wordnik.client.api
import com.wordnik.client.model.Pet import com.wordnik.client.model.Pet
import java.io.File
import com.wordnik.client.model.ApiResponse import com.wordnik.client.model.ApiResponse
import java.io.File
import java.io.File import java.io.File
@ -53,6 +53,7 @@ class PetApi(implicit val swagger: Swagger) extends ScalatraServlet
response.headers += ("Access-Control-Allow-Origin" -> "*") response.headers += ("Access-Control-Allow-Origin" -> "*")
} }
val addPetOperation = (apiOperation[Unit]("addPet") val addPetOperation = (apiOperation[Unit]("addPet")
summary "Add a new pet to the store" summary "Add a new pet to the store"
parameters(bodyParam[Pet]("body").description("")) parameters(bodyParam[Pet]("body").description(""))
@ -60,27 +61,34 @@ class PetApi(implicit val swagger: Swagger) extends ScalatraServlet
post("/pet",operation(addPetOperation)) { post("/pet",operation(addPetOperation)) {
val body = parsedBody.extract[Pet] val body = parsedBody.extract[Pet]
println("body: " + body) println("body: " + body)
} }
val deletePetOperation = (apiOperation[Unit]("deletePet") val deletePetOperation = (apiOperation[Unit]("deletePet")
summary "Deletes a pet" summary "Deletes a pet"
parameters(pathParam[Long]("petId").description(""), headerParam[String]("apiKey").description("").optional) parameters(pathParam[Long]("petId").description(""), headerParam[String]("apiKey").description("").optional)
) )
delete("/pet/{petId}", operation(deletePetOperation)) { delete("/pet/:petId",operation(deletePetOperation)) {
val petId = params.getOrElse("petId", halt(400)) val petId = params.getOrElse("petId", halt(400))
println("petId: " + petId) println("petId: " + petId)
val apiKey = request.getHeader("apiKey") val apiKey = request.getHeader("apiKey")
println("apiKey: " + apiKey) println("apiKey: " + apiKey)
} }
val findPetsByStatusOperation = (apiOperation[List[Pet]]("findPetsByStatus") val findPetsByStatusOperation = (apiOperation[List[Pet]]("findPetsByStatus")
summary "Finds Pets by status" summary "Finds Pets by status"
parameters(queryParam[List[String]]("status").description("")) parameters(queryParam[List[String]]("status").description(""))
@ -88,18 +96,23 @@ class PetApi(implicit val swagger: Swagger) extends ScalatraServlet
get("/pet/findByStatus",operation(findPetsByStatusOperation)) { get("/pet/findByStatus",operation(findPetsByStatusOperation)) {
val statusString = params.getAs[String]("status") val statusString = params.getAs[String]("status")
val status = if ("multi".equals("default")) { val status = if("csv".equals("default")) {
statusString match { statusString match {
case Some(str) => str.split(",") case Some(str) => str.split(",")
case None => List() case None => List()
} }
} else }
else
List() List()
println("status: " + status) println("status: " + status)
} }
val findPetsByTagsOperation = (apiOperation[List[Pet]]("findPetsByTags") val findPetsByTagsOperation = (apiOperation[List[Pet]]("findPetsByTags")
summary "Finds Pets by tags" summary "Finds Pets by tags"
parameters(queryParam[List[String]]("tags").description("")) parameters(queryParam[List[String]]("tags").description(""))
@ -107,30 +120,38 @@ class PetApi(implicit val swagger: Swagger) extends ScalatraServlet
get("/pet/findByTags",operation(findPetsByTagsOperation)) { get("/pet/findByTags",operation(findPetsByTagsOperation)) {
val tagsString = params.getAs[String]("tags") val tagsString = params.getAs[String]("tags")
val tags = if ("multi".equals("default")) { val tags = if("csv".equals("default")) {
tagsString match { tagsString match {
case Some(str) => str.split(",") case Some(str) => str.split(",")
case None => List() case None => List()
} }
} else }
else
List() List()
println("tags: " + tags) println("tags: " + tags)
} }
val getPetByIdOperation = (apiOperation[Pet]("getPetById") val getPetByIdOperation = (apiOperation[Pet]("getPetById")
summary "Find pet by ID" summary "Find pet by ID"
parameters(pathParam[Long]("petId").description("")) parameters(pathParam[Long]("petId").description(""))
) )
get("/pet/{petId}", operation(getPetByIdOperation)) { get("/pet/:petId",operation(getPetByIdOperation)) {
val petId = params.getOrElse("petId", halt(400)) val petId = params.getOrElse("petId", halt(400))
println("petId: " + petId) println("petId: " + petId)
} }
val updatePetOperation = (apiOperation[Unit]("updatePet") val updatePetOperation = (apiOperation[Unit]("updatePet")
summary "Update an existing pet" summary "Update an existing pet"
parameters(bodyParam[Pet]("body").description("")) parameters(bodyParam[Pet]("body").description(""))
@ -138,42 +159,52 @@ class PetApi(implicit val swagger: Swagger) extends ScalatraServlet
put("/pet",operation(updatePetOperation)) { put("/pet",operation(updatePetOperation)) {
val body = parsedBody.extract[Pet] val body = parsedBody.extract[Pet]
println("body: " + body) println("body: " + body)
} }
val updatePetWithFormOperation = (apiOperation[Unit]("updatePetWithForm") val updatePetWithFormOperation = (apiOperation[Unit]("updatePetWithForm")
summary "Updates a pet in the store with form data" summary "Updates a pet in the store with form data"
parameters(pathParam[Long]("petId").description(""), formParam[String]("name").description("").optional, formParam[String]("status").description("").optional) parameters(pathParam[Long]("petId").description(""), formParam[String]("name").description("").optional, formParam[String]("status").description("").optional)
) )
post("/pet/{petId}", operation(updatePetWithFormOperation)) { post("/pet/:petId",operation(updatePetWithFormOperation)) {
val petId = params.getOrElse("petId", halt(400)) val petId = params.getOrElse("petId", halt(400))
println("petId: " + petId) println("petId: " + petId)
val name = params.getAs[String]("name") val name = params.getAs[String]("name")
println("name: " + name) println("name: " + name)
val status = params.getAs[String]("status") val status = params.getAs[String]("status")
println("status: " + status) println("status: " + status)
} }
val uploadFileOperation = (apiOperation[ApiResponse]("uploadFile") val uploadFileOperation = (apiOperation[ApiResponse]("uploadFile")
summary "uploads an image" summary "uploads an image"
parameters(pathParam[Long]("petId").description(""), formParam[String]("additionalMetadata").description("").optional, formParam[File]("file").description("").optional) parameters(pathParam[Long]("petId").description(""), formParam[String]("additionalMetadata").description("").optional, formParam[File]("file").description("").optional)
) )
post("/pet/{petId}/uploadImage", operation(uploadFileOperation)) { post("/pet/:petId/uploadImage",operation(uploadFileOperation)) {
val petId = params.getOrElse("petId", halt(400)) val petId = params.getOrElse("petId", halt(400))
println("petId: " + petId) println("petId: " + petId)
val additionalMetadata = params.getAs[String]("additionalMetadata") val additionalMetadata = params.getAs[String]("additionalMetadata")
println("additionalMetadata: " + additionalMetadata) println("additionalMetadata: " + additionalMetadata)

View File

@ -51,18 +51,22 @@ class StoreApi(implicit val swagger: Swagger) extends ScalatraServlet
response.headers += ("Access-Control-Allow-Origin" -> "*") response.headers += ("Access-Control-Allow-Origin" -> "*")
} }
val deleteOrderOperation = (apiOperation[Unit]("deleteOrder") val deleteOrderOperation = (apiOperation[Unit]("deleteOrder")
summary "Delete purchase order by ID" summary "Delete purchase order by ID"
parameters (pathParam[Long]("orderId").description("")) parameters(pathParam[String]("orderId").description(""))
) )
delete("/store/order/{orderId}", operation(deleteOrderOperation)) { delete("/store/order/:orderId",operation(deleteOrderOperation)) {
val orderId = params.getOrElse("orderId", halt(400)) val orderId = params.getOrElse("orderId", halt(400))
println("orderId: " + orderId) println("orderId: " + orderId)
} }
val getInventoryOperation = (apiOperation[Map[String, Int]]("getInventory") val getInventoryOperation = (apiOperation[Map[String, Int]]("getInventory")
summary "Returns pet inventories by status" summary "Returns pet inventories by status"
parameters() parameters()
@ -71,18 +75,23 @@ class StoreApi(implicit val swagger: Swagger) extends ScalatraServlet
get("/store/inventory",operation(getInventoryOperation)) { get("/store/inventory",operation(getInventoryOperation)) {
} }
val getOrderByIdOperation = (apiOperation[Order]("getOrderById") val getOrderByIdOperation = (apiOperation[Order]("getOrderById")
summary "Find purchase order by ID" summary "Find purchase order by ID"
parameters(pathParam[Long]("orderId").description("")) parameters(pathParam[Long]("orderId").description(""))
) )
get("/store/order/{orderId}", operation(getOrderByIdOperation)) { get("/store/order/:orderId",operation(getOrderByIdOperation)) {
val orderId = params.getOrElse("orderId", halt(400)) val orderId = params.getOrElse("orderId", halt(400))
println("orderId: " + orderId) println("orderId: " + orderId)
} }
val placeOrderOperation = (apiOperation[Order]("placeOrder") val placeOrderOperation = (apiOperation[Order]("placeOrder")
summary "Place an order for a pet" summary "Place an order for a pet"
parameters(bodyParam[Order]("body").description("")) parameters(bodyParam[Order]("body").description(""))
@ -90,6 +99,7 @@ class StoreApi(implicit val swagger: Swagger) extends ScalatraServlet
post("/store/order",operation(placeOrderOperation)) { post("/store/order",operation(placeOrderOperation)) {
val body = parsedBody.extract[Order] val body = parsedBody.extract[Order]
println("body: " + body) println("body: " + body)

View File

@ -51,6 +51,7 @@ class UserApi(implicit val swagger: Swagger) extends ScalatraServlet
response.headers += ("Access-Control-Allow-Origin" -> "*") response.headers += ("Access-Control-Allow-Origin" -> "*")
} }
val createUserOperation = (apiOperation[Unit]("createUser") val createUserOperation = (apiOperation[Unit]("createUser")
summary "Create user" summary "Create user"
parameters(bodyParam[User]("body").description("")) parameters(bodyParam[User]("body").description(""))
@ -58,11 +59,14 @@ class UserApi(implicit val swagger: Swagger) extends ScalatraServlet
post("/user",operation(createUserOperation)) { post("/user",operation(createUserOperation)) {
val body = parsedBody.extract[User] val body = parsedBody.extract[User]
println("body: " + body) println("body: " + body)
} }
val createUsersWithArrayInputOperation = (apiOperation[Unit]("createUsersWithArrayInput") val createUsersWithArrayInputOperation = (apiOperation[Unit]("createUsersWithArrayInput")
summary "Creates list of users with given input array" summary "Creates list of users with given input array"
parameters(bodyParam[List[User]]("body").description("")) parameters(bodyParam[List[User]]("body").description(""))
@ -70,11 +74,14 @@ class UserApi(implicit val swagger: Swagger) extends ScalatraServlet
post("/user/createWithArray",operation(createUsersWithArrayInputOperation)) { post("/user/createWithArray",operation(createUsersWithArrayInputOperation)) {
val body = parsedBody.extract[List[User]] val body = parsedBody.extract[List[User]]
println("body: " + body) println("body: " + body)
} }
val createUsersWithListInputOperation = (apiOperation[Unit]("createUsersWithListInput") val createUsersWithListInputOperation = (apiOperation[Unit]("createUsersWithListInput")
summary "Creates list of users with given input array" summary "Creates list of users with given input array"
parameters(bodyParam[List[User]]("body").description("")) parameters(bodyParam[List[User]]("body").description(""))
@ -82,35 +89,44 @@ class UserApi(implicit val swagger: Swagger) extends ScalatraServlet
post("/user/createWithList",operation(createUsersWithListInputOperation)) { post("/user/createWithList",operation(createUsersWithListInputOperation)) {
val body = parsedBody.extract[List[User]] val body = parsedBody.extract[List[User]]
println("body: " + body) println("body: " + body)
} }
val deleteUserOperation = (apiOperation[Unit]("deleteUser") val deleteUserOperation = (apiOperation[Unit]("deleteUser")
summary "Delete user" summary "Delete user"
parameters(pathParam[String]("username").description("")) parameters(pathParam[String]("username").description(""))
) )
delete("/user/{username}", operation(deleteUserOperation)) { delete("/user/:username",operation(deleteUserOperation)) {
val username = params.getOrElse("username", halt(400)) val username = params.getOrElse("username", halt(400))
println("username: " + username) println("username: " + username)
} }
val getUserByNameOperation = (apiOperation[User]("getUserByName") val getUserByNameOperation = (apiOperation[User]("getUserByName")
summary "Get user by user name" summary "Get user by user name"
parameters(pathParam[String]("username").description("")) parameters(pathParam[String]("username").description(""))
) )
get("/user/{username}", operation(getUserByNameOperation)) { get("/user/:username",operation(getUserByNameOperation)) {
val username = params.getOrElse("username", halt(400)) val username = params.getOrElse("username", halt(400))
println("username: " + username) println("username: " + username)
} }
val loginUserOperation = (apiOperation[String]("loginUser") val loginUserOperation = (apiOperation[String]("loginUser")
summary "Logs user into the system" summary "Logs user into the system"
parameters(queryParam[String]("username").description(""), queryParam[String]("password").description("")) parameters(queryParam[String]("username").description(""), queryParam[String]("password").description(""))
@ -118,15 +134,19 @@ class UserApi(implicit val swagger: Swagger) extends ScalatraServlet
get("/user/login",operation(loginUserOperation)) { get("/user/login",operation(loginUserOperation)) {
val username = params.getAs[String]("username") val username = params.getAs[String]("username")
println("username: " + username) println("username: " + username)
val password = params.getAs[String]("password") val password = params.getAs[String]("password")
println("password: " + password) println("password: " + password)
} }
val logoutUserOperation = (apiOperation[Unit]("logoutUser") val logoutUserOperation = (apiOperation[Unit]("logoutUser")
summary "Logs out current logged in user session" summary "Logs out current logged in user session"
parameters() parameters()
@ -135,17 +155,21 @@ class UserApi(implicit val swagger: Swagger) extends ScalatraServlet
get("/user/logout",operation(logoutUserOperation)) { get("/user/logout",operation(logoutUserOperation)) {
} }
val updateUserOperation = (apiOperation[Unit]("updateUser") val updateUserOperation = (apiOperation[Unit]("updateUser")
summary "Updated user" summary "Updated user"
parameters(pathParam[String]("username").description(""), bodyParam[User]("body").description("")) parameters(pathParam[String]("username").description(""), bodyParam[User]("body").description(""))
) )
put("/user/{username}", operation(updateUserOperation)) { put("/user/:username",operation(updateUserOperation)) {
val username = params.getOrElse("username", halt(400)) val username = params.getOrElse("username", halt(400))
println("username: " + username) println("username: " + username)
val body = parsedBody.extract[User] val body = parsedBody.extract[User]
println("body: " + body) println("body: " + body)

View File

@ -24,7 +24,10 @@
package com.wordnik.client.model package com.wordnik.client.model
case class ApiResponse ( case class ApiResponse (
code: Option[Int], code: Option[Int],
_type: Option[String], _type: Option[String],
message: Option[String]) message: Option[String]
)

View File

@ -24,6 +24,9 @@
package com.wordnik.client.model package com.wordnik.client.model
case class Category ( case class Category (
id: Option[Long], id: Option[Long],
name: Option[String]) name: Option[String]
)

View File

@ -26,10 +26,12 @@ package com.wordnik.client.model
import java.util.Date import java.util.Date
case class Order ( case class Order (
id: Option[Long], id: Option[Long],
petId: Option[Long], petId: Option[Long],
quantity: Option[Int], quantity: Option[Int],
shipDate: Option[Date], shipDate: Option[Date],
status: Option[String], // Order Status status: Option[String], // Order Status
complete: Option[Boolean]) complete: Option[Boolean]
)

View File

@ -27,6 +27,7 @@ package com.wordnik.client.model
import com.wordnik.client.model.Category import com.wordnik.client.model.Category
import com.wordnik.client.model.Tag import com.wordnik.client.model.Tag
case class Pet ( case class Pet (
id: Option[Long], id: Option[Long],
category: Option[Category], category: Option[Category],

View File

@ -24,6 +24,9 @@
package com.wordnik.client.model package com.wordnik.client.model
case class Tag ( case class Tag (
id: Option[Long], id: Option[Long],
name: Option[String]) name: Option[String]
)

View File

@ -24,6 +24,8 @@
package com.wordnik.client.model package com.wordnik.client.model
case class User ( case class User (
id: Option[Long], id: Option[Long],
username: Option[String], username: Option[String],