Skip to content

Commit c233626

Browse files
committed
Update ScalaObjectMapper to treat Option as ReferenceType.
1 parent 5bd3378 commit c233626

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/main/scala/com/fasterxml/jackson/module/scala/experimental/ScalaObjectMapper.scala

+12-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ trait ScalaObjectMapper {
7272
throw new IllegalArgumentException("Need exactly 1 type parameter for collection like types ("+clazz.getName+")")
7373
}
7474
getTypeFactory.constructCollectionLikeType(clazz, typeArguments(0))
75+
} else if (isReference(clazz)) {
76+
val typeArguments = m.typeArguments.map(constructType(_)).toArray
77+
if(typeArguments.length != 1) {
78+
throw new IllegalArgumentException("Need exactly 1 type parameter for reference types ("+clazz.getName+")")
79+
}
80+
getTypeFactory.constructReferenceType(clazz, typeArguments(0))
7581
} else {
7682
val typeArguments = m.typeArguments.map(constructType(_)).toArray
7783
getTypeFactory.constructParametrizedType(clazz, clazz, typeArguments: _*)
@@ -333,10 +339,14 @@ trait ScalaObjectMapper {
333339
MAP.isAssignableFrom(c)
334340
}
335341

336-
private val ITERABLE = classOf[collection.Iterable[_]]
337342
private val OPTION = classOf[Option[_]]
343+
private def isReference(c: Class[_]): Boolean = {
344+
OPTION.isAssignableFrom(c)
345+
}
346+
347+
private val ITERABLE = classOf[collection.Iterable[_]]
338348
private def isCollectionLike(c: Class[_]): Boolean = {
339-
ITERABLE.isAssignableFrom(c) || OPTION.isAssignableFrom(c)
349+
ITERABLE.isAssignableFrom(c)
340350
}
341351

342352
}

src/test/scala/com/fasterxml/jackson/module/scala/experimental/ScalaObjectMapperTest.scala

+9-2
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,15 @@ class ScalaObjectMapperTest extends FlatSpec with Matchers {
196196
assert(result.isInstanceOf[collection.Map[_, _]])
197197
}
198198

199-
it should "read a option values from a JSON array" in {
200-
val result = mapper.readValue[List[Option[String]]](toplevelOptionArrayJson)
199+
it should "read option values into List from a JSON array" in {
200+
val result = mapper.readValue[java.util.ArrayList[Option[String]]](toplevelOptionArrayJson)
201+
import scala.collection.JavaConversions._
202+
result(0) should equal(Some("some"))
203+
result(1) should equal(None)
204+
}
205+
206+
it should "read option values into Array from a JSON array" in {
207+
val result = mapper.readValue[Array[Option[String]]](toplevelOptionArrayJson)
201208
result(0) should equal(Some("some"))
202209
result(1) should equal(None)
203210
}

0 commit comments

Comments
 (0)