Scala cask api effects (#19936)

* Scala-cask improvements:

 * fixe for grouped methods which have routes containing dashes.

Previously our OperationGroup work-around would potentially
Create methods like ‘foo-bar’, which isn’t a valid function name

 * Fix to not import some.package.Array[Byte] when binary format is specified

 * Fix for grouped operations which contain duplicate query parameters

 * Fix for binary response fields. This can come up with the following example

        "responses" : {
          "200" : {
            "content" : {
              "application/json" : {
                "schema" : {
                  "format" : "binary",
                  "type" : "string"
                }
              }
            },
            "description" : "data"
          },

 * Fix for enum model classes
Extracted complex logic for ‘asData’ and ‘asModel’ transformations for properties

 * Introduced a generic effect F[_] for services

This was done to support composable services
(Service A calls Service B) by using monadic
Effect types (ones which can flatMap)

 * Fixed unique union types for responses, asModel and asData fixes for non-model types

* scala-cask: regenerated samples

* Fix for reserved-word properties in the API

* Fix for null imports and reserved-word enum types

* Fixes for api methods with backticked params

* Fix for duplicate (by name) grouped params

* small syntax fix

* logging response type

* Regenerated samples

* String.format fix
This commit is contained in:
Aaron Pritzlaff
2024-11-06 08:14:31 +00:00
committed by GitHub
parent cded99c3fc
commit b51b18e3ca
41 changed files with 1725 additions and 821 deletions

View File

@@ -14,10 +14,10 @@
* https://openapi-generator.tech
*/
// this file was generated from app.mustache
package cask.groupId.server
import scala.util.Try
import _root_.sample.cask.model.*
import _root_.sample.cask.api.*
@@ -30,20 +30,23 @@ import _root_.sample.cask.api.*
* If you wanted fine-grained control over the routes and services, you could
* extend the cask.MainRoutes and mix in this trait by using this:
*
* \{\{\{
* ```
* override def allRoutes = appRoutes
* \}\}\}
* ```
*
* More typically, however, you would extend the 'BaseApp' class
*/
trait AppRoutes {
def appPetService : PetService = PetService()
def appPetService : PetService[Try] = PetService()
def routeForPet : PetRoutes = PetRoutes(appPetService)
def appStoreService : StoreService = StoreService()
def appStoreService : StoreService[Try] = StoreService()
def routeForStore : StoreRoutes = StoreRoutes(appStoreService)
def appUserService : UserService = UserService()
def appUserService : UserService[Try] = UserService()
def routeForUser : UserRoutes = UserRoutes(appUserService)
def appRoutes = Seq(
routeForPet ,
routeForStore ,

View File

@@ -18,6 +18,7 @@
// this file was generated from app.mustache
package cask.groupId.server
import scala.util.Try
import _root_.sample.cask.model.*
import _root_.sample.cask.api.*
@@ -26,11 +27,11 @@ import _root_.sample.cask.api.*
* passing in the custom business logic services
*/
class BaseApp(
override val appPetService : PetService = PetService(),
override val appPetService : PetService[Try] = PetService(),
override val appStoreService : StoreService = StoreService(),
override val appStoreService : StoreService[Try] = StoreService(),
override val appUserService : UserService = UserService(),
override val appUserService : UserService[Try] = UserService(),
override val port : Int = sys.env.get("PORT").map(_.toInt).getOrElse(8080)) extends cask.MainRoutes with AppRoutes {
/** routes for the UI

View File

@@ -22,12 +22,13 @@ import sample.cask.model.*
import upickle.default.{ReadWriter => RW, macroRW}
import upickle.default.*
import scala.util.Try
import sample.cask.model.ApiResponse
import java.io.File
import sample.cask.model.Pet
class PetRoutes(service : PetService) extends cask.Routes {
class PetRoutes(service : PetService[Try]) extends cask.Routes {
// route group for routeWorkAroundForPOSTPet
@cask.post("/pet", true)
@@ -63,7 +64,8 @@ class PetRoutes(service : PetService) extends cask.Routes {
petJson <- Parsed.fromTry(request.bodyAsJson)
petData <- Parsed.eval(PetData.fromJson(petJson)) /* not array or map */
pet <- Parsed.fromTry(petData.validated(failFast))
result <- Parsed.eval(service.addPet(pet))
resultTry <- Parsed.eval(service.addPet(pet))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -84,7 +86,8 @@ class PetRoutes(service : PetService) extends cask.Routes {
val result = for {
petId <- Parsed(petId)
apiKey <- request.headerSingleValueOptional("apiKey")
result <- Parsed.eval(service.deletePet(petId, apiKey))
resultTry <- Parsed.eval(service.deletePet(petId, apiKey))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -102,7 +105,8 @@ class PetRoutes(service : PetService) extends cask.Routes {
def failFast = request.queryParams.keySet.contains("failFast")
val result = for {
result <- Parsed.eval(service.findPetsByStatus(status))
resultTry <- Parsed.eval(service.findPetsByStatus(status))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -121,7 +125,8 @@ class PetRoutes(service : PetService) extends cask.Routes {
def failFast = request.queryParams.keySet.contains("failFast")
val result = for {
result <- Parsed.eval(service.findPetsByTags(tags))
resultTry <- Parsed.eval(service.findPetsByTags(tags))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -141,7 +146,8 @@ class PetRoutes(service : PetService) extends cask.Routes {
val result = for {
petId <- Parsed(petId)
result <- Parsed.eval(service.getPetById(petId))
resultTry <- Parsed.eval(service.getPetById(petId))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -163,7 +169,8 @@ class PetRoutes(service : PetService) extends cask.Routes {
petJson <- Parsed.fromTry(request.bodyAsJson)
petData <- Parsed.eval(PetData.fromJson(petJson)) /* not array or map */
pet <- Parsed.fromTry(petData.validated(failFast))
result <- Parsed.eval(service.updatePet(pet))
resultTry <- Parsed.eval(service.updatePet(pet))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -185,7 +192,8 @@ class PetRoutes(service : PetService) extends cask.Routes {
petId <- Parsed(petId)
name <- request.formSingleValueOptional("name")
status <- request.formSingleValueOptional("status")
result <- Parsed.eval(service.updatePetWithForm(petId, name, status))
resultTry <- Parsed.eval(service.updatePetWithForm(petId, name, status))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -206,7 +214,8 @@ class PetRoutes(service : PetService) extends cask.Routes {
petId <- Parsed(petId)
additionalMetadata <- request.formSingleValueOptional("additionalMetadata")
file <- request.formValueAsFileOptional("file")
result <- Parsed.eval(service.uploadFile(petId, additionalMetadata, file))
resultTry <- Parsed.eval(service.uploadFile(petId, additionalMetadata, file))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {

View File

@@ -22,10 +22,11 @@ import sample.cask.model.*
import upickle.default.{ReadWriter => RW, macroRW}
import upickle.default.*
import scala.util.Try
import sample.cask.model.Order
class StoreRoutes(service : StoreService) extends cask.Routes {
class StoreRoutes(service : StoreService[Try]) extends cask.Routes {
/** Delete purchase order by ID
@@ -38,7 +39,8 @@ class StoreRoutes(service : StoreService) extends cask.Routes {
val result = for {
orderId <- Parsed(orderId)
result <- Parsed.eval(service.deleteOrder(orderId))
resultTry <- Parsed.eval(service.deleteOrder(orderId))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -56,7 +58,8 @@ class StoreRoutes(service : StoreService) extends cask.Routes {
def failFast = request.queryParams.keySet.contains("failFast")
val result = for {
result <- Parsed.eval(service.getInventory())
resultTry <- Parsed.eval(service.getInventory())
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -75,7 +78,8 @@ class StoreRoutes(service : StoreService) extends cask.Routes {
val result = for {
orderId <- Parsed(orderId)
result <- Parsed.eval(service.getOrderById(orderId))
resultTry <- Parsed.eval(service.getOrderById(orderId))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -96,7 +100,8 @@ class StoreRoutes(service : StoreService) extends cask.Routes {
orderJson <- Parsed.fromTry(request.bodyAsJson)
orderData <- Parsed.eval(OrderData.fromJson(orderJson)) /* not array or map */
order <- Parsed.fromTry(orderData.validated(failFast))
result <- Parsed.eval(service.placeOrder(order))
resultTry <- Parsed.eval(service.placeOrder(order))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {

View File

@@ -22,11 +22,12 @@ import sample.cask.model.*
import upickle.default.{ReadWriter => RW, macroRW}
import upickle.default.*
import scala.util.Try
import java.time.OffsetDateTime
import sample.cask.model.User
class UserRoutes(service : UserService) extends cask.Routes {
class UserRoutes(service : UserService[Try]) extends cask.Routes {
// route group for routeWorkAroundForGETUser
@cask.get("/user", true)
@@ -52,7 +53,8 @@ class UserRoutes(service : UserService) extends cask.Routes {
userJson <- Parsed.fromTry(request.bodyAsJson)
userData <- Parsed.eval(UserData.fromJson(userJson)) /* not array or map */
user <- Parsed.fromTry(userData.validated(failFast))
result <- Parsed.eval(service.createUser(user))
resultTry <- Parsed.eval(service.createUser(user))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -71,7 +73,8 @@ class UserRoutes(service : UserService) extends cask.Routes {
val result = for {
user <- Parsed.fromTry(UserData.manyFromJsonStringValidated(request.bodyAsString)).mapError(e => s"Error parsing json as an array of User from >${request.bodyAsString}< : ${e}") /* array */
result <- Parsed.eval(service.createUsersWithArrayInput(user))
resultTry <- Parsed.eval(service.createUsersWithArrayInput(user))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -90,7 +93,8 @@ class UserRoutes(service : UserService) extends cask.Routes {
val result = for {
user <- Parsed.fromTry(UserData.manyFromJsonStringValidated(request.bodyAsString)).mapError(e => s"Error parsing json as an array of User from >${request.bodyAsString}< : ${e}") /* array */
result <- Parsed.eval(service.createUsersWithListInput(user))
resultTry <- Parsed.eval(service.createUsersWithListInput(user))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -109,7 +113,8 @@ class UserRoutes(service : UserService) extends cask.Routes {
val result = for {
username <- Parsed(username)
result <- Parsed.eval(service.deleteUser(username))
resultTry <- Parsed.eval(service.deleteUser(username))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -127,7 +132,8 @@ class UserRoutes(service : UserService) extends cask.Routes {
val result = for {
username <- Parsed(username)
result <- Parsed.eval(service.getUserByName(username))
resultTry <- Parsed.eval(service.getUserByName(username))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -145,7 +151,8 @@ class UserRoutes(service : UserService) extends cask.Routes {
def failFast = request.queryParams.keySet.contains("failFast")
val result = for {
result <- Parsed.eval(service.loginUser(username, password))
resultTry <- Parsed.eval(service.loginUser(username, password))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -164,7 +171,8 @@ class UserRoutes(service : UserService) extends cask.Routes {
def failFast = request.queryParams.keySet.contains("failFast")
val result = for {
result <- Parsed.eval(service.logoutUser())
resultTry <- Parsed.eval(service.logoutUser())
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {
@@ -186,7 +194,8 @@ class UserRoutes(service : UserService) extends cask.Routes {
userJson <- Parsed.fromTry(request.bodyAsJson)
userData <- Parsed.eval(UserData.fromJson(userJson)) /* not array or map */
user <- Parsed.fromTry(userData.validated(failFast))
result <- Parsed.eval(service.updateUser(username, user))
resultTry <- Parsed.eval(service.updateUser(username, user))
result <- Parsed.fromTry(resultTry)
} yield result
(result : @unchecked) match {

View File

@@ -25,6 +25,16 @@ import scala.reflect.ClassTag
import scala.util.*
import upickle.default.*
extension (f: java.io.File) {
def bytes: Array[Byte] = java.nio.file.Files.readAllBytes(f.toPath)
def toBase64: String = java.util.Base64.getEncoder.encodeToString(bytes)
}
given Writer[java.io.File] = new Writer[java.io.File] {
def write0[V](out: upickle.core.Visitor[?, V], v: java.io.File) = out.visitString(v.toBase64, -1)
}
// needed for BigDecimal params
given cask.endpoints.QueryParamReader.SimpleParam[BigDecimal](BigDecimal.apply)

View File

@@ -1,15 +1,14 @@
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
* Contact: team@openapitools.org
*
* NOTE: This class is auto generated by OpenAPI Generator.
*
* https://openapi-generator.tech
*/
/** OpenAPI Petstore This is a sample server Petstore server. For this sample, you can use the api
* key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
* Contact: team@openapitools.org
*
* NOTE: This class is auto generated by OpenAPI Generator.
*
* https://openapi-generator.tech
*/
// this model was generated using modelTest.mustache
package sample.cask.model
@@ -20,16 +19,16 @@ import scala.util.*
class ApiResponseTest extends AnyWordSpec with Matchers {
"ApiResponse.fromJson" should {
"""not parse invalid json""" in {
val Failure(err) = Try(ApiResponseData.fromJsonString("invalid jason"))
err.getMessage should startWith ("Error parsing json 'invalid jason'")
}
"""parse """ ignore {
val Failure(err : ValidationErrors) = ApiResponseData.fromJsonString("""""").validated()
sys.error("TODO")
}
"ApiResponse.fromJson" should {
"""not parse invalid json""" in {
val Failure(err) = Try(ApiResponseData.fromJsonString("invalid jason"))
err.getMessage should startWith("Error parsing json 'invalid jason'")
}
"""parse """ ignore {
val Failure(err: ValidationErrors) = ApiResponseData.fromJsonString("""""").validated()
sys.error("TODO")
}
}
}

View File

@@ -1,15 +1,14 @@
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
* Contact: team@openapitools.org
*
* NOTE: This class is auto generated by OpenAPI Generator.
*
* https://openapi-generator.tech
*/
/** OpenAPI Petstore This is a sample server Petstore server. For this sample, you can use the api
* key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
* Contact: team@openapitools.org
*
* NOTE: This class is auto generated by OpenAPI Generator.
*
* https://openapi-generator.tech
*/
// this model was generated using modelTest.mustache
package sample.cask.model
@@ -20,16 +19,16 @@ import scala.util.*
class CategoryTest extends AnyWordSpec with Matchers {
"Category.fromJson" should {
"""not parse invalid json""" in {
val Failure(err) = Try(CategoryData.fromJsonString("invalid jason"))
err.getMessage should startWith ("Error parsing json 'invalid jason'")
}
"""parse """ ignore {
val Failure(err : ValidationErrors) = CategoryData.fromJsonString("""""").validated()
sys.error("TODO")
}
"Category.fromJson" should {
"""not parse invalid json""" in {
val Failure(err) = Try(CategoryData.fromJsonString("invalid jason"))
err.getMessage should startWith("Error parsing json 'invalid jason'")
}
"""parse """ ignore {
val Failure(err: ValidationErrors) = CategoryData.fromJsonString("""""").validated()
sys.error("TODO")
}
}
}

View File

@@ -1,15 +1,14 @@
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
* Contact: team@openapitools.org
*
* NOTE: This class is auto generated by OpenAPI Generator.
*
* https://openapi-generator.tech
*/
/** OpenAPI Petstore This is a sample server Petstore server. For this sample, you can use the api
* key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
* Contact: team@openapitools.org
*
* NOTE: This class is auto generated by OpenAPI Generator.
*
* https://openapi-generator.tech
*/
// this model was generated using modelTest.mustache
package sample.cask.model
@@ -21,16 +20,16 @@ import scala.util.*
class OrderTest extends AnyWordSpec with Matchers {
"Order.fromJson" should {
"""not parse invalid json""" in {
val Failure(err) = Try(OrderData.fromJsonString("invalid jason"))
err.getMessage should startWith ("Error parsing json 'invalid jason'")
}
"""parse """ ignore {
val Failure(err : ValidationErrors) = OrderData.fromJsonString("""""").validated()
sys.error("TODO")
}
"Order.fromJson" should {
"""not parse invalid json""" in {
val Failure(err) = Try(OrderData.fromJsonString("invalid jason"))
err.getMessage should startWith("Error parsing json 'invalid jason'")
}
"""parse """ ignore {
val Failure(err: ValidationErrors) = OrderData.fromJsonString("""""").validated()
sys.error("TODO")
}
}
}

View File

@@ -1,20 +1,17 @@
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
* Contact: team@openapitools.org
*
* NOTE: This class is auto generated by OpenAPI Generator.
*
* https://openapi-generator.tech
*/
/** OpenAPI Petstore This is a sample server Petstore server. For this sample, you can use the api
* key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
* Contact: team@openapitools.org
*
* NOTE: This class is auto generated by OpenAPI Generator.
*
* https://openapi-generator.tech
*/
// this model was generated using modelTest.mustache
package sample.cask.model
import sample.cask.model.Category
import sample.cask.model.Tag
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
@@ -22,16 +19,16 @@ import scala.util.*
class PetTest extends AnyWordSpec with Matchers {
"Pet.fromJson" should {
"""not parse invalid json""" in {
val Failure(err) = Try(PetData.fromJsonString("invalid jason"))
err.getMessage should startWith ("Error parsing json 'invalid jason'")
}
"""parse """ ignore {
val Failure(err : ValidationErrors) = PetData.fromJsonString("""""").validated()
sys.error("TODO")
}
"Pet.fromJson" should {
"""not parse invalid json""" in {
val Failure(err) = Try(PetData.fromJsonString("invalid jason"))
err.getMessage should startWith("Error parsing json 'invalid jason'")
}
"""parse """ ignore {
val Failure(err: ValidationErrors) = PetData.fromJsonString("""""").validated()
sys.error("TODO")
}
}
}

View File

@@ -1,15 +1,14 @@
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
* Contact: team@openapitools.org
*
* NOTE: This class is auto generated by OpenAPI Generator.
*
* https://openapi-generator.tech
*/
/** OpenAPI Petstore This is a sample server Petstore server. For this sample, you can use the api
* key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
* Contact: team@openapitools.org
*
* NOTE: This class is auto generated by OpenAPI Generator.
*
* https://openapi-generator.tech
*/
// this model was generated using modelTest.mustache
package sample.cask.model
@@ -20,16 +19,16 @@ import scala.util.*
class TagTest extends AnyWordSpec with Matchers {
"Tag.fromJson" should {
"""not parse invalid json""" in {
val Failure(err) = Try(TagData.fromJsonString("invalid jason"))
err.getMessage should startWith ("Error parsing json 'invalid jason'")
}
"""parse """ ignore {
val Failure(err : ValidationErrors) = TagData.fromJsonString("""""").validated()
sys.error("TODO")
}
"Tag.fromJson" should {
"""not parse invalid json""" in {
val Failure(err) = Try(TagData.fromJsonString("invalid jason"))
err.getMessage should startWith("Error parsing json 'invalid jason'")
}
"""parse """ ignore {
val Failure(err: ValidationErrors) = TagData.fromJsonString("""""").validated()
sys.error("TODO")
}
}
}

View File

@@ -1,15 +1,14 @@
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
* Contact: team@openapitools.org
*
* NOTE: This class is auto generated by OpenAPI Generator.
*
* https://openapi-generator.tech
*/
/** OpenAPI Petstore This is a sample server Petstore server. For this sample, you can use the api
* key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
* Contact: team@openapitools.org
*
* NOTE: This class is auto generated by OpenAPI Generator.
*
* https://openapi-generator.tech
*/
// this model was generated using modelTest.mustache
package sample.cask.model
@@ -20,16 +19,16 @@ import scala.util.*
class UserTest extends AnyWordSpec with Matchers {
"User.fromJson" should {
"""not parse invalid json""" in {
val Failure(err) = Try(UserData.fromJsonString("invalid jason"))
err.getMessage should startWith ("Error parsing json 'invalid jason'")
}
"""parse """ ignore {
val Failure(err : ValidationErrors) = UserData.fromJsonString("""""").validated()
sys.error("TODO")
}
"User.fromJson" should {
"""not parse invalid json""" in {
val Failure(err) = Try(UserData.fromJsonString("invalid jason"))
err.getMessage should startWith("Error parsing json 'invalid jason'")
}
"""parse """ ignore {
val Failure(err: ValidationErrors) = UserData.fromJsonString("""""").validated()
sys.error("TODO")
}
}
}