From 393c29654b8d3d53071f0d2932a16ff81688ece6 Mon Sep 17 00:00:00 2001 From: Bassam Date: Mon, 6 May 2024 05:26:27 -0400 Subject: [PATCH] fix: Fix OneOf OpenAPI Serialization Issue (#1010) Signed-off-by: Bassam Riman --- .../client/kotlin/.openapi-generator-ignore | 4 ++ .../client/models/DateTimeParameter.kt | 41 ++++++++++++++++++ .../identus/client/models/DidParameter.kt | 40 +++++++++++++++++ .../client/models/VcVerificationParameter.kt | 41 ++++++++++++++++++ .../controller/http/VcVerification.scala | 9 ++-- .../http/VcVerificationParameter.scala | 32 ++++++++++---- .../VcVerificationControllerImplSpec.scala | 43 +++++++++---------- 7 files changed, 175 insertions(+), 35 deletions(-) create mode 100644 cloud-agent/client/kotlin/src/main/kotlin/org/hyperledger/identus/client/models/DateTimeParameter.kt create mode 100644 cloud-agent/client/kotlin/src/main/kotlin/org/hyperledger/identus/client/models/DidParameter.kt create mode 100644 cloud-agent/client/kotlin/src/main/kotlin/org/hyperledger/identus/client/models/VcVerificationParameter.kt diff --git a/cloud-agent/client/kotlin/.openapi-generator-ignore b/cloud-agent/client/kotlin/.openapi-generator-ignore index 6d1ca9e7b9..2f78a69926 100644 --- a/cloud-agent/client/kotlin/.openapi-generator-ignore +++ b/cloud-agent/client/kotlin/.openapi-generator-ignore @@ -11,3 +11,7 @@ src/main/kotlin/org/hyperledger/identus/client/models/ServiceType.kt src/main/kotlin/org/hyperledger/identus/client/models/StatusPurpose.kt src/main/kotlin/org/hyperledger/identus/client/models/CredentialSubject.kt + +src/main/kotlin/org/hyperledger/identus/client/models/DateTimeParameter.kt +src/main/kotlin/org/hyperledger/identus/client/models/DidParameter.kt +src/main/kotlin/org/hyperledger/identus/client/models/VcVerificationParameter.kt diff --git a/cloud-agent/client/kotlin/src/main/kotlin/org/hyperledger/identus/client/models/DateTimeParameter.kt b/cloud-agent/client/kotlin/src/main/kotlin/org/hyperledger/identus/client/models/DateTimeParameter.kt new file mode 100644 index 0000000000..be24c039e2 --- /dev/null +++ b/cloud-agent/client/kotlin/src/main/kotlin/org/hyperledger/identus/client/models/DateTimeParameter.kt @@ -0,0 +1,41 @@ +/** + * + * Please note: + * This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * Do not edit this file manually. + * + */ + +@file:Suppress( + "ArrayInDataClass", + "EnumEntryName", + "RemoveRedundantQualifierName", + "UnusedImport" +) + +package org.hyperledger.identus.client.models + + +import com.google.gson.annotations.SerializedName + +/** + * + * + * @param dateTime + * @param parameterType + */ + + +data class DateTimeParameter ( + + @SerializedName("did") + override val did: kotlin.String? = null, + + @SerializedName("parameterType") + override val parameterType: kotlin.String = "DateTimeParameter", + + @SerializedName("dateTime") + override val dateTime: java.time.OffsetDateTime, + +) : VcVerificationParameter + diff --git a/cloud-agent/client/kotlin/src/main/kotlin/org/hyperledger/identus/client/models/DidParameter.kt b/cloud-agent/client/kotlin/src/main/kotlin/org/hyperledger/identus/client/models/DidParameter.kt new file mode 100644 index 0000000000..bec73ad0d1 --- /dev/null +++ b/cloud-agent/client/kotlin/src/main/kotlin/org/hyperledger/identus/client/models/DidParameter.kt @@ -0,0 +1,40 @@ +/** + * + * Please note: + * This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * Do not edit this file manually. + * + */ + +@file:Suppress( + "ArrayInDataClass", + "EnumEntryName", + "RemoveRedundantQualifierName", + "UnusedImport" +) + +package org.hyperledger.identus.client.models + + +import com.google.gson.annotations.SerializedName + +/** + * + * + * @param did + * @param parameterType + */ + + +data class DidParameter ( + + @SerializedName("did") + override val did: kotlin.String, + + @SerializedName("parameterType") + override val parameterType: kotlin.String = "DidParameter", + + @get:SerializedName("dateTime") + override val dateTime: java.time.OffsetDateTime? = null, +) : VcVerificationParameter + diff --git a/cloud-agent/client/kotlin/src/main/kotlin/org/hyperledger/identus/client/models/VcVerificationParameter.kt b/cloud-agent/client/kotlin/src/main/kotlin/org/hyperledger/identus/client/models/VcVerificationParameter.kt new file mode 100644 index 0000000000..5ff79ee8e3 --- /dev/null +++ b/cloud-agent/client/kotlin/src/main/kotlin/org/hyperledger/identus/client/models/VcVerificationParameter.kt @@ -0,0 +1,41 @@ +/** + * + * Please note: + * This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * Do not edit this file manually. + * + */ + +@file:Suppress( + "ArrayInDataClass", + "EnumEntryName", + "RemoveRedundantQualifierName", + "UnusedImport" +) + +package org.hyperledger.identus.client.models + +import org.hyperledger.identus.client.models.DateTimeParameter +import org.hyperledger.identus.client.models.DidParameter + +import com.google.gson.annotations.SerializedName + +/** + * + * + * @param dateTime + * @param parameterType + * @param did + */ + + +interface VcVerificationParameter { + + @get:SerializedName("dateTime") + val dateTime: java.time.OffsetDateTime? + @get:SerializedName("parameterType") + val parameterType: kotlin.String + @get:SerializedName("did") + val did: kotlin.String? +} + diff --git a/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/verification/controller/http/VcVerification.scala b/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/verification/controller/http/VcVerification.scala index 159d86cf62..13e90fd4e1 100644 --- a/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/verification/controller/http/VcVerification.scala +++ b/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/verification/controller/http/VcVerification.scala @@ -4,7 +4,7 @@ import org.hyperledger.identus.api.http.ErrorResponse import org.hyperledger.identus.pollux.core.service import org.hyperledger.identus.pollux.core.service.verification.VcVerification as ServiceVcVerification import sttp.tapir.Schema -import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder} +import zio.json.{JsonDecoder, JsonEncoder} import zio.{IO, *} enum VcVerification { @@ -23,11 +23,12 @@ enum VcVerification { } object VcVerification { - given encoder: JsonEncoder[VcVerification] = - DeriveJsonEncoder.gen[VcVerification] + given encoder: JsonEncoder[VcVerification] = JsonEncoder[String].contramap(_.toString) given decoder: JsonDecoder[VcVerification] = - DeriveJsonDecoder.gen[VcVerification] + JsonDecoder[String].mapOrFail(s => + VcVerification.values.find(_.toString == s).toRight(s"Unknown VcVerification: $s") + ) given schema: Schema[VcVerification] = Schema.derivedEnumeration.defaultStringBased diff --git a/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/verification/controller/http/VcVerificationParameter.scala b/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/verification/controller/http/VcVerificationParameter.scala index 4821fa3ba8..0ceba92f3f 100644 --- a/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/verification/controller/http/VcVerificationParameter.scala +++ b/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/verification/controller/http/VcVerificationParameter.scala @@ -5,19 +5,33 @@ import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder} import java.time.OffsetDateTime -sealed trait VcVerificationParameter +sealed trait VcVerificationParameter(val parameterType: String) object VcVerificationParameter { - given encoder: JsonEncoder[VcVerificationParameter] = - DeriveJsonEncoder.gen[VcVerificationParameter] + given encoder: JsonEncoder[VcVerificationParameter] = DidParameter.encoder + .orElseEither(DateTimeParameter.encoder) + .contramap[VcVerificationParameter] { + case did: DidParameter => Left(did) + case dateTime: DateTimeParameter => Right(dateTime) + } + + given decoder: JsonDecoder[VcVerificationParameter] = DidParameter.decoder + .orElseEither(DateTimeParameter.decoder) + .map[VcVerificationParameter] { + case Left(did) => did + case Right(dateTime) => dateTime + } + + given schema: Schema[VcVerificationParameter] = + Schema + .oneOfUsingField[VcVerificationParameter, String](a => a.parameterType, t => t)( + ("DidParameter", DidParameter.schema), + ("DateTimeParameter", DateTimeParameter.schema) + ) - given decoder: JsonDecoder[VcVerificationParameter] = - DeriveJsonDecoder.gen[VcVerificationParameter] - - given schema: Schema[VcVerificationParameter] = Schema.derived } -case class DidParameter(aud: String) extends VcVerificationParameter +case class DidParameter(did: String) extends VcVerificationParameter("DidParameter") object DidParameter { given encoder: JsonEncoder[DidParameter] = @@ -29,7 +43,7 @@ object DidParameter { given schema: Schema[DidParameter] = Schema.derived } -case class DateTimeParameter(dateTime: OffsetDateTime) extends VcVerificationParameter +case class DateTimeParameter(dateTime: OffsetDateTime) extends VcVerificationParameter("DateTimeParameter") object DateTimeParameter { given encoder: JsonEncoder[DateTimeParameter] = diff --git a/cloud-agent/service/server/src/test/scala/org/hyperledger/identus/verification/controller/VcVerificationControllerImplSpec.scala b/cloud-agent/service/server/src/test/scala/org/hyperledger/identus/verification/controller/VcVerificationControllerImplSpec.scala index 1bbb9282ab..86ac30be9b 100644 --- a/cloud-agent/service/server/src/test/scala/org/hyperledger/identus/verification/controller/VcVerificationControllerImplSpec.scala +++ b/cloud-agent/service/server/src/test/scala/org/hyperledger/identus/verification/controller/VcVerificationControllerImplSpec.scala @@ -72,31 +72,30 @@ object VcVerificationControllerImplSpec extends ZIOSpecDefault with VcVerificati signedJwtCredential = issuer.signer.encode(jwtCredentialPayload.asJson) authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]] backend = httpBackend(vcVerificationController, authenticator) + request = List( + VcVerificationRequest( + signedJwtCredential.value, + List( + ParameterizableVcVerification(VcVerification.SignatureVerification, None), + ParameterizableVcVerification(VcVerification.NotBeforeCheck, Some(DateTimeParameter(currentTime))), + ParameterizableVcVerification(VcVerification.ExpirationCheck, Some(DateTimeParameter(currentTime))) + ) + ), + VcVerificationRequest( + signedJwtCredential.value, + List( + ParameterizableVcVerification(VcVerification.AudienceCheck, Some(DidParameter(verifier.value))), + ParameterizableVcVerification( + VcVerification.IssuerIdentification, + Some(DidParameter(issuer.did.value)) + ) + ) + ) + ).toJsonPretty response: Response[Either[DeserializationException[String], List[VcVerificationResponse]]] <- basicRequest .post(uri"${vcVerificationUriBase}") - .body( - List( - VcVerificationRequest( - signedJwtCredential.value, - List( - ParameterizableVcVerification(VcVerification.SignatureVerification, None), - ParameterizableVcVerification(VcVerification.NotBeforeCheck, Some(DateTimeParameter(currentTime))), - ParameterizableVcVerification(VcVerification.ExpirationCheck, Some(DateTimeParameter(currentTime))) - ) - ), - VcVerificationRequest( - signedJwtCredential.value, - List( - ParameterizableVcVerification(VcVerification.AudienceCheck, Some(DidParameter(verifier.value))), - ParameterizableVcVerification( - VcVerification.IssuerIdentification, - Some(DidParameter(issuer.did.value)) - ) - ) - ) - ).toJsonPretty - ) + .body(request) .response(asJsonAlways[List[VcVerificationResponse]]) .send(backend) statusCodeIs200 = assert(response.code)(equalTo(StatusCode.Ok))