Jim Schubert 25036e48d5
[scala] Support for Set when array has uniqueItems=true (#4926)
* [scala] Set support for unique arrays

This includes and builds upon community contribution for better Set support in Scala.
It makes property + model work as expected with Set and default values
across all Scala generators. Included tests to account for new changes.

This also reverts the community contribution to remove ListBuffer
imports and change the default for array to ListBuffer. Users should use
the instantiation types map to modify the desired array instantiation
type. Any new default should target a new minor release after community
discussion, as it affects all existing SDKs generated with
openapi-generator.

* [scala] Improve default handling of monadic collection type

* [scala] Regenerate samples

* Update ScalaPlayFrameworkServerCodegen.java

Scala Play defaulted to List and should continue to do so.

Co-authored-by: František Kocun <frantisek.kocun@gmail.com>
2020-01-05 09:20:56 -05:00

145 lines
3.7 KiB
Scala

package api
import org.openapitools.OpenApiExceptions
import javax.inject.{Inject, Singleton}
import play.api.libs.json._
import play.api.mvc._
import model.User
@javax.annotation.Generated(value = Array("org.openapitools.codegen.languages.ScalaPlayFrameworkServerCodegen"), date = "2020-01-04T23:10:22.106-05:00[America/New_York]")
@Singleton
class UserApiController @Inject()(cc: ControllerComponents, api: UserApi) extends AbstractController(cc) {
/**
* POST /v2/user
*/
def createUser(): Action[AnyContent] = Action { request =>
def executeApi(): Unit = {
val body = request.body.asJson.map(_.as[User]).getOrElse {
throw new OpenApiExceptions.MissingRequiredParameterException("body", "body")
}
api.createUser(body)
}
executeApi()
Ok
}
/**
* POST /v2/user/createWithArray
*/
def createUsersWithArrayInput(): Action[AnyContent] = Action { request =>
def executeApi(): Unit = {
val body = request.body.asJson.map(_.as[List[User]]).getOrElse {
throw new OpenApiExceptions.MissingRequiredParameterException("body", "body")
}
api.createUsersWithArrayInput(body)
}
executeApi()
Ok
}
/**
* POST /v2/user/createWithList
*/
def createUsersWithListInput(): Action[AnyContent] = Action { request =>
def executeApi(): Unit = {
val body = request.body.asJson.map(_.as[List[User]]).getOrElse {
throw new OpenApiExceptions.MissingRequiredParameterException("body", "body")
}
api.createUsersWithListInput(body)
}
executeApi()
Ok
}
/**
* DELETE /v2/user/:username
* @param username The name that needs to be deleted
*/
def deleteUser(username: String): Action[AnyContent] = Action { request =>
def executeApi(): Unit = {
api.deleteUser(username)
}
executeApi()
Ok
}
/**
* GET /v2/user/:username
* @param username The name that needs to be fetched. Use user1 for testing.
*/
def getUserByName(username: String): Action[AnyContent] = Action { request =>
def executeApi(): User = {
api.getUserByName(username)
}
val result = executeApi()
val json = Json.toJson(result)
Ok(json)
}
/**
* GET /v2/user/login?username=[value]&password=[value]
*/
def loginUser(): Action[AnyContent] = Action { request =>
def executeApi(): String = {
val username = request.getQueryString("username")
.getOrElse {
throw new OpenApiExceptions.MissingRequiredParameterException("username", "query string")
}
val password = request.getQueryString("password")
.getOrElse {
throw new OpenApiExceptions.MissingRequiredParameterException("password", "query string")
}
api.loginUser(username, password)
}
val result = executeApi()
val json = Json.toJson(result)
Ok(json)
}
/**
* GET /v2/user/logout
*/
def logoutUser(): Action[AnyContent] = Action { request =>
def executeApi(): Unit = {
api.logoutUser()
}
executeApi()
Ok
}
/**
* PUT /v2/user/:username
* @param username name that need to be deleted
*/
def updateUser(username: String): Action[AnyContent] = Action { request =>
def executeApi(): Unit = {
val body = request.body.asJson.map(_.as[User]).getOrElse {
throw new OpenApiExceptions.MissingRequiredParameterException("body", "body")
}
api.updateUser(username, body)
}
executeApi()
Ok
}
private def splitCollectionParam(paramValues: String, collectionFormat: String): List[String] = {
val splitBy =
collectionFormat match {
case "csv" => ",+"
case "tsv" => "\t+"
case "ssv" => " +"
case "pipes" => "|+"
}
paramValues.split(splitBy).toList
}
}