From 1eb7d6edb691a08c538eb86310e317d1270eeb52 Mon Sep 17 00:00:00 2001 From: Samuel Giles Date: Fri, 18 Nov 2016 15:02:09 +0000 Subject: [PATCH] If the request store is empty, generate an empty Object response (#332) * If the request store is empty, generate an empty Object response * Don't fail if prefs is null * Don't return null if the object doesn't contain any keys. Instead, return an empty WritableMap * Unused import --- .../app/src/main/java/org/mozilla/magnet/api/Utils.java | 5 ++--- .../java/org/mozilla/magnet/magnetapi/ApiPreferences.java | 8 +++++++- lib/app.js | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/android/app/src/main/java/org/mozilla/magnet/api/Utils.java b/android/app/src/main/java/org/mozilla/magnet/api/Utils.java index a89c6210..d2c201f4 100644 --- a/android/app/src/main/java/org/mozilla/magnet/api/Utils.java +++ b/android/app/src/main/java/org/mozilla/magnet/api/Utils.java @@ -83,10 +83,9 @@ public static WritableMap jsonToWritableMap(JSONObject jsonObject) { return null; } - Iterator iterator = jsonObject.keys(); if (!iterator.hasNext()) { - return null; + return writableMap; } while (iterator.hasNext()) { @@ -155,4 +154,4 @@ public static WritableArray jsonArrayToWritableArray(JSONArray jsonArray) { return writableArray; } -} \ No newline at end of file +} diff --git a/android/app/src/main/java/org/mozilla/magnet/magnetapi/ApiPreferences.java b/android/app/src/main/java/org/mozilla/magnet/magnetapi/ApiPreferences.java index ceac3015..0cffcdae 100644 --- a/android/app/src/main/java/org/mozilla/magnet/magnetapi/ApiPreferences.java +++ b/android/app/src/main/java/org/mozilla/magnet/magnetapi/ApiPreferences.java @@ -28,7 +28,13 @@ class ApiPreferences extends Api { */ @Override public void get(String aPath, Callback aCallback) { - aCallback.resolve(getCache().getJsonObject(PREFERENCES_STORE_KEY)); + JSONObject json = getCache().getJsonObject(PREFERENCES_STORE_KEY); + + if (json == null) { + json = new JSONObject(); + } + + aCallback.resolve(json); } /** diff --git a/lib/app.js b/lib/app.js index 5e0b54bc..8095d3af 100644 --- a/lib/app.js +++ b/lib/app.js @@ -199,7 +199,7 @@ class App extends React.Component { debug('inflate user flags'); return api.get('preferences') .then(prefs => { - Object.keys(prefs ).forEach(key => { + Object.keys(prefs).forEach(key => { debug('got user flag', key, prefs[key]); const value = parseBoolean(prefs[key]); if (value == null) return;