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

Commit bc12d19

Browse files
authored
fix ContextBuilder.copyFrom NPE (#15)
base `copyOnWrite*Attributes` on respective objects nullability otherwise they will not be initialized causing `NPE`
2 parents 0006632 + 84ac547 commit bc12d19

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/main/java/com/launchdarkly/sdk/ContextBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ ContextBuilder copyFrom(LDContext context) {
380380
anonymous = context.isAnonymous();
381381
attributes = context.attributes;
382382
privateAttributes = context.privateAttributes;
383-
copyOnWriteAttributes = true;
384-
copyOnWritePrivateAttributes = true;
383+
copyOnWriteAttributes = context.attributes != null;
384+
copyOnWritePrivateAttributes = context.privateAttributes != null;
385385
return this;
386386
}
387387

src/test/java/com/launchdarkly/sdk/ContextBuilderTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static org.hamcrest.Matchers.emptyIterable;
1111
import static org.hamcrest.Matchers.equalTo;
1212
import static org.hamcrest.Matchers.is;
13+
import static org.hamcrest.Matchers.notNullValue;
1314
import static org.hamcrest.Matchers.nullValue;
1415

1516
@SuppressWarnings("javadoc")
@@ -131,4 +132,14 @@ public void builderFromContext() {
131132
}
132133
}
133134
}
135+
136+
@Test
137+
public void doesNotThrowNPEWhenReusingContext() {
138+
LDContext initialContext = LDContext.builder("123456").build();
139+
LDContext downstreamContext = LDContext.builderFromContext(initialContext)
140+
.set("some_attribute", "someValue")
141+
.build();
142+
143+
assertThat(downstreamContext, notNullValue());
144+
}
134145
}

0 commit comments

Comments
 (0)