Skip to content

Commit

Permalink
ATL-7076: Fix OneOf OpenAPI Serialization Issue
Browse files Browse the repository at this point in the history
Signed-off-by: Bassam Riman <[email protected]>
  • Loading branch information
CryptoKnightIOG committed May 4, 2024
1 parent cf156d9 commit 3e71e9d
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 35 deletions.
4 changes: 4 additions & 0 deletions cloud-agent/client/kotlin/.openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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?
}

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand All @@ -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] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 3e71e9d

Please sign in to comment.