Skip to content

Commit db83218

Browse files
authored
fix: Handle missing targeting key in updated OpenFeature SDK. (#21)
1 parent 7f59d87 commit db83218

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public LDContext toLdContext(EvaluationContext evaluationContext) {
6363
return BuildSingleContext(evaluationContext.asMap(), finalKind, targetingKey);
6464
}
6565

66+
private boolean isNullOrEmpty(String value) {
67+
return value == null || value.isEmpty();
68+
}
69+
6670
/**
6771
* Get and validate a targeting key.
6872
*
@@ -74,7 +78,7 @@ private String getTargetingKey(String targetingKey, Value keyAsValue) {
7478
// Currently the targeting key will always have a value, but it can be empty.
7579
// So we want to treat an empty string as a not defined one. Later it could
7680
// become null, so we will need to check that.
77-
if (!Objects.equals(targetingKey, "") && keyAsValue != null && keyAsValue.isString()) {
81+
if (!isNullOrEmpty(targetingKey) && keyAsValue != null && keyAsValue.isString()) {
7882
// There is both a targeting key and a key. It will work, but probably
7983
// is not intentional.
8084
logger.warn("EvaluationContext contained both a 'key' and 'targetingKey'.");
@@ -87,10 +91,10 @@ private String getTargetingKey(String targetingKey, Value keyAsValue) {
8791

8892
if (keyAsValue != null && keyAsValue.isString()) {
8993
// Targeting key takes precedence over key, because targeting key is in the spec.
90-
targetingKey = !Objects.equals(targetingKey, "") ? targetingKey : keyAsValue.asString();
94+
targetingKey = !isNullOrEmpty(targetingKey) ? targetingKey : keyAsValue.asString();
9195
}
9296

93-
if (targetingKey == null || targetingKey.isEmpty()) {
97+
if (isNullOrEmpty(targetingKey)) {
9498
logger.error("The EvaluationContext must contain either a 'targetingKey' or a 'key' and the type " + "must be a string.");
9599
}
96100
return targetingKey;

src/main/java/com/launchdarkly/openfeature/serverprovider/Provider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.io.IOException;
1414
import java.time.temporal.ChronoUnit;
1515
import java.util.Collections;
16-
import java.util.concurrent.TimeoutException;
1716

1817
/**
1918
* An OpenFeature {@link FeatureProvider} which enables the use of the LaunchDarkly Server-Side SDK for Java

0 commit comments

Comments
 (0)