File tree Expand file tree Collapse file tree
main/java/org/apache/shenyu/common/dto/convert/rule/canary
test/java/org/apache/shenyu/common/dto/convert/rule/canary Expand file tree Collapse file tree Original file line number Diff line number Diff 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" );
Original file line number Diff line number Diff line change 2828import static org .junit .jupiter .api .Assertions .assertAll ;
2929import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
3030import static org .junit .jupiter .api .Assertions .assertEquals ;
31+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
3132import static org .junit .jupiter .api .Assertions .assertNull ;
3233import 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 (
You can’t perform that action at this time.
0 commit comments