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 0e3fa1d391 fix(admin): preserve tag references during deletion (#7245)
0e3fa1d391 is described below

commit 0e3fa1d391bc20f8e1808cf2951513c524d4a710
Author: Liming Deng <[email protected]>
AuthorDate: Wed Sep 30 09:59:32 2026 +0800

    fix(admin): preserve tag references during deletion (#7245)
---
 .../shenyu/admin/mapper/TagRelationMapper.java     |  9 ++-
 .../shenyu/admin/service/impl/TagServiceImpl.java  | 14 +++-
 .../main/resources/mappers/tag-relation-sqlmap.xml |  8 ++
 .../admin/service/TagDeletionIntegrationTest.java  | 89 ++++++++++++++++++++++
 .../shenyu/admin/service/TagServiceTest.java       | 21 +++++
 5 files changed, 139 insertions(+), 2 deletions(-)

diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/TagRelationMapper.java
 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/TagRelationMapper.java
index ceb95a8f67..67622e601e 100644
--- 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/TagRelationMapper.java
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/TagRelationMapper.java
@@ -56,6 +56,14 @@ public interface TagRelationMapper extends ExistProvider {
      */
     int deleteByIds(List<String> ids);
 
+    /**
+     * Delete relations belonging to the specified tags.
+     *
+     * @param tagIds tag ids
+     * @return deleted rows
+     */
+    int deleteByTagIds(@Param("tagIds") List<String> tagIds);
+
     /**
      * update record selective.
      *
@@ -125,4 +133,3 @@ public interface TagRelationMapper extends ExistProvider {
      */
     int deleteByApiIds(@Param(value = "apiIds") List<String> apiIds);
 }
-
diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/TagServiceImpl.java
 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/TagServiceImpl.java
index e42ff6cf9d..7a373675f5 100644
--- 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/TagServiceImpl.java
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/TagServiceImpl.java
@@ -21,6 +21,7 @@ import com.google.common.collect.Lists;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.admin.mapper.TagMapper;
+import org.apache.shenyu.admin.mapper.TagRelationMapper;
 import org.apache.shenyu.admin.model.dto.TagDTO;
 import org.apache.shenyu.admin.model.entity.BaseDO;
 import org.apache.shenyu.admin.model.entity.TagDO;
@@ -31,6 +32,7 @@ import org.apache.shenyu.admin.utils.Assert;
 import org.apache.shenyu.common.constant.AdminConstants;
 import org.apache.shenyu.common.utils.GsonUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.sql.Timestamp;
 import java.util.List;
@@ -49,8 +51,11 @@ public class TagServiceImpl implements TagService {
 
     private final TagMapper tagMapper;
 
-    public TagServiceImpl(final TagMapper tagMapper) {
+    private final TagRelationMapper tagRelationMapper;
+
+    public TagServiceImpl(final TagMapper tagMapper, final TagRelationMapper 
tagRelationMapper) {
         this.tagMapper = tagMapper;
+        this.tagRelationMapper = tagRelationMapper;
     }
 
     @Override
@@ -104,7 +109,14 @@ public class TagServiceImpl implements TagService {
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public int delete(final List<String> ids) {
+        if (CollectionUtils.isEmpty(ids)) {
+            return 0;
+        }
+        
Assert.isTrue(tagMapper.selectByParentTagIds(ids).stream().allMatch(tag -> 
ids.contains(tag.getId())),
+                "cannot delete tags with remaining children");
+        tagRelationMapper.deleteByTagIds(ids);
         return tagMapper.deleteByIds(ids);
     }
 
diff --git a/shenyu-admin/src/main/resources/mappers/tag-relation-sqlmap.xml 
b/shenyu-admin/src/main/resources/mappers/tag-relation-sqlmap.xml
index 647ef5154c..7dc16797ef 100644
--- a/shenyu-admin/src/main/resources/mappers/tag-relation-sqlmap.xml
+++ b/shenyu-admin/src/main/resources/mappers/tag-relation-sqlmap.xml
@@ -155,6 +155,14 @@
         </foreach>
     </delete>
 
+    <delete id="deleteByTagIds">
+        DELETE FROM tag_relation
+        WHERE tag_id IN
+        <foreach item="id" collection="tagIds" open="(" separator="," 
close=")">
+            #{id, jdbcType=VARCHAR}
+        </foreach>
+    </delete>
+
     <insert id="batchInsert" parameterType="java.util.List">
         INSERT INTO tag_relation
         (id,
diff --git 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/TagDeletionIntegrationTest.java
 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/TagDeletionIntegrationTest.java
new file mode 100644
index 0000000000..a3680a9c69
--- /dev/null
+++ 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/TagDeletionIntegrationTest.java
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.shenyu.admin.service;
+
+import jakarta.annotation.Resource;
+import org.apache.shenyu.admin.AbstractSpringIntegrationTest;
+import org.apache.shenyu.admin.exception.ValidFailException;
+import org.junit.jupiter.api.Test;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.support.TransactionTemplate;
+
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Integration tests for atomic tag deletion.
+ */
+public class TagDeletionIntegrationTest extends AbstractSpringIntegrationTest {
+
+    @Resource
+    private TagService tagService;
+
+    @Resource
+    private JdbcTemplate jdbcTemplate;
+
+    @Resource
+    private PlatformTransactionManager transactionManager;
+
+    @Test
+    public void testDeleteRelationsAndPreserveOtherTags() {
+        new 
TransactionTemplate(transactionManager).executeWithoutResult(status -> {
+            insertTag("delete-parent", "/");
+            insertTag("delete-child", "delete-parent");
+            insertTag("keep-tag", "/");
+            assertThrows(ValidFailException.class, () -> 
tagService.delete(List.of("delete-parent")));
+            assertEquals(3, countRelations());
+            assertEquals(2, tagService.delete(List.of("delete-parent", 
"delete-child")));
+            assertEquals(1, countRelations());
+            assertEquals(1, jdbcTemplate.queryForObject("SELECT COUNT(*) FROM 
tag WHERE id = 'keep-tag'", Integer.class));
+            status.setRollbackOnly();
+        });
+    }
+
+    @Test
+    public void testRollbackRestoresTagsAndRelations() {
+        TransactionTemplate transaction = new 
TransactionTemplate(transactionManager);
+        transaction.executeWithoutResult(status -> insertTag("delete-parent", 
"/"));
+        try {
+            transaction.executeWithoutResult(status -> {
+                assertEquals(1, tagService.delete(List.of("delete-parent")));
+                assertEquals(0, countRelations());
+                status.setRollbackOnly();
+            });
+            assertEquals(1, countRelations());
+            assertEquals(1, jdbcTemplate.queryForObject("SELECT COUNT(*) FROM 
tag WHERE id = 'delete-parent'", Integer.class));
+        } finally {
+            tagService.delete(List.of("delete-parent"));
+        }
+    }
+
+    private void insertTag(final String id, final String parentId) {
+        jdbcTemplate.update("INSERT INTO tag (id, tag_name, tag_desc, 
parent_tag_id, ext) VALUES (?, ?, '', ?, '{}')", id, id, parentId);
+        jdbcTemplate.update("INSERT INTO tag_relation (id, api_id, tag_id) 
VALUES (?, 'deletion-api', ?)", id, id);
+    }
+
+    private int countRelations() {
+        return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM tag_relation 
WHERE api_id = 'deletion-api'", Integer.class);
+    }
+}
+
diff --git 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/TagServiceTest.java
 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/TagServiceTest.java
index cd38346c27..79cb1a4ea9 100644
--- 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/TagServiceTest.java
+++ 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/TagServiceTest.java
@@ -20,6 +20,7 @@ package org.apache.shenyu.admin.service;
 import com.google.common.collect.Lists;
 import org.apache.shenyu.admin.exception.ValidFailException;
 import org.apache.shenyu.admin.mapper.TagMapper;
+import org.apache.shenyu.admin.mapper.TagRelationMapper;
 import org.apache.shenyu.admin.model.dto.TagDTO;
 import org.apache.shenyu.admin.model.entity.TagDO;
 import org.apache.shenyu.admin.model.vo.TagVO;
@@ -42,6 +43,9 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
 
 /**
  * Test cases for TagService.
@@ -53,6 +57,9 @@ public class TagServiceTest {
     @Mock
     private TagMapper tagMapper;
 
+    @Mock
+    private TagRelationMapper tagRelationMapper;
+
     @InjectMocks
     private TagServiceImpl tagService;
 
@@ -98,6 +105,20 @@ public class TagServiceTest {
         assertEquals(cnt, 1);
     }
 
+    @Test
+    public void testDeleteRejectsRemainingChildren() {
+        
given(tagMapper.selectByParentTagIds(any())).willReturn(List.of(buildTagDO()));
+        assertThrows(ValidFailException.class, () -> 
tagService.delete(List.of("parent")));
+        verifyNoInteractions(tagRelationMapper);
+        verify(tagMapper, never()).deleteByIds(any());
+    }
+
+    @Test
+    public void testDeleteEmptyList() {
+        assertEquals(0, tagService.delete(List.of()));
+        verifyNoInteractions(tagMapper, tagRelationMapper);
+    }
+
     @Test
     public void testFindById() {
         
given(this.tagMapper.selectByPrimaryKey(any())).willReturn(buildTagDO());

Reply via email to