From 9262ddebe274daebdc70f0532dd91e8d3d92c58e Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 31 Aug 2026 21:18:13 +0000 Subject: [PATCH] fix: Exclude targeting keys from custom attributes Co-Authored-By: rlamb@launchdarkly.com <4955475+kinyoklion@users.noreply.github.com> --- src/EvaluationContextConverter.php | 2 +- tests/EvaluationContextConverterTest.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/EvaluationContextConverter.php b/src/EvaluationContextConverter.php index b838f8c..135ae89 100644 --- a/src/EvaluationContextConverter.php +++ b/src/EvaluationContextConverter.php @@ -131,7 +131,7 @@ private function buildSingleContext(array $attributes, string $kind, string $key foreach ($attributes as $k => $v) { // Key has been processed, so we can skip it. - if ($k === "key" || $key === "targetingKey") { + if ($k === "key" || $k === "targetingKey") { continue; } diff --git a/tests/EvaluationContextConverterTest.php b/tests/EvaluationContextConverterTest.php index 281ad38..ad5f157 100644 --- a/tests/EvaluationContextConverterTest.php +++ b/tests/EvaluationContextConverterTest.php @@ -156,6 +156,23 @@ public function testCanCreateMultiKindContext(): void $this->assertEquals("Org name", $orgContext->getName()); } + public function testMultiContextTargetingKeyIsNotAddedAsAnAttribute(): void + { + $attributes = [ + 'kind' => 'multi', + 'user' => ['targetingKey' => 'user-key', 'name' => 'User name'] + ]; + $context = new EvaluationContext(null, new Attributes($attributes)); + + $ldContext = $this->contextConverter->toLdContext($context); + + /** @var \LaunchDarkly\LDContext */ + $userContext = $ldContext->getIndividualContext("user"); + $this->assertEquals("user-key", $userContext->getKey()); + $contextJson = $userContext->jsonSerialize(); + $this->assertArrayNotHasKey("targetingKey", $contextJson); + } + public function testMultiContextDiscardsInvalidSingleKind(): void { $attributes = [