Skip to content

Commit

Permalink
Updated samples
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronp committed Nov 7, 2024
1 parent b06cf65 commit 1cb59a8
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,8 @@ private static boolean importResponseImplicits(final CodegenOperation op) {
boolean doImport = false;
for (var response : op.responses) {
if (response.isModel && !importBlacklist.contains(response.dataType)) {
System.out.println("is model: " + response.dataType);
doImport = true;
break;
}
}
return doImport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ class PetRoutes(service : PetService[Try]) extends cask.Routes {
val result = for {
petJson <- Parsed.fromTry(request.bodyAsJson)
petData <- Parsed.eval(PetData.fromJson(petJson)) /* not array or map */
pet <- Parsed.fromTry(petData.validated(failFast))
pet <- Parsed.fromTry(PetData.validated(petData, failFast))
resultTry <- Parsed.eval(service.addPet(pet))
result <- Parsed.fromTry(resultTry)
} yield result

// this is required for union (oneOf) types
import Pet.{given, *}

(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(value : Pet) => cask.Response(data = write(value), 200, headers = Seq("Content-Type" -> "application/json"))
Expand All @@ -90,6 +93,7 @@ class PetRoutes(service : PetService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result


(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(other) => cask.Response(s"$other", 200)
Expand All @@ -109,6 +113,7 @@ class PetRoutes(service : PetService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result


(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(value : List[Pet]) => cask.Response(data = write(value), 200, headers = Seq("Content-Type" -> "application/json"))
Expand All @@ -129,6 +134,7 @@ class PetRoutes(service : PetService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result


(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(value : List[Pet]) => cask.Response(data = write(value), 200, headers = Seq("Content-Type" -> "application/json"))
Expand All @@ -150,6 +156,9 @@ class PetRoutes(service : PetService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result

// this is required for union (oneOf) types
import Pet.{given, *}

(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(value : Pet) => cask.Response(data = write(value), 200, headers = Seq("Content-Type" -> "application/json"))
Expand All @@ -168,11 +177,14 @@ class PetRoutes(service : PetService[Try]) extends cask.Routes {
val result = for {
petJson <- Parsed.fromTry(request.bodyAsJson)
petData <- Parsed.eval(PetData.fromJson(petJson)) /* not array or map */
pet <- Parsed.fromTry(petData.validated(failFast))
pet <- Parsed.fromTry(PetData.validated(petData, failFast))
resultTry <- Parsed.eval(service.updatePet(pet))
result <- Parsed.fromTry(resultTry)
} yield result

// this is required for union (oneOf) types
import Pet.{given, *}

(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(value : Pet) => cask.Response(data = write(value), 200, headers = Seq("Content-Type" -> "application/json"))
Expand All @@ -196,6 +208,7 @@ class PetRoutes(service : PetService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result


(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(other) => cask.Response(s"$other", 200)
Expand All @@ -218,6 +231,9 @@ class PetRoutes(service : PetService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result

// this is required for union (oneOf) types
import ApiResponse.{given, *}

(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(value : ApiResponse) => cask.Response(data = write(value), 200, headers = Seq("Content-Type" -> "application/json"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class StoreRoutes(service : StoreService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result


(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(other) => cask.Response(s"$other", 200)
Expand All @@ -62,6 +63,9 @@ class StoreRoutes(service : StoreService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result

// this is required for union (oneOf) types
import Map[String, Int].{given, *}

(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(value : Map[String, Int]) => cask.Response(data = write(value), 200, headers = Seq("Content-Type" -> "application/json"))
Expand All @@ -82,6 +86,9 @@ class StoreRoutes(service : StoreService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result

// this is required for union (oneOf) types
import Order.{given, *}

(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(value : Order) => cask.Response(data = write(value), 200, headers = Seq("Content-Type" -> "application/json"))
Expand All @@ -99,11 +106,14 @@ class StoreRoutes(service : StoreService[Try]) extends cask.Routes {
val result = for {
orderJson <- Parsed.fromTry(request.bodyAsJson)
orderData <- Parsed.eval(OrderData.fromJson(orderJson)) /* not array or map */
order <- Parsed.fromTry(orderData.validated(failFast))
order <- Parsed.fromTry(OrderData.validated(orderData, failFast))
resultTry <- Parsed.eval(service.placeOrder(order))
result <- Parsed.fromTry(resultTry)
} yield result

// this is required for union (oneOf) types
import Order.{given, *}

(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(value : Order) => cask.Response(data = write(value), 200, headers = Seq("Content-Type" -> "application/json"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ class UserRoutes(service : UserService[Try]) extends cask.Routes {
val result = for {
userJson <- Parsed.fromTry(request.bodyAsJson)
userData <- Parsed.eval(UserData.fromJson(userJson)) /* not array or map */
user <- Parsed.fromTry(userData.validated(failFast))
user <- Parsed.fromTry(UserData.validated(userData, failFast))
resultTry <- Parsed.eval(service.createUser(user))
result <- Parsed.fromTry(resultTry)
} yield result


(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(other) => cask.Response(s"$other", 200)
Expand All @@ -77,6 +78,7 @@ class UserRoutes(service : UserService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result


(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(other) => cask.Response(s"$other", 200)
Expand All @@ -97,6 +99,7 @@ class UserRoutes(service : UserService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result


(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(other) => cask.Response(s"$other", 200)
Expand All @@ -117,6 +120,7 @@ class UserRoutes(service : UserService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result


(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(other) => cask.Response(s"$other", 200)
Expand All @@ -136,6 +140,9 @@ class UserRoutes(service : UserService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result

// this is required for union (oneOf) types
import User.{given, *}

(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(value : User) => cask.Response(data = write(value), 200, headers = Seq("Content-Type" -> "application/json"))
Expand All @@ -155,6 +162,7 @@ class UserRoutes(service : UserService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result


(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(value : String) => cask.Response(data = write(value), 200, headers = Seq("Content-Type" -> "application/json"))
Expand All @@ -175,6 +183,7 @@ class UserRoutes(service : UserService[Try]) extends cask.Routes {
result <- Parsed.fromTry(resultTry)
} yield result


(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(other) => cask.Response(s"$other", 200)
Expand All @@ -193,11 +202,12 @@ class UserRoutes(service : UserService[Try]) extends cask.Routes {
username <- Parsed(username)
userJson <- Parsed.fromTry(request.bodyAsJson)
userData <- Parsed.eval(UserData.fromJson(userJson)) /* not array or map */
user <- Parsed.fromTry(userData.validated(failFast))
user <- Parsed.fromTry(UserData.validated(userData, failFast))
resultTry <- Parsed.eval(service.updateUser(username, user))
result <- Parsed.fromTry(resultTry)
} yield result


(result : @unchecked) match {
case Left(error) => cask.Response(error, 500)
case Right(other) => cask.Response(s"$other", 200)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.9.9
sbt.version=1.10.3
2 changes: 1 addition & 1 deletion samples/server/petstore/scala-cask/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.2.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.17.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.2.0")
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import upickle.default.{ReadWriter => RW, macroRW}
import upickle.default.*



case class ApiResponse(
code: Option[Int] = None ,
`type`: Option[String] = None ,
Expand Down Expand Up @@ -55,3 +55,4 @@ enum Fields(val fieldName : String) extends Field(fieldName) {

}


Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ case class ApiResponseData(
}

def validationErrors(path : Seq[Field], failFast : Boolean) : Seq[ValidationError] = {
val errors = scala.collection.mutable.ListBuffer[ValidationError]()
val _allValidationErrors = scala.collection.mutable.ListBuffer[ValidationError]()
// ================== code validation ==================


Expand All @@ -59,7 +59,7 @@ case class ApiResponseData(



errors.toSeq
_allValidationErrors.toSeq
}

/**
Expand All @@ -85,6 +85,8 @@ case class ApiResponseData(

object ApiResponseData {

def validated(d8a : ApiResponseData, failFast : Boolean) : scala.util.Try[ApiResponse] = d8a.validated(failFast)

def fromJson(jason : ujson.Value) : ApiResponseData = try {
val data = read[ApiResponseData](jason)
data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import upickle.default.{ReadWriter => RW, macroRW}
import upickle.default.*



case class Category(
id: Option[Long] = None ,
name: Option[String] = None
Expand Down Expand Up @@ -52,3 +52,4 @@ enum Fields(val fieldName : String) extends Field(fieldName) {

}


Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ case class CategoryData(
}

def validationErrors(path : Seq[Field], failFast : Boolean) : Seq[ValidationError] = {
val errors = scala.collection.mutable.ListBuffer[ValidationError]()
val _allValidationErrors = scala.collection.mutable.ListBuffer[ValidationError]()
// ================== id validation ==================


Expand All @@ -48,17 +48,17 @@ case class CategoryData(

// ================== name validation ==================
// validate against pattern '^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$'
if (errors.isEmpty || !failFast) {
if (_allValidationErrors.isEmpty || !failFast) {
val regex = """^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$"""
if name == null || !regex.r.matches(name) then
errors += ValidationError(path :+ Category.Fields.name, s"value '$name' doesn't match pattern $regex")
_allValidationErrors += ValidationError(path :+ Category.Fields.name, s"value '$name' doesn't match pattern $regex")
}





errors.toSeq
_allValidationErrors.toSeq
}

/**
Expand All @@ -83,6 +83,8 @@ case class CategoryData(

object CategoryData {

def validated(d8a : CategoryData, failFast : Boolean) : scala.util.Try[Category] = d8a.validated(failFast)

def fromJson(jason : ujson.Value) : CategoryData = try {
val data = read[CategoryData](jason)
data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import upickle.default.{ReadWriter => RW, macroRW}
import upickle.default.*



case class Order(
id: Option[Long] = None ,
petId: Option[Long] = None ,
Expand Down Expand Up @@ -73,3 +73,4 @@ enum Fields(val fieldName : String) extends Field(fieldName) {

}


Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ case class OrderData(
}

def validationErrors(path : Seq[Field], failFast : Boolean) : Seq[ValidationError] = {
val errors = scala.collection.mutable.ListBuffer[ValidationError]()
val _allValidationErrors = scala.collection.mutable.ListBuffer[ValidationError]()
// ================== id validation ==================


Expand Down Expand Up @@ -82,7 +82,7 @@ case class OrderData(



errors.toSeq
_allValidationErrors.toSeq
}

/**
Expand Down Expand Up @@ -111,6 +111,8 @@ case class OrderData(

object OrderData {

def validated(d8a : OrderData, failFast : Boolean) : scala.util.Try[Order] = d8a.validated(failFast)

def fromJson(jason : ujson.Value) : OrderData = try {
val data = read[OrderData](jason)
data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import upickle.default.{ReadWriter => RW, macroRW}
import upickle.default.*



case class Pet(
id: Option[Long] = None ,
category: Option[Category] = None ,
Expand Down Expand Up @@ -72,3 +72,4 @@ enum Fields(val fieldName : String) extends Field(fieldName) {

}


Loading

0 comments on commit 1cb59a8

Please sign in to comment.