forked from loafle/openapi-generator-original
moved to client subdirectory
This commit is contained in:
parent
ad5d44769b
commit
1077e48e7e
@ -25,7 +25,7 @@ object ScalaPetstoreCodegen extends BasicScalaGenerator {
|
||||
override def invokerPackage = Some("com.wordnik.client")
|
||||
|
||||
// where to write generated code
|
||||
override def destinationDir = "samples/petstore/scala/src/main/scala"
|
||||
override def destinationDir = "samples/client/petstore/scala/src/main/scala"
|
||||
|
||||
// package for models
|
||||
override def modelPackage = Some("com.wordnik.petstore.model")
|
||||
@ -35,7 +35,7 @@ object ScalaPetstoreCodegen extends BasicScalaGenerator {
|
||||
|
||||
// supporting classes
|
||||
override def supportingFiles = List(
|
||||
("apiInvoker.mustache", "samples/petstore/scala/src/main/scala/com/wordnik/client", "ApiInvoker.scala"),
|
||||
("pom.mustache", "samples/petstore/scala", "pom.xml")
|
||||
("apiInvoker.mustache", "samples/client/petstore/scala/src/main/scala/com/wordnik/client", "ApiInvoker.scala"),
|
||||
("pom.mustache", "samples/client/petstore/scala", "pom.xml")
|
||||
)
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package com.wordnik.client
|
||||
|
||||
import com.wordnik.swagger.core.util.JsonUtil
|
||||
import com.sun.jersey.api.client.Client
|
||||
import com.sun.jersey.api.client.ClientResponse
|
||||
import com.sun.jersey.api.client.config.ClientConfig
|
||||
@ -13,7 +12,27 @@ import javax.ws.rs.core.MediaType
|
||||
import scala.collection.JavaConverters._
|
||||
import scala.collection.mutable.HashMap
|
||||
|
||||
import com.fasterxml.jackson.module.scala.DefaultScalaModule
|
||||
import com.fasterxml.jackson.core.JsonGenerator.Feature
|
||||
import com.fasterxml.jackson.databind._
|
||||
import com.fasterxml.jackson.annotation._
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize
|
||||
|
||||
object ScalaJsonUtil {
|
||||
def getJsonMapper = {
|
||||
val mapper = new ObjectMapper()
|
||||
mapper.registerModule(new DefaultScalaModule())
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT)
|
||||
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
|
||||
mapper
|
||||
}
|
||||
}
|
||||
|
||||
object ApiInvoker {
|
||||
val mapper = ScalaJsonUtil.getJsonMapper
|
||||
val defaultHeaders: HashMap[String, String] = HashMap()
|
||||
val hostMap: HashMap[String, Client] = HashMap()
|
||||
|
||||
@ -33,14 +52,14 @@ object ApiInvoker {
|
||||
} else {
|
||||
containerType match {
|
||||
case "List" => {
|
||||
val typeInfo = JsonUtil.getJsonMapper.getTypeFactory().constructCollectionType(classOf[java.util.List[_]], cls)
|
||||
val response = JsonUtil.getJsonMapper.readValue(json, typeInfo).asInstanceOf[java.util.List[_]]
|
||||
val typeInfo = mapper.getTypeFactory().constructCollectionType(classOf[java.util.List[_]], cls)
|
||||
val response = mapper.readValue(json, typeInfo).asInstanceOf[java.util.List[_]]
|
||||
response.asScala.toList
|
||||
}
|
||||
case _ => {
|
||||
json match {
|
||||
case e: String if ("\"\"" == e) => null
|
||||
case _ => JsonUtil.getJsonMapper.readValue(json, cls)
|
||||
case _ => mapper.readValue(json, cls)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -50,8 +69,8 @@ object ApiInvoker {
|
||||
def serialize(obj: AnyRef): String = {
|
||||
if (obj != null) {
|
||||
obj match {
|
||||
case e: List[_] => JsonUtil.getJsonMapper.writeValueAsString(obj.asInstanceOf[List[_]].asJava)
|
||||
case _ => JsonUtil.getJsonMapper.writeValueAsString(obj)
|
||||
case e: List[_] => mapper.writeValueAsString(obj.asInstanceOf[List[_]].asJava)
|
||||
case _ => mapper.writeValueAsString(obj)
|
||||
}
|
||||
} else null
|
||||
}
|
||||
@ -115,3 +134,4 @@ class ApiException extends Exception {
|
||||
this()
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,6 @@
|
||||
package com.wordnik.petstore.model
|
||||
|
||||
case class Category (
|
||||
id: Long,
|
||||
name: String)
|
||||
|
@ -0,0 +1,11 @@
|
||||
package com.wordnik.petstore.model
|
||||
|
||||
import java.util.Date
|
||||
case class Order (
|
||||
id: Long,
|
||||
petId: Long,
|
||||
/* Order Status */
|
||||
status: String,
|
||||
quantity: Int,
|
||||
shipDate: Date)
|
||||
|
@ -0,0 +1,13 @@
|
||||
package com.wordnik.petstore.model
|
||||
|
||||
import com.wordnik.petstore.model.Category
|
||||
import com.wordnik.petstore.model.Tag
|
||||
case class Pet (
|
||||
id: Long,
|
||||
tags: List[Tag],
|
||||
category: Category,
|
||||
/* pet status in the store */
|
||||
status: String,
|
||||
name: String,
|
||||
photoUrls: List[String])
|
||||
|
@ -0,0 +1,6 @@
|
||||
package com.wordnik.petstore.model
|
||||
|
||||
case class Tag (
|
||||
id: Long,
|
||||
name: String)
|
||||
|
@ -0,0 +1,13 @@
|
||||
package com.wordnik.petstore.model
|
||||
|
||||
case class User (
|
||||
id: Long,
|
||||
lastName: String,
|
||||
username: String,
|
||||
phone: String,
|
||||
email: String,
|
||||
/* User Status */
|
||||
userStatus: Int,
|
||||
firstName: String,
|
||||
password: String)
|
||||
|
@ -7,7 +7,7 @@ import org.scalatest.FlatSpec
|
||||
import org.scalatest.matchers.ShouldMatchers
|
||||
|
||||
import scala.collection.mutable.{ ListBuffer, HashMap }
|
||||
import scala.collection.JavaConversions._
|
||||
import scala.collection.JavaConverters._
|
||||
import scala.reflect.BeanProperty
|
||||
|
||||
@RunWith(classOf[JUnitRunner])
|
||||
@ -26,22 +26,13 @@ class PetApiTest extends FlatSpec with ShouldMatchers {
|
||||
}
|
||||
|
||||
it should "add a new pet" in {
|
||||
val pet = new Pet
|
||||
pet.id = 1000
|
||||
|
||||
pet.tags = (for (i <- (1 to 5)) yield {
|
||||
val tag = new Tag
|
||||
tag.id = i; tag.name = "tag-" + i; tag
|
||||
}).toList
|
||||
pet.status = "lost"
|
||||
pet.category = {
|
||||
val category = new Category; category.id = 1; category.name = "sold"
|
||||
category
|
||||
}
|
||||
pet.name = "dragon"
|
||||
pet.photoUrls = (for (i <- (1 to 10)) yield {
|
||||
"http://foo.com/photo/" + i
|
||||
}).toList
|
||||
val pet = Pet(
|
||||
1000,
|
||||
(for (i <- (1 to 5)) yield Tag(i, "tag-" + i)).toList,
|
||||
Category(1, "sold"),
|
||||
"lost",
|
||||
"dragon",
|
||||
(for (i <- (1 to 10)) yield "http://foo.com/photo/" + i).toList)
|
||||
|
||||
api.addPet(pet)
|
||||
api.getPetById("1000") match {
|
||||
@ -59,11 +50,14 @@ class PetApiTest extends FlatSpec with ShouldMatchers {
|
||||
}
|
||||
|
||||
it should "update a pet" in {
|
||||
val pet = new Pet
|
||||
pet.id = 1000
|
||||
val pet = Pet(
|
||||
1000,
|
||||
(for (i <- (1 to 5)) yield Tag(i, "tag-" + i)).toList,
|
||||
Category(1, "sold"),
|
||||
"confused",
|
||||
"programmer",
|
||||
(for (i <- (1 to 10)) yield "http://foo.com/photo/" + i).toList)
|
||||
|
||||
pet.name = "programmer"
|
||||
pet.status = "confused"
|
||||
api.addPet(pet)
|
||||
|
||||
api.getPetById("1000") match {
|
||||
@ -73,8 +67,8 @@ class PetApiTest extends FlatSpec with ShouldMatchers {
|
||||
}
|
||||
case None => fail("didn't find pet created")
|
||||
}
|
||||
pet.status = "fulfilled"
|
||||
api.updatePet(pet)
|
||||
val updatedPet = pet.copy(status="fulfilled")
|
||||
api.updatePet(updatedPet)
|
||||
api.getPetById("1000") match {
|
||||
case Some(pet) => {
|
||||
pet.name should be("programmer")
|
||||
@ -97,14 +91,13 @@ class PetApiTest extends FlatSpec with ShouldMatchers {
|
||||
println("finding by tags")
|
||||
api.findPetsByTags("tag1,tag2") match {
|
||||
case Some(pets) => {
|
||||
/* pets.foreach(pet => {
|
||||
pets.foreach(pet => {
|
||||
val tags = (for (tag <- pet.tags) yield tag.name).toSet
|
||||
println("checking tags " + tags)
|
||||
if ((tags & Set("tag1", "tag2")).size == 0) fail("unexpected tags in " + tags)
|
||||
if ((tags & Set("tag1", "tag2")).size == 0)
|
||||
fail("unexpected tags in " + tags)
|
||||
})
|
||||
*/
|
||||
}
|
||||
case None => //fail("didn't find pets by tag")
|
||||
case None => fail("didn't find pets by tag")
|
||||
}
|
||||
}
|
||||
}
|
@ -32,11 +32,12 @@ class StoreApiTest extends FlatSpec with ShouldMatchers {
|
||||
|
||||
it should "place an order" in {
|
||||
val now = new java.util.Date
|
||||
val order = new Order
|
||||
order.id = 1000
|
||||
order.petId = 10
|
||||
order.quantity = 101
|
||||
order.shipDate = now
|
||||
val order = Order (
|
||||
1000,
|
||||
10,
|
||||
"pending",
|
||||
101,
|
||||
now)
|
||||
|
||||
api.placeOrder(order)
|
||||
|
||||
@ -53,11 +54,12 @@ class StoreApiTest extends FlatSpec with ShouldMatchers {
|
||||
|
||||
it should "delete an order" in {
|
||||
val now = new java.util.Date
|
||||
val order = new Order
|
||||
order.id = 1001
|
||||
order.petId = 10
|
||||
order.quantity = 101
|
||||
order.shipDate = now
|
||||
val order = Order(
|
||||
1001,
|
||||
10,
|
||||
"pending",
|
||||
101,
|
||||
now)
|
||||
|
||||
api.placeOrder(order)
|
||||
|
@ -47,15 +47,15 @@ class UserApiTest extends FlatSpec with ShouldMatchers {
|
||||
}
|
||||
|
||||
it should "create a user" in {
|
||||
val user = new User
|
||||
user.id = 1002
|
||||
user.username = "johnny"
|
||||
user.password = "XXXXXXXXXXX"
|
||||
user.email = "johnny@fail.com"
|
||||
user.firstName = "Johnny"
|
||||
user.lastName = "Rocket"
|
||||
user.phone = "408-867-5309"
|
||||
user.userStatus = 1
|
||||
val user = User(
|
||||
1002,
|
||||
"Rocket",
|
||||
"johnny",
|
||||
"408-867-5309",
|
||||
"johnny@fail.com",
|
||||
1,
|
||||
"Johnny",
|
||||
"XXXXXXXXXXX")
|
||||
|
||||
api.createUser(user)
|
||||
|
||||
@ -70,16 +70,15 @@ class UserApiTest extends FlatSpec with ShouldMatchers {
|
||||
|
||||
it should "create 2 users" in {
|
||||
val userArray = (for (i <- (1 to 2)) yield {
|
||||
val user = new User
|
||||
user.id = 2000 + i
|
||||
user.username = "johnny-" + i
|
||||
user.password = "XXXXXXXXXXX"
|
||||
user.email = "johnny-" + i + "@fail.com"
|
||||
user.firstName = "Johnny"
|
||||
user.lastName = "Rocket-" + i
|
||||
user.phone = "408-867-5309"
|
||||
user.userStatus = 1
|
||||
user
|
||||
User(
|
||||
2000 + i,
|
||||
"Rocket-" + i,
|
||||
"johnny-" + i,
|
||||
"408-867-5309",
|
||||
"johnny-" + i + "@fail.com",
|
||||
1,
|
||||
"Johnny",
|
||||
"XXXXXXXXXXX")
|
||||
}).toArray
|
||||
api.createUsersWithArrayInput(userArray)
|
||||
|
||||
@ -96,16 +95,15 @@ class UserApiTest extends FlatSpec with ShouldMatchers {
|
||||
|
||||
it should "create 3 users" in {
|
||||
val userList = (for (i <- (1 to 3)) yield {
|
||||
val user = new User
|
||||
user.id = 3000 + i
|
||||
user.username = "fred-" + i
|
||||
user.password = "XXXXXXXXXXX"
|
||||
user.email = "fred-" + i + "@fail.com"
|
||||
user.firstName = "Johnny"
|
||||
user.lastName = "Rocket-" + i
|
||||
user.phone = "408-867-5309"
|
||||
user.userStatus = 1
|
||||
user
|
||||
User(
|
||||
3000 + i,
|
||||
"Rocket-" + i,
|
||||
"fred-" + i,
|
||||
"408-867-5309",
|
||||
"fred-" + i + "@fail.com",
|
||||
1,
|
||||
"Johnny",
|
||||
"XXXXXXXXXXX")
|
||||
}).toList
|
||||
api.createUsersWithListInput(userList)
|
||||
|
||||
@ -121,15 +119,15 @@ class UserApiTest extends FlatSpec with ShouldMatchers {
|
||||
}
|
||||
|
||||
it should "update a user" in {
|
||||
val user = new User
|
||||
user.id = 4000
|
||||
user.username = "tony"
|
||||
user.password = "XXXXXXXXXXX"
|
||||
user.email = "tony@fail.com"
|
||||
user.firstName = "Tony"
|
||||
user.lastName = "Tiger"
|
||||
user.phone = "408-867-5309"
|
||||
user.userStatus = 1
|
||||
val user = User(
|
||||
4000,
|
||||
"Tiger",
|
||||
"tony",
|
||||
"408-867-5309",
|
||||
"tony@fail.com",
|
||||
1,
|
||||
"Tony",
|
||||
"XXXXXXXXXXX")
|
||||
|
||||
api.createUser(user)
|
||||
|
||||
@ -140,9 +138,10 @@ class UserApiTest extends FlatSpec with ShouldMatchers {
|
||||
}
|
||||
case None =>
|
||||
}
|
||||
user.email = "tony@succeed.com"
|
||||
|
||||
api.updateUser("tony", user)
|
||||
val updatedUser = user.copy(email="tony@succeed.com")
|
||||
|
||||
api.updateUser("tony", updatedUser)
|
||||
api.getUserByName("tony") match {
|
||||
case Some(user) => {
|
||||
user.email should be ("tony@succeed.com")
|
@ -1,17 +0,0 @@
|
||||
package com.wordnik.petstore.model
|
||||
|
||||
import scala.reflect.BeanProperty
|
||||
|
||||
class Category {
|
||||
@BeanProperty var id: Long = 0L
|
||||
@BeanProperty var name: String = _
|
||||
override def toString: String = {
|
||||
val sb = new StringBuilder
|
||||
sb.append("class Category {\n")
|
||||
sb.append(" id: ").append(id).append("\n")
|
||||
sb.append(" name: ").append(name).append("\n")
|
||||
sb.append("}\n")
|
||||
sb.toString
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +0,0 @@
|
||||
package com.wordnik.petstore.model
|
||||
|
||||
import java.util.Date
|
||||
import scala.reflect.BeanProperty
|
||||
|
||||
class Order {
|
||||
@BeanProperty var id: Long = 0L
|
||||
@BeanProperty var petId: Long = 0L
|
||||
/* Order Status */
|
||||
@BeanProperty var status: String = _
|
||||
@BeanProperty var quantity: Int = 0
|
||||
@BeanProperty var shipDate: Date = _
|
||||
override def toString: String = {
|
||||
val sb = new StringBuilder
|
||||
sb.append("class Order {\n")
|
||||
sb.append(" id: ").append(id).append("\n")
|
||||
sb.append(" petId: ").append(petId).append("\n")
|
||||
sb.append(" status: ").append(status).append("\n")
|
||||
sb.append(" quantity: ").append(quantity).append("\n")
|
||||
sb.append(" shipDate: ").append(shipDate).append("\n")
|
||||
sb.append("}\n")
|
||||
sb.toString
|
||||
}
|
||||
}
|
||||
|
@ -1,28 +0,0 @@
|
||||
package com.wordnik.petstore.model
|
||||
|
||||
import com.wordnik.petstore.model.Category
|
||||
import com.wordnik.petstore.model.Tag
|
||||
import scala.reflect.BeanProperty
|
||||
|
||||
class Pet {
|
||||
@BeanProperty var id: Long = 0L
|
||||
@BeanProperty var tags: java.util.List[Tag] = _
|
||||
@BeanProperty var category: Category = _
|
||||
/* pet status in the store */
|
||||
@BeanProperty var status: String = _
|
||||
@BeanProperty var name: String = _
|
||||
@BeanProperty var photoUrls: java.util.List[String] = _
|
||||
override def toString: String = {
|
||||
val sb = new StringBuilder
|
||||
sb.append("class Pet {\n")
|
||||
sb.append(" id: ").append(id).append("\n")
|
||||
sb.append(" tags: ").append(tags).append("\n")
|
||||
sb.append(" category: ").append(category).append("\n")
|
||||
sb.append(" status: ").append(status).append("\n")
|
||||
sb.append(" name: ").append(name).append("\n")
|
||||
sb.append(" photoUrls: ").append(photoUrls).append("\n")
|
||||
sb.append("}\n")
|
||||
sb.toString
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
package com.wordnik.petstore.model
|
||||
|
||||
import scala.reflect.BeanProperty
|
||||
|
||||
class Tag {
|
||||
@BeanProperty var id: Long = 0L
|
||||
@BeanProperty var name: String = _
|
||||
override def toString: String = {
|
||||
val sb = new StringBuilder
|
||||
sb.append("class Tag {\n")
|
||||
sb.append(" id: ").append(id).append("\n")
|
||||
sb.append(" name: ").append(name).append("\n")
|
||||
sb.append("}\n")
|
||||
sb.toString
|
||||
}
|
||||
}
|
||||
|
@ -1,30 +0,0 @@
|
||||
package com.wordnik.petstore.model
|
||||
|
||||
import scala.reflect.BeanProperty
|
||||
|
||||
class User {
|
||||
@BeanProperty var id: Long = 0L
|
||||
@BeanProperty var lastName: String = _
|
||||
@BeanProperty var username: String = _
|
||||
@BeanProperty var phone: String = _
|
||||
@BeanProperty var email: String = _
|
||||
/* User Status */
|
||||
@BeanProperty var userStatus: Int = 0
|
||||
@BeanProperty var firstName: String = _
|
||||
@BeanProperty var password: String = _
|
||||
override def toString: String = {
|
||||
val sb = new StringBuilder
|
||||
sb.append("class User {\n")
|
||||
sb.append(" id: ").append(id).append("\n")
|
||||
sb.append(" lastName: ").append(lastName).append("\n")
|
||||
sb.append(" username: ").append(username).append("\n")
|
||||
sb.append(" phone: ").append(phone).append("\n")
|
||||
sb.append(" email: ").append(email).append("\n")
|
||||
sb.append(" userStatus: ").append(userStatus).append("\n")
|
||||
sb.append(" firstName: ").append(firstName).append("\n")
|
||||
sb.append(" password: ").append(password).append("\n")
|
||||
sb.append("}\n")
|
||||
sb.toString
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user