Skip to content

Commit 5e543d6

Browse files
authored
partial re-enable of MergeTest (#818)
1 parent 88d6ec0 commit 5e543d6

2 files changed

Lines changed: 67 additions & 27 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package tools.jackson.module.scala.deser
2+
3+
import com.fasterxml.jackson.annotation.JsonMerge
4+
import tools.jackson.core.`type`.TypeReference
5+
import tools.jackson.databind.{MapperFeature, ObjectMapper, ObjectReader}
6+
import tools.jackson.module.scala.DefaultScalaModule
7+
8+
import scala.collection.{Map, mutable}
9+
10+
class MergeScala2Test extends DeserializerTest {
11+
12+
val module: DefaultScalaModule.type = DefaultScalaModule
13+
14+
// This test relies on enabling MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS
15+
// which is not enabled by default in the Jackson v2 but not in Jackson v3
16+
def newScalaMapper: ObjectMapper = newBuilder
17+
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
18+
.build()
19+
20+
21+
val firstPairMapJson = """{"one":{"first":"1"},"two":{"second":"2"},"three":{"first":"3","second":"4"}}"""
22+
val secondPairMapJson = """{"two":{"first":"22"},"three":{"second":"33"}}"""
23+
val secondPairMap = Map("two" -> Pair("22", null), "three" -> Pair(null, "33"))
24+
val mergedPairMap = Map("one" -> Pair("1", null), "two" -> Pair("22", "2"), "three" -> Pair("3", "33"))
25+
26+
behavior of "The DefaultScalaModule when reading for updating"
27+
28+
// started failing in jackson-module-scala v3.2.0 but only for Scala 3
29+
// see https://github.com/FasterXML/jackson-module-scala/issues/817
30+
it should "merge only the annotated pair map" in {
31+
val typeReference = new TypeReference[ClassWithMaps[Pair]]{}
32+
val initial = deserialize(classJson(firstPairMapJson), typeReference)
33+
val result = updateValue(newScalaMapper, initial, typeReference, classJson(secondPairMapJson))
34+
35+
result shouldBe ClassWithMaps(secondPairMap, mergedPairMap)
36+
}
37+
38+
def classJson(nestedJson: String) = s"""{"field1":$nestedJson,"field2":$nestedJson}"""
39+
40+
private def updateValue[T](mapper: ObjectMapper, valueToUpdate: T,
41+
typeReference: TypeReference[T], src: String): T = {
42+
objectReaderFor(mapper, valueToUpdate, typeReference).readValue(src)
43+
}
44+
45+
private def objectReaderFor[T](mapper: ObjectMapper, valueToUpdate: T,
46+
typeReference: TypeReference[T]): ObjectReader = {
47+
mapper.readerForUpdating(valueToUpdate).forType(typeReference)
48+
}
49+
}

src/test/scala/tools/jackson/module/scala/deser/MergeTest.scala

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class MergeTest extends DeserializerTest {
1717

1818
val module: DefaultScalaModule.type = DefaultScalaModule
1919

20-
// This test replies on enabling MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS
20+
// This test relies on enabling MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS
2121
// which is not enabled by default in the Jackson v2 but not in Jackson v3
2222
def newScalaMapper: ObjectMapper = newBuilder
2323
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
@@ -28,6 +28,21 @@ class MergeTest extends DeserializerTest {
2828
.defaultMergeable(true)
2929
.build()
3030

31+
val firstListJson = """["one","two"]"""
32+
val secondListJson = """["three"]"""
33+
val secondList = List("three")
34+
val mergedList = List("one", "two", "three")
35+
36+
val firstStringMapJson = """{"one":"1","two":"2"}"""
37+
val secondStringMapJson = """{"two":"22","three":"33"}"""
38+
val secondStringMap = Map("two" -> "22", "three" -> "33")
39+
val mergedStringMap = Map("one" -> "1", "two" -> "22", "three" -> "33")
40+
41+
val firstPairMapJson = """{"one":{"first":"1"},"two":{"second":"2"},"three":{"first":"3","second":"4"}}"""
42+
val secondPairMapJson = """{"two":{"first":"22"},"three":{"second":"33"}}"""
43+
val secondPairMap = Map("two" -> Pair("22", null), "three" -> Pair(null, "33"))
44+
val mergedPairMap = Map("one" -> Pair("1", null), "two" -> Pair("22", "2"), "three" -> Pair("3", "33"))
45+
3146
behavior of "The DefaultScalaModule when reading for updating"
3247

3348
it should "merge both lists" in {
@@ -69,16 +84,7 @@ class MergeTest extends DeserializerTest {
6984

7085
result shouldBe ClassWithMaps(mergedPairMap, mergedPairMap)
7186
}
72-
73-
// https://github.com/FasterXML/jackson-module-scala/issues/817
74-
it should "merge only the annotated pair map" ignore {
75-
val typeReference = new TypeReference[ClassWithMaps[Pair]]{}
76-
val initial = deserialize(classJson(firstPairMapJson), typeReference)
77-
val result = updateValue(newScalaMapper, initial, typeReference, classJson(secondPairMapJson))
78-
79-
result shouldBe ClassWithMaps(secondPairMap, mergedPairMap)
80-
}
81-
87+
8288
it should "merge both mutable maps" in {
8389
val typeReference = new TypeReference[ClassWithMutableMaps[String]]{}
8490
val initial = deserialize(classJson(firstStringMapJson), typeReference)
@@ -95,22 +101,7 @@ class MergeTest extends DeserializerTest {
95101
result shouldBe ClassWithMutableMaps(mutable.Map() ++ secondStringMap, mutable.Map() ++ mergedStringMap)
96102
}
97103

98-
def classJson(nestedJson: String) = s"""{"field1":$nestedJson,"field2":$nestedJson}"""
99-
100-
val firstListJson = """["one","two"]"""
101-
val secondListJson = """["three"]"""
102-
val secondList = List("three")
103-
val mergedList = List("one", "two", "three")
104-
105-
val firstStringMapJson = """{"one":"1","two":"2"}"""
106-
val secondStringMapJson = """{"two":"22","three":"33"}"""
107-
val secondStringMap = Map("two" -> "22", "three" -> "33")
108-
val mergedStringMap = Map("one" -> "1", "two" -> "22", "three" -> "33")
109-
110-
val firstPairMapJson = """{"one":{"first":"1"},"two":{"second":"2"},"three":{"first":"3","second":"4"}}"""
111-
val secondPairMapJson = """{"two":{"first":"22"},"three":{"second":"33"}}"""
112-
val secondPairMap = Map("two" -> Pair("22", null), "three" -> Pair(null, "33"))
113-
val mergedPairMap = Map("one" -> Pair("1", null), "two" -> Pair("22", "2"), "three" -> Pair("3", "33"))
104+
private def classJson(nestedJson: String) = s"""{"field1":$nestedJson,"field2":$nestedJson}"""
114105

115106
private def updateValue[T](mapper: ObjectMapper, valueToUpdate: T,
116107
typeReference: TypeReference[T], src: String): T = {

0 commit comments

Comments
 (0)