[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.CodegenConstants;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen;
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");
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation op : operationList) {
// force http method to lower case
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;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -26,10 +26,12 @@ package com.wordnik.client.model
import java.util.Date
case class Order (
id: Option[Long],
petId: Option[Long],
quantity: Option[Int],
shipDate: Option[Date],
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.Tag
case class Pet (
id: Option[Long],
category: Option[Category],

View File

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

View File

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