Skip to content

Commit

Permalink
Merge pull request #69 from contentful/iss67
Browse files Browse the repository at this point in the history
Fix NPE when deserializing an entry with no fields
  • Loading branch information
tomxor committed May 19, 2015
2 parents af3748f + 2523031 commit 1f92140
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/main/java/com/contentful/java/cda/ResourceTypeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -253,14 +254,25 @@ private void setBaseFields(CDAResource target, JsonObject sys, JsonElement jsonE
// Fields
JsonElement fields = jsonElement.getAsJsonObject().get("fields");
if (target instanceof ResourceWithMap) {
Map fieldsMap;
if (fields == null) {
fieldsMap = Collections.emptyMap();
} else {
fieldsMap = context.<Map<String, Object>>deserialize(fields.getAsJsonObject(), Map.class);
}

ResourceWithMap res = (ResourceWithMap) target;
target.setLocale(space.getDefaultLocale());
res.setRawFields(
context.<Map<String, Object>>deserialize(fields.getAsJsonObject(), Map.class));
res.getLocalizedFieldsMap().put(space.getDefaultLocale(), res.getRawFields());
res.setRawFields(fieldsMap);
res.getLocalizedFieldsMap().put(space.getDefaultLocale(), fieldsMap);
} else if (target instanceof ResourceWithList) {
ResourceWithList<Object> res = (ResourceWithList<Object>) target;
res.setFields(context.<List<Object>>deserialize(fields.getAsJsonArray(), List.class));
List fieldsList;
if (fields == null) {
fieldsList = Collections.emptyList();
} else {
fieldsList = context.<List<Object>>deserialize(fields.getAsJsonArray(), List.class);
}
((ResourceWithList<Object>) target).setFields(fieldsList);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/test/kotlin/com/contentful/java/cda/SerializationTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import kotlin.test.assertTrue
* Serialization Tests.
*/
class SerializationTests : BaseTest() {
test fun testNoFields() {
enqueue("entry_no_fields.json")
client!!.entries().fetchOne("id")
}

test fun testSerialization() {
enqueue("entry_fetch_all_response.json")
val original = client!!.entries().fetchAll()
Expand Down
24 changes: 24 additions & 0 deletions src/test/resources/entry_no_fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "sid"
}
},
"type": "Entry",
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "cid"
}
},
"fields": {},
"id": "id",
"revision": 1,
"createdAt": "2015-05-19T19:32:05.078Z",
"updatedAt": "2015-05-19T19:32:05.078Z"
}
}

0 comments on commit 1f92140

Please sign in to comment.