diff --git a/api/app/models/VersionsModel.scala b/api/app/models/VersionsModel.scala index 066cf5c3a..ed2f015ec 100644 --- a/api/app/models/VersionsModel.scala +++ b/api/app/models/VersionsModel.scala @@ -1,6 +1,7 @@ package models import builder.api_json.upgrades.ServiceParser +import cats.data.Validated.{Invalid, Valid} import cats.data.ValidatedNec import cats.implicits._ import db.{ApplicationsDao, Authorization, InternalVersion, OrganizationsDao} @@ -20,6 +21,16 @@ class VersionsModel @Inject()( } def toModels(versions: Seq[InternalVersion]): Seq[Version] = { + validateModels(versions) match { + case Invalid(e) => { + println(s"Could not convert versions to models: ${e.toNonEmptyList.toList.mkString(", ")}") + Nil + } + case Valid(versions) => versions + } + } + + private def validateModels(versions: Seq[InternalVersion]): ValidatedNec[String, Seq[Version]] = { val applications = applicationsDao.findAll( Authorization.All, guids = Some(versions.map(_.applicationGuid)), @@ -32,25 +43,26 @@ class VersionsModel @Inject()( limit = None ).map { o => o.guid -> o }.toMap - versions.flatMap { v => + versions.map { v => ( - applications.get(v.applicationGuid).flatMap { app => - organizations.get(app.organizationGuid).flatMap { org => - service(v).toOption.map { svc => - Version( - guid = v.guid, - organization = Reference(guid = org.guid, key = org.key), - application = Reference(guid = app.guid, key = app.key), - version = v.version, - original = v.original, - service = svc, - audit = v.audit - ) - } + applications.get(v.applicationGuid).toValidNec("Cannot find application").andThen { app => + organizations.get(app.organizationGuid).toValidNec("Cannot find organization").map { org => + (org, app) } - } - ) - } + }, + service(v) + ).mapN { case ((org, app), svc) => + Version( + guid = v.guid, + organization = Reference(guid = org.guid, key = org.key), + application = Reference(guid = app.guid, key = app.key), + version = v.version, + original = v.original, + service = svc, + audit = v.audit + ) + } + }.sequence } private def service(v: InternalVersion): ValidatedNec[String, Service] = { diff --git a/generated/app/ApicollectiveApibuilderSpecV0Client.scala b/generated/app/ApicollectiveApibuilderSpecV0Client.scala index 78fc3e50c..461b713fa 100644 --- a/generated/app/ApicollectiveApibuilderSpecV0Client.scala +++ b/generated/app/ApicollectiveApibuilderSpecV0Client.scala @@ -1,6 +1,6 @@ /** * Generated by API Builder - https://www.apibuilder.io - * Service version: 0.16.50 + * Service version: 0.16.53 * User agent: apibuilder app.apibuilder.io/apicollective/apibuilder-spec/latest/play_2_9_scala_3_client */ package io.apibuilder.spec.v0.models { @@ -1390,12 +1390,12 @@ package io.apibuilder.spec.v0.models { } implicit def jsonReadsApibuilderSpecResponseCode[T <: io.apibuilder.spec.v0.models.ResponseCode]: play.api.libs.json.Reads[T] = (json: play.api.libs.json.JsValue) => { - val default = jsonReadsApibuilderSpecResponseCodeInt.reads(json).map(_.asInstanceOf[T]) - val all: Seq[play.api.libs.json.JsResult[T]] = Seq( - default, + Seq( + jsonReadsApibuilderSpecResponseCodeInt.reads(json).map(_.asInstanceOf[T]), jsonReadsApibuilderSpecResponseCodeOption.reads(json).map(_.asInstanceOf[T]) - ) - all.view.find(_.isSuccess).getOrElse(default) + ).view.find(_.isSuccess).getOrElse { + play.api.libs.json.JsSuccess(ResponseCodeUndefinedType(json.toString).asInstanceOf[T]) + } } @@ -1565,7 +1565,7 @@ package io.apibuilder.spec.v0 { val Namespace = "io.apibuilder.spec.v0" val UserAgent = "apibuilder app.apibuilder.io/apicollective/apibuilder-spec/latest/play_2_9_scala_3_client" - val Version = "0.16.50" + val Version = "0.16.53" val VersionMajor = 0 }