Skip to content

Commit

Permalink
[@unimodules/core] Add support for null values to @unimodules/core an…
Browse files Browse the repository at this point in the history
…d expo-notifications (#8153)

# Why

Fixes #8142 (comment).

# How

Added support for `null` values to `expo-notifications` and `@unimodules/core`. Also, moved helper methods implementations to interface.

# Test Plan

Added more coverage to `test-suite` which confirmed that the fix is correct.
  • Loading branch information
sjchmiela committed May 5, 2020
1 parent 4cfc400 commit 385e7e0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 89 deletions.
10 changes: 10 additions & 0 deletions apps/test-suite/tests/NewNotifications.js
Expand Up @@ -1202,6 +1202,16 @@ async function sendTestPushNotification(expoPushToken, notificationOverrides) {
{
to: expoPushToken,
title: 'Hello from Expo server!',
data: {
firstLevelString: 'value',
firstLevelObject: {
secondLevelInteger: 2137,
secondLevelObject: {
thirdLevelList: [21, 3, 1995, null, 4, 15],
thirdLevelNull: null,
},
},
},
...notificationOverrides,
},
]),
Expand Down
2 changes: 2 additions & 0 deletions packages/@unimodules/core/CHANGELOG.md
Expand Up @@ -8,6 +8,8 @@

### 🐛 Bug fixes

