This is an automated email from the ASF dual-hosted git repository.

Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new ee8ccf952d fix: handle null discoveryUpstreams in proxy selector 
update (#6534)
ee8ccf952d is described below

commit ee8ccf952dc3841c47f5755bd1a3fdb630dee2b3
Author: lll-peanut <[email protected]>
AuthorDate: Sun Aug 2 23:10:02 2026 +0800

    fix: handle null discoveryUpstreams in proxy selector update (#6534)
    
    Signed-off-by: lll-peanut <[email protected]>
    Co-authored-by: aias00 <[email protected]>
---
 .../service/impl/ProxySelectorServiceImpl.java     | 38 ++++++++++++----------
 .../admin/service/ProxySelectorServiceTest.java    | 37 +++++++++++++++++++++
 2 files changed, 57 insertions(+), 18 deletions(-)

diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ProxySelectorServiceImpl.java
 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ProxySelectorServiceImpl.java
index a4b2a40fd9..d439a4e427 100644
--- 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ProxySelectorServiceImpl.java
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ProxySelectorServiceImpl.java
@@ -368,28 +368,30 @@ public class ProxySelectorServiceImpl implements 
ProxySelectorService {
         discoveryDO.setProps(discovery.getProps());
         discoveryMapper.updateSelective(discoveryDO);
         // update discovery upstream list
-        int result = 
discoveryUpstreamMapper.deleteByDiscoveryHandlerId(discoveryHandlerId);
-        LOG.info("delete discovery upstreams, count is: {}", result);
-        proxySelectorAddDTO.getDiscoveryUpstreams().forEach(discoveryUpstream 
-> {
-            DiscoveryUpstreamDO discoveryUpstreamDO = 
DiscoveryUpstreamDO.builder()
-                    .id(UUIDUtils.getInstance().generateShortUuid())
-                    .discoveryHandlerId(discoveryHandlerId)
-                    .namespaceId(discoveryDO.getNamespaceId())
-                    .protocol(discoveryUpstream.getProtocol())
-                    .url(discoveryUpstream.getUrl())
-                    .status(discoveryUpstream.getStatus())
-                    .weight(discoveryUpstream.getWeight())
-                    .props(discoveryUpstream.getProps())
-                    
.dateCreated(Optional.ofNullable(discoveryUpstream.getStartupTime()).map(t -> 
new Timestamp(Long.parseLong(t))).orElse(currentTime))
-                    
.dateUpdated(Optional.ofNullable(discoveryUpstream.getStartupTime()).map(t -> 
new Timestamp(Long.parseLong(t))).orElse(currentTime))
-                    .build();
-            discoveryUpstreamMapper.insert(discoveryUpstreamDO);
-        });
+        if 
(!CollectionUtils.isEmpty(proxySelectorAddDTO.getDiscoveryUpstreams())) {
+            int result = 
discoveryUpstreamMapper.deleteByDiscoveryHandlerId(discoveryHandlerId);
+            LOG.info("delete discovery upstreams, count is: {}", result);
+            
proxySelectorAddDTO.getDiscoveryUpstreams().forEach(discoveryUpstream -> {
+                DiscoveryUpstreamDO discoveryUpstreamDO = 
DiscoveryUpstreamDO.builder()
+                        .id(UUIDUtils.getInstance().generateShortUuid())
+                        .discoveryHandlerId(discoveryHandlerId)
+                        .namespaceId(discoveryDO.getNamespaceId())
+                        .protocol(discoveryUpstream.getProtocol())
+                        .url(discoveryUpstream.getUrl())
+                        .status(discoveryUpstream.getStatus())
+                        .weight(discoveryUpstream.getWeight())
+                        .props(discoveryUpstream.getProps())
+                        
.dateCreated(Optional.ofNullable(discoveryUpstream.getStartupTime()).map(t -> 
new Timestamp(Long.parseLong(t))).orElse(currentTime))
+                        
.dateUpdated(Optional.ofNullable(discoveryUpstream.getStartupTime()).map(t -> 
new Timestamp(Long.parseLong(t))).orElse(currentTime))
+                        .build();
+                discoveryUpstreamMapper.insert(discoveryUpstreamDO);
+            });
+            LOG.info("insert discovery upstreams, count is: {}", 
proxySelectorAddDTO.getDiscoveryUpstreams().size());
+        }
         List<DiscoveryUpstreamDTO> fetchAll = 
discoveryUpstreamMapper.selectByDiscoveryHandlerId(discoveryHandlerDO.getId()).stream()
                 
.map(DiscoveryTransfer.INSTANCE::mapToDTO).collect(Collectors.toList());
         DiscoveryProcessor discoveryProcessor = 
discoveryProcessorHolder.chooseProcessor(discoveryDO.getDiscoveryType());
         
discoveryProcessor.changeUpstream(DiscoveryTransfer.INSTANCE.mapToDTO(proxySelectorDO),
 fetchAll);
-        LOG.info("insert discovery upstreams, count is: {}", 
proxySelectorAddDTO.getDiscoveryUpstreams().size());
         return ShenyuResultMessage.UPDATE_SUCCESS;
     }
 
diff --git 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ProxySelectorServiceTest.java
 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ProxySelectorServiceTest.java
index 6948169dc8..d38951ec98 100644
--- 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ProxySelectorServiceTest.java
+++ 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ProxySelectorServiceTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.shenyu.admin.service;
 
+import org.apache.shenyu.admin.discovery.DiscoveryProcessor;
 import org.apache.shenyu.admin.discovery.DiscoveryProcessorHolder;
 import org.apache.shenyu.admin.mapper.DiscoveryHandlerMapper;
 import org.apache.shenyu.admin.mapper.DiscoveryMapper;
@@ -25,6 +26,8 @@ import org.apache.shenyu.admin.mapper.DiscoveryUpstreamMapper;
 import org.apache.shenyu.admin.mapper.ProxySelectorMapper;
 import org.apache.shenyu.admin.mapper.SelectorMapper;
 import org.apache.shenyu.admin.model.dto.ProxySelectorAddDTO;
+import org.apache.shenyu.admin.model.entity.DiscoveryDO;
+import org.apache.shenyu.admin.model.entity.DiscoveryHandlerDO;
 import org.apache.shenyu.admin.model.entity.DiscoveryRelDO;
 import org.apache.shenyu.admin.model.entity.ProxySelectorDO;
 import org.apache.shenyu.admin.model.page.PageParameter;
@@ -53,6 +56,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
 
 @ExtendWith(MockitoExtension.class)
 @MockitoSettings(strictness = Strictness.LENIENT)
@@ -119,6 +125,37 @@ class ProxySelectorServiceTest {
         assertEquals(proxySelectorService.createOrUpdate(proxySelectorDTO), 
ShenyuResultMessage.CREATE_SUCCESS);
     }
 
+    @Test
+    void testUpdateWithNullDiscoveryUpstreams() {
+
+        ProxySelectorAddDTO proxySelectorDTO = new ProxySelectorAddDTO();
+        proxySelectorDTO.setId("proxy-1");
+        proxySelectorDTO.setName("test");
+        proxySelectorDTO.setPluginName("test");
+        proxySelectorDTO.setForwardPort(8080);
+        proxySelectorDTO.setDiscovery(new ProxySelectorAddDTO.Discovery());
+
+        DiscoveryRelDO discoveryRelDO = new DiscoveryRelDO();
+        discoveryRelDO.setDiscoveryHandlerId("handler-1");
+        
given(discoveryRelMapper.selectByProxySelectorId("proxy-1")).willReturn(discoveryRelDO);
+
+        DiscoveryHandlerDO discoveryHandlerDO = new DiscoveryHandlerDO();
+        discoveryHandlerDO.setId("handler-1");
+        discoveryHandlerDO.setDiscoveryId("discovery-1");
+        
given(discoveryHandlerMapper.selectById("handler-1")).willReturn(discoveryHandlerDO);
+
+        DiscoveryDO discoveryDO = new DiscoveryDO();
+        discoveryDO.setDiscoveryType("local");
+        
given(discoveryMapper.selectById("discovery-1")).willReturn(discoveryDO);
+
+        DiscoveryProcessor discoveryProcessor = mock(DiscoveryProcessor.class);
+        
given(discoveryProcessorHolder.chooseProcessor("local")).willReturn(discoveryProcessor);
+        
given(discoveryUpstreamMapper.selectByDiscoveryHandlerId("handler-1")).willReturn(Collections.emptyList());
+
+        assertEquals(proxySelectorService.update(proxySelectorDTO), 
ShenyuResultMessage.UPDATE_SUCCESS);
+        verify(discoveryUpstreamMapper, 
never()).deleteByDiscoveryHandlerId(any());
+    }
+
     @Test
     void testDelete() {
 

Reply via email to