Skip to content

Commit 0c83087

Browse files
authored
Merge pull request #95 from alanwguo/master
Allow null as value to represent no values for optionals in spray-json formatter
2 parents 1857ff3 + 3d34df6 commit 0c83087

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/main/scala/org/zalando/jsonapi/json/sprayjson/SprayJsonReadSupport.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ private[json] object SprayJsonReadSupport {
1010
implicit class RichJsObject(val obj: JsObject) extends AnyVal {
1111
def fieldOpt(fieldName: String): Option[JsValue] =
1212
obj.getFields(fieldName).toList match {
13+
case Seq(JsNull) None
1314
case Seq(value) Some(value)
1415
case _ None
1516
}

src/test/scala/org/zalando/jsonapi/json/JsonBaseSpec.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ trait JsonBaseSpec[JsonBaseType] extends WordSpec {
4444

4545
protected lazy val resourceObjectWithEmptyRelationshipsJson = parseJson(resourceObjectWithEmptyRelationshipsJsonString)
4646

47+
protected lazy val resourceObjectWithNullRelationshipJson = parseJson(resourceObjectWithNullRelationshipJsonString)
48+
49+
4750
protected lazy val attributesJsonString =
4851
"""
4952
|{
@@ -632,4 +635,33 @@ trait JsonBaseSpec[JsonBaseType] extends WordSpec {
632635
)
633636
))
634637
))
638+
639+
protected lazy val resourceObjectWithNullRelationshipJsonString =
640+
"""
641+
|{
642+
| "data": [{
643+
| "type": "person",
644+
| "id": "1234",
645+
| "attributes": {
646+
| "name": "foobar"
647+
| },
648+
| "relationships": {
649+
| "father": {
650+
| "data": null
651+
| }
652+
| }
653+
| }]
654+
|}
655+
""".stripMargin
656+
657+
protected lazy val resourceObjectWithNullRelationshipObject = RootObject(Some(
658+
ResourceObjects(List(
659+
ResourceObject(
660+
`type` = "person",
661+
id = Some("1234"),
662+
attributes = Some(List(Attribute("name", StringValue("foobar")))),
663+
relationships = Some(Map("father" -> Relationship()))
664+
)
665+
))
666+
))
635667
}

src/test/scala/org/zalando/jsonapi/json/sprayjson/SprayJsonJsonapiFormatSpec.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ class SprayJsonJsonapiFormatSpec extends JsonBaseSpec[JsValue] with MustMatchers
109109
"transform empty relationship object correctly" in {
110110
resourceObjectWithEmptyRelationshipsJson.convertTo[RootObject] === resourceObjectWithEmptyRelationshipsObject
111111
}
112+
"transform null relationship object correctly" in {
113+
resourceObjectWithNullRelationshipJson.convertTo[RootObject] === resourceObjectWithNullRelationshipObject
114+
}
112115
"fail if data is not an array nor an object" in {
113116
val json = s"""{"data": "foo"}""".parseJson
114117
intercept[DeserializationException] {

0 commit comments

Comments
 (0)