From ddba70afcc7d87a8b50f3b60d432575358a87f8d Mon Sep 17 00:00:00 2001 From: Pedro Dias Date: Thu, 9 Nov 2023 15:29:32 +0000 Subject: [PATCH 1/2] Update ContextBuilder.java base copyOnWrite*Attributes on respective objects nullability otherwise they will not be initialized causing NPE --- src/main/java/com/launchdarkly/sdk/ContextBuilder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/launchdarkly/sdk/ContextBuilder.java b/src/main/java/com/launchdarkly/sdk/ContextBuilder.java index 731a5a5..ac220c2 100644 --- a/src/main/java/com/launchdarkly/sdk/ContextBuilder.java +++ b/src/main/java/com/launchdarkly/sdk/ContextBuilder.java @@ -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; + copyOnWritePrivateAttributes = context.privateAttributes != null; return this; } From 84ac547e8e54b2895da551389baf70f04fe43605 Mon Sep 17 00:00:00 2001 From: Pedro Dias Date: Thu, 9 Nov 2023 17:32:29 +0000 Subject: [PATCH 2/2] add test --- .../java/com/launchdarkly/sdk/ContextBuilderTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/java/com/launchdarkly/sdk/ContextBuilderTest.java b/src/test/java/com/launchdarkly/sdk/ContextBuilderTest.java index d30bff9..2d8e491 100644 --- a/src/test/java/com/launchdarkly/sdk/ContextBuilderTest.java +++ b/src/test/java/com/launchdarkly/sdk/ContextBuilderTest.java @@ -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") @@ -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()); + } }