Skip to content

Commit

Permalink
missing targeting key should return null
Browse files Browse the repository at this point in the history
Problem:
getTargetingKey of missing targeting key causing NullPointerException
Solution:
getTargetingKey of missing targeting key returns null

Signed-off-by: liran2000 <[email protected]>
  • Loading branch information
liran2000 authored and Kavindu-Dodan committed Mar 20, 2024
1 parent ee49872 commit 2be7f2b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/dev/openfeature/sdk/ImmutableContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public ImmutableContext(String targetingKey, Map<String, Value> attributes) {
*/
@Override
public String getTargetingKey() {
return this.getValue(TARGETING_KEY).asString();
Value value = this.getValue(TARGETING_KEY);
return value == null ? null : value.asString();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dev/openfeature/sdk/MutableContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public void setTargetingKey(String targetingKey) {
*/
@Override
public String getTargetingKey() {
return this.getValue(TARGETING_KEY).asString();
Value value = this.getValue(TARGETING_KEY);
return value == null ? null : value.asString();
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/dev/openfeature/sdk/ImmutableContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ void shouldRetainTargetingKeyWhenOverridingContextTargetingKeyValueIsEmpty() {
EvaluationContext merge = ctx.merge(overriding);
assertEquals("targeting_key", merge.getTargetingKey());
}
@DisplayName("missing targeting key should return null")
@Test
void missingTargetingKeyShould() {
EvaluationContext ctx = new ImmutableContext();
assertEquals(null, ctx.getTargetingKey());
}

@DisplayName("Merge should retain all the attributes from the existing context when overriding context is null")
@Test
Expand Down

0 comments on commit 2be7f2b

Please sign in to comment.