Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

fix ContextBuilder.copyFrom NPE #15

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/launchdarkly/sdk/ContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ ContextBuilder copyFrom(LDContext context) {
anonymous = context.isAnonymous();
attributes = context.attributes;
privateAttributes = context.privateAttributes;
copyOnWriteAttributes = true;
copyOnWritePrivateAttributes = true;
copyOnWriteAttributes = context.attributes != null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Objects.nonNull(context.attributes)

copyOnWritePrivateAttributes = context.privateAttributes != null;
return this;
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/launchdarkly/sdk/ContextBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

@SuppressWarnings("javadoc")
Expand Down Expand Up @@ -131,4 +132,14 @@ public void builderFromContext() {
}
}
}

@Test
public void doesNotThrowNPEWhenReusingContext() {
LDContext initialContext = LDContext.builder("123456").build();
LDContext downstreamContext = LDContext.builderFromContext(initialContext)
.set("some_attribute", "someValue")
.build();

assertThat(downstreamContext, notNullValue());
}
}