From 1b13efffc59e4e8c5d06247f3c87593d17f7ee28 Mon Sep 17 00:00:00 2001 From: movingsam Date: Sun, 8 Mar 2026 11:01:49 +0800 Subject: [PATCH] fix: add null check in RouteCache.GetRoute to prevent ArgumentNullException - Allow nullable tenantCode and serviceName parameters - Return null early if serviceName is null or empty - Fix test: GetRoute_WithNullServiceName_ShouldReturnNull --- src/yarpgateway/Services/RouteCache.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/yarpgateway/Services/RouteCache.cs b/src/yarpgateway/Services/RouteCache.cs index 4d9ebed..0656d01 100644 --- a/src/yarpgateway/Services/RouteCache.cs +++ b/src/yarpgateway/Services/RouteCache.cs @@ -54,13 +54,23 @@ public class RouteCache : IRouteCache _logger.LogInformation("Route cache reloaded"); } - public RouteInfo? GetRoute(string tenantCode, string serviceName) + public RouteInfo? GetRoute(string? tenantCode, string? serviceName) { + // 参数校验 + if (string.IsNullOrEmpty(serviceName)) + { + _logger.LogDebug("GetRoute called with null or empty serviceName"); + return null; + } + + tenantCode ??= string.Empty; + _lock.EnterUpgradeableReadLock(); try { // 1. 优先查找租户专用路由 - if (_tenantRoutes.TryGetValue(tenantCode, out var tenantRouteMap) && + if (!string.IsNullOrEmpty(tenantCode) && + _tenantRoutes.TryGetValue(tenantCode, out var tenantRouteMap) && tenantRouteMap.TryGetValue(serviceName, out var tenantRoute)) { _logger.LogDebug("Found tenant-specific route: {Tenant}/{Service} -> {Cluster}",