Skip to content

Commit 7a71f03

Browse files
authored
Check for Null Parameters in the Feature APIs (#161)
* check for null parameters in feature APIs * add null parameter tests for isFeatureEnabled * add null parameter tests for getFeatureVariableBoolean * add null parameter tests for getFeatureVariableDouble * add null parameter tests for getFeatureVariableInteger * add null parameter tests for getFeatureVariableString
1 parent 429ad63 commit 7a71f03

File tree

2 files changed

+512
-15
lines changed

2 files changed

+512
-15
lines changed

core-api/src/main/java/com/optimizely/ab/Optimizely.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016-2017, Optimizely, Inc. and contributors *
2+
* Copyright 2016-2018, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -43,21 +43,19 @@
4343
import com.optimizely.ab.notification.NotificationBroadcaster;
4444
import com.optimizely.ab.notification.NotificationCenter;
4545
import com.optimizely.ab.notification.NotificationListener;
46-
4746
import org.slf4j.Logger;
4847
import org.slf4j.LoggerFactory;
4948

49+
import javax.annotation.CheckForNull;
50+
import javax.annotation.Nonnull;
51+
import javax.annotation.Nullable;
52+
import javax.annotation.concurrent.ThreadSafe;
5053
import java.util.ArrayList;
5154
import java.util.Collections;
5255
import java.util.HashMap;
5356
import java.util.List;
5457
import java.util.Map;
5558

56-
import javax.annotation.CheckForNull;
57-
import javax.annotation.Nonnull;
58-
import javax.annotation.Nullable;
59-
import javax.annotation.concurrent.ThreadSafe;
60-
6159
/**
6260
* Top-level container class for Optimizely functionality.
6361
* Thread-safe, so can be created as a singleton and safely passed around.
@@ -333,6 +331,14 @@ public void track(@Nonnull String eventName,
333331
public @Nonnull Boolean isFeatureEnabled(@Nonnull String featureKey,
334332
@Nonnull String userId,
335333
@Nonnull Map<String, String> attributes) {
334+
if (featureKey == null) {
335+
logger.warn("The featureKey parameter must be nonnull.");
336+
return false;
337+
}
338+
else if (userId == null) {
339+
logger.warn("The userId parameter must be nonnull.");
340+
return false;
341+
}
336342
FeatureFlag featureFlag = projectConfig.getFeatureKeyMapping().get(featureKey);
337343
if (featureFlag == null) {
338344
logger.info("No feature flag was found for key \"{}\".", featureKey);
@@ -533,6 +539,18 @@ String getFeatureVariableValueForType(@Nonnull String featureKey,
533539
@Nonnull String userId,
534540
@Nonnull Map<String, String> attributes,
535541
@Nonnull LiveVariable.VariableType variableType) {
542+
if (featureKey == null) {
543+
logger.warn("The featureKey parameter must be nonnull.");
544+
return null;
545+
}
546+
else if (variableKey == null) {
547+
logger.warn("The variableKey parameter must be nonnull.");
548+
return null;
549+
}
550+
else if (userId == null) {
551+
logger.warn("The userId parameter must be nonnull.");
552+
return null;
553+
}
536554
FeatureFlag featureFlag = projectConfig.getFeatureKeyMapping().get(featureKey);
537555
if (featureFlag == null) {
538556
logger.info("No feature flag was found for key \"{}\".", featureKey);

0 commit comments

Comments
 (0)