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

dengliming 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 ce8801cc64 fix: update tag relations logic to clear existing tags when 
tagIds is empty and skip processing when null (#6948)
ce8801cc64 is described below

commit ce8801cc640885d4545c64002ad2ad95b914007b
Author: Limbo <[email protected]>
AuthorDate: Thu Sep 17 23:33:42 2026 +0800

    fix: update tag relations logic to clear existing tags when tagIds is empty 
and skip processing when null (#6948)
    
    Co-authored-by: Liming Deng <[email protected]>
---
 .../shenyu/admin/service/impl/ApiServiceImpl.java  | 22 ++++++++++++----------
 .../shenyu/admin/service/ApiServiceTest.java       | 22 ++++++++++++++++++++++
 2 files changed, 34 insertions(+), 10 deletions(-)

diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ApiServiceImpl.java
 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ApiServiceImpl.java
index 6386166fe2..8ac6ae4f74 100644
--- 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ApiServiceImpl.java
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ApiServiceImpl.java
@@ -112,18 +112,20 @@ public class ApiServiceImpl implements ApiService {
         ApiDO apiDO = ApiDO.buildApiDO(apiDTO);
         final int updateRows = apiMapper.updateByPrimaryKeySelective(apiDO);
         if (updateRows > 0) {
-            if (CollectionUtils.isNotEmpty(apiDTO.getTagIds())) {
+            if (Objects.nonNull(apiDTO.getTagIds())) {
                 List<String> tagIds = apiDTO.getTagIds();
-                Timestamp currentTime = new 
Timestamp(System.currentTimeMillis());
-                List<TagRelationDO> tags = tagIds.stream().map(tagId -> 
TagRelationDO.builder()
-                    .id(UUIDUtils.getInstance().generateShortUuid())
-                    .apiId(apiDO.getId())
-                    .tagId(tagId)
-                    .dateCreated(currentTime)
-                    .dateUpdated(currentTime)
-                    .build()).collect(Collectors.toList());
                 tagRelationMapper.deleteByApiId(apiDO.getId());
-                tagRelationMapper.batchInsert(tags);
+                if (CollectionUtils.isNotEmpty(tagIds)) {
+                    Timestamp currentTime = new 
Timestamp(System.currentTimeMillis());
+                    List<TagRelationDO> tags = tagIds.stream().map(tagId -> 
TagRelationDO.builder()
+                        .id(UUIDUtils.getInstance().generateShortUuid())
+                        .apiId(apiDO.getId())
+                        .tagId(tagId)
+                        .dateCreated(currentTime)
+                        .dateUpdated(currentTime)
+                        .build()).collect(Collectors.toList());
+                    tagRelationMapper.batchInsert(tags);
+                }
             }
         }
         return ShenyuResultMessage.UPDATE_SUCCESS;
diff --git 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ApiServiceTest.java
 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ApiServiceTest.java
index 5c789f5482..0b69cbf3a1 100644
--- 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ApiServiceTest.java
+++ 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ApiServiceTest.java
@@ -57,6 +57,7 @@ import static org.mockito.ArgumentMatchers.anyList;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -101,6 +102,27 @@ public final class ApiServiceTest {
         testUpdate("123");
     }
 
+    @Test
+    public void testUpdateWithEmptyTagIdsClearsRelations() {
+        ApiDTO apiDTO = buildApiDTO("123");
+        apiDTO.setTagIds(Collections.emptyList());
+        
when(apiMapper.updateByPrimaryKeySelective(any(ApiDO.class))).thenReturn(1);
+
+        assertEquals(ShenyuResultMessage.UPDATE_SUCCESS, 
apiService.createOrUpdate(apiDTO));
+        verify(tagRelationMapper).deleteByApiId("123");
+        verify(tagRelationMapper, never()).batchInsert(anyList());
+    }
+
+    @Test
+    public void testUpdateWithNullTagIdsKeepsRelations() {
+        ApiDTO apiDTO = buildApiDTO("123");
+        
when(apiMapper.updateByPrimaryKeySelective(any(ApiDO.class))).thenReturn(1);
+
+        assertEquals(ShenyuResultMessage.UPDATE_SUCCESS, 
apiService.createOrUpdate(apiDTO));
+        verify(tagRelationMapper, never()).deleteByApiId("123");
+        verify(tagRelationMapper, never()).batchInsert(anyList());
+    }
+
     @Test
     public void testDelete() {
         List<ApiDO> apis = Collections.singletonList(buildApiDO("123"));

Reply via email to