|
| 1 | +package com.fasterxml.jackson.module.scala.deser |
| 2 | + |
| 3 | +import java.io.StringWriter |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.{DeserializationFeature, MapperFeature, ObjectMapper, SerializationFeature} |
| 6 | +import com.fasterxml.jackson.module.scala.DefaultScalaModule |
| 7 | +import org.junit.runner.RunWith |
| 8 | +import org.scalatest.{FlatSpec, Matchers} |
| 9 | +import org.scalatest.prop.TableDrivenPropertyChecks |
| 10 | +import org.scalatestplus.junit.JUnitRunner |
| 11 | + |
| 12 | +import scala.collection.immutable.{ListMap, Queue, Stack, TreeMap} |
| 13 | + |
| 14 | +// taken from https://github.com/dejanlokar1/serialization_problem/blob/master/src/test/scala/SerializationTest.scala |
| 15 | +// test for https://github.com/FasterXML/jackson-databind/issues/2422 |
| 16 | + |
| 17 | +@RunWith(classOf[JUnitRunner]) |
| 18 | +class ListMapTest extends FlatSpec with Matchers with TableDrivenPropertyChecks { |
| 19 | + private val mapper = { |
| 20 | + val _mapper = new ObjectMapper |
| 21 | + _mapper.registerModule(DefaultScalaModule) |
| 22 | + _mapper.disable(MapperFeature.AUTO_DETECT_IS_GETTERS) |
| 23 | + _mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| 24 | + _mapper.disable(SerializationFeature.INDENT_OUTPUT) |
| 25 | + _mapper |
| 26 | + } |
| 27 | + |
| 28 | + case class SampleCaseClass(map: Map[String, String] = Map(), seq: Seq[String] = List()) |
| 29 | + |
| 30 | + "Map Serialization" should "handle problematic list map" ignore { |
| 31 | + val sampleListMap = ListMap("foo" -> "bar") |
| 32 | + val sampleCaseClass = SampleCaseClass(map = sampleListMap) |
| 33 | + |
| 34 | + // list map can be serialized |
| 35 | + serialize(sampleListMap) shouldBe """{"foo":"bar"}""" |
| 36 | + // list map in a case class can not be serialized |
| 37 | + serialize(sampleCaseClass) shouldBe """{"map":{"foo":"bar"},"seq":[]}""" |
| 38 | + } |
| 39 | + |
| 40 | + "Map Serialization" should "handle working maps" in { |
| 41 | + val maps = Table( |
| 42 | + "Map implementations", |
| 43 | + Map("foo" -> "bar"), |
| 44 | + TreeMap("foo" -> "bar") |
| 45 | + ) |
| 46 | + |
| 47 | + forAll(maps) { map => |
| 48 | + val sampleCaseClass = SampleCaseClass(map = map) |
| 49 | + serialize(sampleCaseClass) shouldBe """{"map":{"foo":"bar"},"seq":[]}""" |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + "Map Serialization" should "handle working sequences" in { |
| 54 | + val sequences = Table( |
| 55 | + "Sequence implementations", |
| 56 | + List("foo"), |
| 57 | + Stack("foo"), |
| 58 | + Stream("foo"), |
| 59 | + Queue("foo"), |
| 60 | + Vector("foo") |
| 61 | + ) |
| 62 | + |
| 63 | + forAll(sequences) { seq => |
| 64 | + val sampleCaseClass = SampleCaseClass(seq = seq) |
| 65 | + serialize(sampleCaseClass) shouldBe """{"map":{},"seq":["foo"]}""" |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private def serialize(value: Any): String = { |
| 70 | + val writer = new StringWriter() |
| 71 | + mapper.writeValue(writer, value) |
| 72 | + writer.toString |
| 73 | + } |
| 74 | +} |
0 commit comments