- Fixed error when serializing a `Map` containing a `null` ([#8153](https://github.com/expo/expo/pull/8153) by [@sjchmiela](https://github.com/sjchmiela))

## [5.1.1] - 2020-05-05

### 🛠 Breaking changes
Expand Down
@@ -1,8 +1,5 @@
package org.unimodules.core.arguments;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -34,11 +31,6 @@ public Object get(String key) {
return mMap.get(key);
}

@Override
public boolean getBoolean(String key) {
return getBoolean(key, false);
}

@Override
public boolean getBoolean(String key, boolean defaultValue) {
Object value = mMap.get(key);
Expand All @@ -48,11 +40,6 @@ public boolean getBoolean(String key, boolean defaultValue) {
return defaultValue;
}

@Override
public double getDouble(String key) {
return getDouble(key, 0);
}

@Override
public double getDouble(String key, double defaultValue) {
Object value = mMap.get(key);
Expand All @@ -62,11 +49,6 @@ public double getDouble(String key, double defaultValue) {
return defaultValue;
}

@Override
public int getInt(String key) {
return getInt(key, 0);
}

@Override
public int getInt(String key, int defaultValue) {
Object value = mMap.get(key);
Expand All @@ -76,11 +58,6 @@ public int getInt(String key, int defaultValue) {
return defaultValue;
}

@Override
public String getString(String key) {
return getString(key, null);
}

@Override
public String getString(String key, String defaultValue) {
Object value = mMap.get(key);
Expand All @@ -90,11 +67,6 @@ public String getString(String key, String defaultValue) {
return defaultValue;
}

@Override
public List getList(String key) {
return getList(key, null);
}

@Override
public List getList(String key, List defaultValue) {
Object value = mMap.get(key);
Expand All @@ -104,11 +76,6 @@ public List getList(String key, List defaultValue) {
return defaultValue;
}

@Override
public Map getMap(String key) {
return getMap(key, null);
}

@Override
public Map getMap(String key, Map defaultValue) {
Object value = mMap.get(key);
Expand All @@ -127,41 +94,4 @@ public boolean isEmpty() {
public int size() {
return mMap.size();
}

@Override
public ReadableArguments getArguments(String key) {
Map value = getMap(key);
if (value != null) {
return new MapArguments(value);
}
return null;
}

@Override
public Bundle toBundle() {
Bundle bundle = new Bundle();
for (String key : mMap.keySet()) {
Object value = mMap.get(key);
if (value instanceof String) {
bundle.putString(key, (String) value);
} else if (value instanceof Integer) {
bundle.putInt(key, (Integer) value);
} else if (value instanceof Double) {
bundle.putDouble(key, (Double) value);
} else if (value instanceof Long) {
bundle.putLong(key, (Long) value);
} else if (value instanceof Boolean) {
bundle.putBoolean(key, (Boolean) value);
} else if (value instanceof ArrayList) {
bundle.putParcelableArrayList(key, (ArrayList) value);
} else if (value instanceof Map) {
bundle.putBundle(key, new MapArguments((Map) value).toBundle());
} else if (value instanceof Bundle) {
bundle.putBundle(key, (Bundle) value);
} else {
throw new UnsupportedOperationException("Could not put a value of " + value.getClass() + " to bundle.");
}
}
return bundle;
}
}
Expand Up @@ -2,6 +2,7 @@

import android.os.Bundle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand All @@ -13,35 +14,81 @@ public interface ReadableArguments {

Object get(String key);

boolean getBoolean(String key);
default boolean getBoolean(String key) {
return getBoolean(key, false);
}

boolean getBoolean(String key, boolean defaultValue);

double getDouble(String key);
default double getDouble(String key) {
return getDouble(key, 0);
}

double getDouble(String key, double defaultValue);

int getInt(String key);
default int getInt(String key) {
return getInt(key, 0);
}

int getInt(String key, int defaultValue);

String getString(String key);
default String getString(String key) {
return getString(key, null);
}

String getString(String key, String defaultValue);

List getList(String key);
default List getList(String key) {
return getList(key, null);
}

List getList(String key, List defaultValue);

Map getMap(String key);
default Map getMap(String key) {
return getMap(key, null);
}

Map getMap(String key, Map defaultValue);

ReadableArguments getArguments(String key);
@SuppressWarnings("unchecked")
default ReadableArguments getArguments(String key) {
Object value = get(key);
if (value instanceof Map) {
return new MapArguments((Map) value);
}
return null;
}

boolean isEmpty();

int size();

Bundle toBundle();
default Bundle toBundle() {
Bundle bundle = new Bundle();
for (String key : keys()) {
Object value = get(key);
if (value == null) {
bundle.putString(key, null);
} else if (value instanceof String) {
bundle.putString(key, (String) value);
} else if (value instanceof Integer) {
bundle.putInt(key, (Integer) value);
} else if (value instanceof Double) {
bundle.putDouble(key, (Double) value);
} else if (value instanceof Long) {
bundle.putLong(key, (Long) value);
} else if (value instanceof Boolean) {
bundle.putBoolean(key, (Boolean) value);
} else if (value instanceof ArrayList) {
bundle.putParcelableArrayList(key, (ArrayList) value);
} else if (value instanceof Map) {
bundle.putBundle(key, new MapArguments((Map) value).toBundle());
} else if (value instanceof Bundle) {
bundle.putBundle(key, (Bundle) value);
} else {
throw new UnsupportedOperationException("Could not put a value of " + value.getClass() + " to bundle.");
}
}
return bundle;
}
}
2 changes: 2 additions & 0 deletions packages/expo-notifications/CHANGELOG.md
Expand Up @@ -8,6 +8,8 @@

### 🐛 Bug fixes

- Fixed crash when serializing a notification containing a `null` value ([#8153](https://github.com/expo/expo/pull/8153) by [@sjchmiela](https://github.com/sjchmiela))

## [0.1.5] - 2020-05-05

### 🛠 Breaking changes
Expand Down
Expand Up @@ -91,17 +91,15 @@ private static Bundle toBundle(@Nullable JSONObject notification) {
Iterator<String> keyIterator = notification.keys();
while (keyIterator.hasNext()) {
String key = keyIterator.next();
try {
Object value = notification.get(key);
if (value instanceof JSONObject) {
notificationMap.put(key, toBundle((JSONObject) value));
} else if (value instanceof JSONArray) {
notificationMap.put(key, toList((JSONArray) value));
} else if (value != null) {
notificationMap.put(key, value);
}
} catch (JSONException e) {
Log.e("expo-notifications", "Could not serialize whole notification - dropped value for key " + key + ": " + notification.opt(key));
Object value = notification.opt(key);
if (value instanceof JSONObject) {
notificationMap.put(key, toBundle((JSONObject) value));
} else if (value instanceof JSONArray) {
notificationMap.put(key, toList((JSONArray) value));
} else if (JSONObject.NULL.equals(value)) {
notificationMap.put(key, null);
} else {
notificationMap.put(key, value);
}
}
return new MapArguments(notificationMap).toBundle();
Expand Down

0 comments on commit 385e7e0

Please sign in to comment.