Prevent returning cache values when null cache key is set (#1097)
Previously, setting a null cache key could return a cached task or result. This explicitly prevents that from happening. The new tests failed before and pass now.
Fixes #1092
diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java b/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java
index 5f2dd6a..b24e2bc 100644
--- a/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java
+++ b/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java
@@ -327,7 +327,7 @@
*/
private static LottieTask<LottieComposition> cache(
@Nullable final String cacheKey, Callable<LottieResult<LottieComposition>> callable) {
- final LottieComposition cachedComposition = LottieCompositionCache.getInstance().get(cacheKey);
+ final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey);
if (cachedComposition != null) {
return new LottieTask<>(new Callable<LottieResult<LottieComposition>>() {
@Override
@@ -336,7 +336,7 @@
}
});
}
- if (taskCache.containsKey(cacheKey)) {
+ if (cacheKey != null && taskCache.containsKey(cacheKey)) {
return taskCache.get(cacheKey);
}
diff --git a/lottie/src/test/java/com/airbnb/lottie/LottieCompositionFactoryTest.java b/lottie/src/test/java/com/airbnb/lottie/LottieCompositionFactoryTest.java
index ba2c162..653391c 100644
--- a/lottie/src/test/java/com/airbnb/lottie/LottieCompositionFactoryTest.java
+++ b/lottie/src/test/java/com/airbnb/lottie/LottieCompositionFactoryTest.java
@@ -16,6 +16,7 @@
import java.io.StringReader;
import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
@@ -80,4 +81,20 @@
assertNotNull(result.getException());
assertNull(result.getValue());
}
+
+ @Test
+ public void testNullMultipleTimesAsync() {
+ JsonReader reader = new JsonReader(new StringReader(JSON));
+ LottieTask<LottieComposition> task1 = LottieCompositionFactory.fromJsonReader(reader, null);
+ LottieTask<LottieComposition> task2 = LottieCompositionFactory.fromJsonReader(reader, null);
+ assertFalse(task1 == task2);
+ }
+
+ @Test
+ public void testNullMultipleTimesSync() {
+ JsonReader reader = new JsonReader(new StringReader(JSON));
+ LottieResult<LottieComposition> task1 = LottieCompositionFactory.fromJsonReaderSync(reader, null);
+ LottieResult<LottieComposition> task2 = LottieCompositionFactory.fromJsonReaderSync(reader, null);
+ assertFalse(task1 == task2);
+ }
}