Skip to content

Commit c1a784f

Browse files
aymenfurterCopilot
andcommitted
sdk(java): Reject null in withContext to match the other SDKs
Java accepted null as a "clear" operation while .NET rejects it and the other SDKs disallow it at the type level. Since null is Java's default, an uninitialized variable would have silently dropped the context -- producing exactly the unattributed telemetry this feature removes. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 79152db2-4cc7-4777-983a-655fae5b68c9
1 parent c162ce7 commit c1a784f

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

java/src/main/java/com/github/copilot/rpc/PermissionRequestResult.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package com.github.copilot.rpc;
66

77
import java.util.List;
8+
import java.util.Objects;
89

910
import com.fasterxml.jackson.annotation.JsonIgnore;
1011
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -202,12 +203,14 @@ public PermissionDecisionContext getDecisionContext() {
202203
* sibling of {@code result}.
203204
*
204205
* @param context
205-
* the decision context, or {@code null} to clear it
206+
* the decision context; must not be {@code null}
206207
* @return this result for method chaining
208+
* @throws NullPointerException
209+
* if {@code context} is {@code null}
207210
* @since 1.3.0
208211
*/
209212
public PermissionRequestResult withContext(PermissionDecisionContext context) {
210-
this.decisionContext = context;
213+
this.decisionContext = Objects.requireNonNull(context, "context must not be null");
211214
return this;
212215
}
213216
}

java/src/test/java/com/github/copilot/rpc/PermissionRequestResultDecisionContextTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.junit.jupiter.api.Assertions.assertFalse;
99
import static org.junit.jupiter.api.Assertions.assertNull;
1010
import static org.junit.jupiter.api.Assertions.assertSame;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
1112
import static org.junit.jupiter.api.Assertions.assertTrue;
1213

1314
import com.fasterxml.jackson.databind.JsonNode;
@@ -85,4 +86,12 @@ void serializingResultWithContextDoesNotEmitContextInsideResult() throws Excepti
8586
"@JsonIgnore must keep decisionContext out of the serialized result");
8687
assertEquals(PermissionRequestResultKind.APPROVED.getValue(), resultJson.get("kind").asText());
8788
}
89+
90+
@Test
91+
void withContextRejectsNull() {
92+
var result = PermissionRequestResult.approveOnce();
93+
94+
assertThrows(NullPointerException.class, () -> result.withContext(null),
95+
"withContext must reject null rather than silently dropping the context");
96+
}
8897
}

0 commit comments

Comments
 (0)