Skip to content

Commit

Permalink
fix NonEmptyList deserialization failing on wildcard types (#80)
Browse files Browse the repository at this point in the history
* fix NonEmptyList deserialization failing on wildcard types

* fix lint

* fix lint
  • Loading branch information
myuwono authored Apr 5, 2022
1 parent ba2a4ec commit fe4a3d7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ public object NonEmptyListDeserializerResolver : Deserializers.Base() {
elementDeserializer: JsonDeserializer<*>?
): JsonDeserializer<NonEmptyList<*>>? =
if (NonEmptyList::class.java.isAssignableFrom(type.rawClass)) {
StdDelegatingDeserializer<NonEmptyList<*>>(
NonEmptyListDeserializationConverter(type.bindings.getBoundType(0))
)
val boundType = type.bindings.getBoundType(0) ?: config.constructType(Object::class.java)
StdDelegatingDeserializer<NonEmptyList<*>>(NonEmptyListDeserializationConverter(boundType))
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import arrow.core.test.generators.option
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
Expand Down Expand Up @@ -87,6 +88,15 @@ class EitherModuleTest : FunSpec() {
testClass.shouldRoundTrip(mapper)
}
}

test("should round-trip on wildcard types") {
val mapper = ObjectMapper().registerArrowModule()
checkAll(Arb.either(Arb.int(1..10), Arb.string(5))) { original: Either<*, *> ->
val serialized = mapper.writeValueAsString(original)
val deserialized = shouldNotThrowAny { mapper.readValue(serialized, Either::class.java) }
deserialized shouldBe original
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import arrow.core.test.generators.option
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
Expand Down Expand Up @@ -86,6 +87,17 @@ class IorModuleTest : FunSpec() {
testClass.shouldRoundTrip(mapper)
}
}

test("should round-trip with wildcard types") {
checkAll(Arb.ior(Arb.int(1..10), Arb.string(10, Codepoint.az()))) { original: Ior<*, *> ->
val mapper = ObjectMapper().registerKotlinModule().registerArrowModule()
val serialized = mapper.writeValueAsString(original)
val deserialized: Ior<*, *> = shouldNotThrowAny {
mapper.readValue(serialized, Ior::class.java)
}
deserialized shouldBe original
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package arrow.integrations.jackson.module

import arrow.core.Nel
import arrow.core.NonEmptyList
import arrow.core.test.UnitSpec
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.arbitrary.arbitrary
Expand Down Expand Up @@ -55,5 +57,18 @@ class NonEmptyListModuleTest : UnitSpec() {
decoded shouldBe wrapper
}
}

"should round trip on NonEmptyList with wildcard type" {
val mapperWithSettings = ObjectMapper().registerKotlinModule().registerArrowModule()

checkAll(Arb.nonEmptyList(Arb.string())) { original: NonEmptyList<*> ->
val deserialized: NonEmptyList<*> = shouldNotThrowAny {
val serialized: String = mapperWithSettings.writeValueAsString(original)
mapperWithSettings.readValue(serialized, NonEmptyList::class.java)
}

deserialized shouldBe original
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.arbitrary.arbitrary
Expand Down Expand Up @@ -64,5 +65,14 @@ class OptionModuleTest : UnitSpec() {
decoded shouldBe option
}
}

"should round-trip on wildcard types" {
val mapper = ObjectMapper().registerArrowModule()
checkAll(Arb.option(Arb.int(1..10))) { original: Option<*> ->
val serialized = mapper.writeValueAsString(original)
val deserialized = shouldNotThrowAny { mapper.readValue(serialized, Option::class.java) }
deserialized shouldBe original
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import arrow.core.valid
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
Expand Down Expand Up @@ -80,6 +81,18 @@ class ValidatedModuleTest : FunSpec() {
testClass.shouldRoundTrip(mapper)
}
}

test("should round-trip with wildcard types") {
checkAll(Arb.validated(Arb.int(1..10), Arb.string(10, Codepoint.az()))) {
original: Validated<*, *> ->
val mapper = ObjectMapper().registerKotlinModule().registerArrowModule()
val serialized = mapper.writeValueAsString(original)
val deserialized: Validated<*, *> = shouldNotThrowAny {
mapper.readValue(serialized, Validated::class.java)
}
deserialized shouldBe original
}
}
}
}

Expand Down

0 comments on commit fe4a3d7

Please sign in to comment.