arrow-kt / arrow-integrations

Λrrow Integrations is part of Λrrow, a functional companion to Kotlin's Standard Library
http://arrow-kt.io
Other
27 stars 6 forks source link

[BUG] Wrong Option deserialization #105

Closed herulume closed 1 year ago

herulume commented 1 year ago

Describe the bug

Option<T> de-serializes to it's internal representation (when T is present and when it's not) .

To Reproduce

Run this adapted example

import arrow.core.Option
import arrow.core.none
import arrow.core.some
import arrow.integrations.jackson.module.registerArrowModule
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class ExampleTest :
    FunSpec(
        {

            data class Organization(
                val name: String,
                val address: Option<String>,
                val email: Option<String>
            )

            val mapper = ObjectMapper().registerKotlinModule()
                .registerArrowModule()
                .setSerializationInclusion(
                    JsonInclude.Include.NON_ABSENT
                ) // will not serialize None as nulls

            val prettyPrinter = mapper.writerWithDefaultPrettyPrinter()

            test("example #1: data structure serialization") {
                val arrowKt = Organization("arrow-kt", none(), "foo@barr".some())

                val jsonString = prettyPrinter.writeValueAsString(arrowKt)

                jsonString shouldBe """
      {
        "name" : "arrow-kt",
        "email" : "foo@bar"
      }
    """.trimIndent()

                mapper.readValue(jsonString, Organization::class.java) shouldBe arrowKt
            }
        })

Expected behavior

Screenshots image

Environment (please complete the following information):

Additional context This problem appear with an Option of a value class but I was able to reproduce it with an Option<String> as per the example.

myuwono commented 1 year ago

@herulume thanks for reporting this 🙌 What was the version of arrow and arrow-integrations that were used? I struggle to replicate the problem in latest. Would you mind trying this with 0.13.3-alpha.22? Sorry about the alpha version. Arrow team is currently in the process to release this library. I'll poke the team again just to be sure.

Here is the branch with the identical test, which seemingly has the expected behaviour https://github.com/arrow-kt/arrow-integrations/pull/106/files

myuwono commented 1 year ago

We've just released 0.14.0 which is of the same code as 0.13.3-alpha.22. @herulume can we try against that and see if the problem persists?

herulume commented 1 year ago

Hey! Thank you for such a fast answer. It is indeed solved now 🙏