Skip to content

Commit

Permalink
fix: Handle missing targeting key in updated OpenFeature SDK. (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion authored Jun 5, 2024
1 parent 7f59d87 commit db83218
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public LDContext toLdContext(EvaluationContext evaluationContext) {
return BuildSingleContext(evaluationContext.asMap(), finalKind, targetingKey);
}

private boolean isNullOrEmpty(String value) {
return value == null || value.isEmpty();
}

/**
* Get and validate a targeting key.
*
Expand All @@ -74,7 +78,7 @@ private String getTargetingKey(String targetingKey, Value keyAsValue) {
// Currently the targeting key will always have a value, but it can be empty.
// So we want to treat an empty string as a not defined one. Later it could
// become null, so we will need to check that.
if (!Objects.equals(targetingKey, "") && keyAsValue != null && keyAsValue.isString()) {
if (!isNullOrEmpty(targetingKey) && keyAsValue != null && keyAsValue.isString()) {
// There is both a targeting key and a key. It will work, but probably
// is not intentional.
logger.warn("EvaluationContext contained both a 'key' and 'targetingKey'.");
Expand All @@ -87,10 +91,10 @@ private String getTargetingKey(String targetingKey, Value keyAsValue) {

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

if (targetingKey == null || targetingKey.isEmpty()) {
if (isNullOrEmpty(targetingKey)) {
logger.error("The EvaluationContext must contain either a 'targetingKey' or a 'key' and the type " + "must be a string.");
}
return targetingKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.io.IOException;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.concurrent.TimeoutException;

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

0 comments on commit db83218

Please sign in to comment.