eu-digital-identity-wallet / eudi-lib-jvm-presentation-exchange-kt

Implementation of DIF Presentation Exchange v2 specification in Kotlin (jvm)
Apache License 2.0
5 stars 3 forks source link

Make Types java.io.Serializable #138

Closed ydanneg closed 7 months ago

ydanneg commented 7 months ago

fixes https://github.com/eu-digital-identity-wallet/eudi-lib-jvm-presentation-exchange-kt/issues/137

ydanneg commented 7 months ago

There is a problem: https://github.com/eu-digital-identity-wallet/eudi-lib-jvm-presentation-exchange-kt/issues/137#issuecomment-2015018285

babisRoutis commented 7 months ago

There is a problem: #137 (comment)

Indeed.

We need to be able to reproduce this. Took the unit-test that you provided in your gist. Can I ask you to include a similar unit-test to this PR?

Something like:

class SerializationTest {

    @Test
    fun `Can serialize and deserialize a PresentationDefinition`() {
        val supportedClaimFormats =
            listOf(SupportedClaimFormat.jwt(ClaimFormat.JwtType.SD_JWT, setOf(JwtAlgorithm.DigSig.ES256))!!)
        val presentationDefinition = PresentationDefinition(
            id = Id("pdId"),
            inputDescriptors = emptyList(),
            name = Name("name"),
            purpose = Purpose("purpose"),
            format = Format(supportedClaimFormats),
            submissionRequirements = listOf(),

        )

        testSerialization(presentationDefinition)
    }

    @Test
    fun `format test`() {
        testSerialization("v2.0.0/presentation-definition/format_example.json")
    }

    @Test
    fun `basic example`() {
        testSerialization("v2.0.0/presentation-definition/basic_example.json")
    }

    @Test
    fun `single group example`() {
        testSerialization("v2.0.0/presentation-definition/single_group_example.json").also {
            it.submissionRequirements?.forEach { x -> println(x) }
        }
    }

    @Test
    fun `multi group example`() {
        testSerialization("v2.0.0/presentation-definition/multi_group_example.json").also {
            it.submissionRequirements?.forEach { x -> println(x) }
        }
    }

    @Test
    fun `mDL example`() {
        testSerialization("v2.0.0/presentation-definition/mDL-example.json").also {
            it.submissionRequirements?.forEach { x -> println(x) }
        }
    }

    private fun testSerialization(f: String): PresentationDefinition =
        assertDoesNotThrow {
            val pd = PresentationExchange.jsonParser.decodePresentationDefinition(load(f)!!).getOrThrow()
            testSerialization(pd)
            pd
        }

    private fun load(f: String): InputStream? =
        PresentationDefinitionTest::class.java.classLoader.getResourceAsStream(f)
}

private inline fun <reified T> testSerialization(pd: T) {
    val deserialized = deserialize<T>(serialize(pd))
    assertEquals(pd, deserialized)
}

private fun serialize(obj: Any?): ByteArray =
    ByteArrayOutputStream().use {
        ObjectOutputStream(it).writeObject(obj)
        it.toByteArray()
    }
private inline fun <reified T> deserialize(bytes: ByteArray): T =
    ObjectInputStream(ByteArrayInputStream(bytes)).use { out ->
        out.readObject() as T
    }
babisRoutis commented 7 months ago

@ydanneg thanks.

Now it's reproducible. Need to sort out the Filter = JsonObject problem, I guess :thinking:

ydanneg commented 7 months ago

@babisRoutis Hackish but works :) https://github.com/ydanneg/eudi-lib-jvm-presentation-exchange-kt/compare/feat/serializable...ydanneg:eudi-lib-jvm-presentation-exchange-kt:feat/serializable-hack?expand=1

babisRoutis commented 7 months ago

@ydanneg

I think we have found an alternative solution on this. In a nutshell involves changing the Filter from being an alias of JsonObject to be a value class which contains a json as string and a custom JsonSerializer.

I am not sure how we should proceed this PR. I see the following options:

  1. Stop this PR and let us redefine the Fitler add the test and the java.io.Serializable
  2. We can apply to main the redefinition of Filter and then you can rebase your branch from main.

What do you think?

ydanneg commented 7 months ago

I can rebase later, sure.

babisRoutis commented 7 months ago

@ydanneg I see that I can write to your branch. Is it ok with you to apply the changes on your branch ?

(On Monday :smile: )

ydanneg commented 7 months ago

@ydanneg I see that I can write to your branch. Is it ok with you to apply the changes on your branch ?

(On Monday :smile: )

sure. go ahead! :)

babisRoutis commented 7 months ago

@vafeini Can you please review this PR? If you agree we can proceed and perhaps release a fix version.