Skip to content

Commit 1d600f3

Browse files
committed
[ISSUE #8037] Guarantee a non-null parsed Divide handle
1 parent 947c216 commit 1d600f3

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/rule/canary/CanaryConfigValidator.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private CanaryConfigValidator() {
4848
* An absent or null Canary configuration preserves legacy routing.
4949
*
5050
* @param handle the serialized rule handle
51-
* @return the validated handle
51+
* @return the validated handle, never null
5252
* @throws IllegalArgumentException when the configuration is invalid
5353
*/
5454
public static DivideRuleHandle parseHandle(final String handle) {
@@ -60,6 +60,11 @@ public static DivideRuleHandle parseHandle(final String handle) {
6060
}
6161
StickySessionConfigValidator.validateJson(root.get("sticky"));
6262
DivideRuleHandle result = GsonUtils.getInstance().fromJson(root, DivideRuleHandle.class);
63+
if (Objects.isNull(result)) {
64+
// the adapter always instantiates the handle for a JSON object; keep the
65+
// non-null contract explicit for callers that dereference it directly
66+
throw new IllegalArgumentException("Invalid Divide handle JSON");
67+
}
6368
if (Objects.nonNull(result.getCanary()) && result.getRetry() > 0
6469
&& !RetryEnum.CURRENT.getName().equals(StringUtils.defaultIfEmpty(result.getRetryStrategy(), RetryEnum.CURRENT.getName()))) {
6570
throw new IllegalArgumentException("Canary routing currently supports only current-node retries; failover requires partition-aware retry support");

‎shenyu-common/src/test/java/org/apache/shenyu/common/dto/convert/rule/canary/CanaryConfigValidatorTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static org.junit.jupiter.api.Assertions.assertAll;
2929
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3030
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3132
import static org.junit.jupiter.api.Assertions.assertNull;
3233
import static org.junit.jupiter.api.Assertions.assertThrows;
3334

@@ -125,6 +126,13 @@ void testNullStickyIsTreatedAsAbsent() {
125126
assertNull(CanaryConfigValidator.parseHandle("{\"sticky\":null}").getSticky());
126127
}
127128

129+
@Test
130+
void testParseHandleNeverReturnsNull() {
131+
assertNotNull(CanaryConfigValidator.parseHandle("{}"));
132+
assertNotNull(CanaryConfigValidator.parseHandle("{\"sticky\":null}"));
133+
assertThrows(IllegalArgumentException.class, () -> CanaryConfigValidator.parseHandle("null"));
134+
}
135+
128136
@Test
129137
void testStickyStructuralTypesAreNamedBeforeCoercion() {
130138
assertAll(

0 commit comments

Comments
 (0)