修复DictUtils未检查类型转换并补充单元测试
This commit is contained in:
18
doc/2026-04-15-DictUtils泛型告警修复后端实施记录.md
Normal file
18
doc/2026-04-15-DictUtils泛型告警修复后端实施记录.md
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# DictUtils 泛型告警修复后端实施记录
|
||||||
|
|
||||||
|
## 变更时间
|
||||||
|
- 2026-04-15
|
||||||
|
|
||||||
|
## 变更范围
|
||||||
|
- `ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java`
|
||||||
|
- `ruoyi-common/src/test/java/com/ruoyi/common/utils/DictUtilsTest.java`
|
||||||
|
|
||||||
|
## 变更内容
|
||||||
|
- 修复 `DictUtils#getDictCache` 中对缓存对象直接强转 `List<SysDictData>` 触发的未检查类型转换告警。
|
||||||
|
- 调整缓存读取顺序,优先处理 `JSONArray`,避免 JSON 数组被 `List` 分支提前命中后返回非 `SysDictData` 元素。
|
||||||
|
- 对普通 `List` 缓存执行逐项类型校验并复制为强类型结果列表。
|
||||||
|
- 新增 `JSONArray` 缓存场景测试,覆盖字典缓存反序列化读取逻辑。
|
||||||
|
|
||||||
|
## 验证结果
|
||||||
|
- 执行 `mvn -pl ruoyi-common -Dtest=DictUtilsTest test`,测试通过。
|
||||||
|
- 执行 `mvn -pl ruoyi-common -am clean compile`,编译通过,未再出现 `DictUtils.java` 的未检查类型转换告警。
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
package com.ruoyi.common.utils;
|
package com.ruoyi.common.utils;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.List;
|
||||||
import com.alibaba.fastjson2.JSONArray;
|
import java.util.Map;
|
||||||
|
import com.alibaba.fastjson2.JSONArray;
|
||||||
import com.ruoyi.common.constant.CacheConstants;
|
import com.ruoyi.common.constant.CacheConstants;
|
||||||
import com.ruoyi.common.core.domain.entity.SysDictData;
|
import com.ruoyi.common.core.domain.entity.SysDictData;
|
||||||
import com.ruoyi.common.core.redis.RedisCache;
|
import com.ruoyi.common.core.redis.RedisCache;
|
||||||
@@ -42,15 +43,25 @@ public class DictUtils
|
|||||||
public static List<SysDictData> getDictCache(String key)
|
public static List<SysDictData> getDictCache(String key)
|
||||||
{
|
{
|
||||||
Object cacheObject = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
|
Object cacheObject = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
|
||||||
if (cacheObject instanceof List)
|
|
||||||
{
|
|
||||||
return (List<SysDictData>) cacheObject;
|
|
||||||
}
|
|
||||||
if (cacheObject instanceof JSONArray)
|
if (cacheObject instanceof JSONArray)
|
||||||
{
|
{
|
||||||
JSONArray arrayCache = (JSONArray) cacheObject;
|
JSONArray arrayCache = (JSONArray) cacheObject;
|
||||||
return arrayCache.toList(SysDictData.class);
|
return arrayCache.toList(SysDictData.class);
|
||||||
}
|
}
|
||||||
|
if (cacheObject instanceof List<?>)
|
||||||
|
{
|
||||||
|
List<?> listCache = (List<?>) cacheObject;
|
||||||
|
List<SysDictData> dictDatas = new ArrayList<SysDictData>(listCache.size());
|
||||||
|
for (Object item : listCache)
|
||||||
|
{
|
||||||
|
if (!(item instanceof SysDictData))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
dictDatas.add((SysDictData) item);
|
||||||
|
}
|
||||||
|
return dictDatas;
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import com.alibaba.fastjson2.JSONArray;
|
||||||
import com.ruoyi.common.core.cache.InMemoryCacheStore;
|
import com.ruoyi.common.core.cache.InMemoryCacheStore;
|
||||||
import com.ruoyi.common.core.domain.entity.SysDictData;
|
import com.ruoyi.common.core.domain.entity.SysDictData;
|
||||||
import com.ruoyi.common.core.redis.RedisCache;
|
import com.ruoyi.common.core.redis.RedisCache;
|
||||||
@@ -43,4 +44,23 @@ class DictUtilsTest
|
|||||||
assertEquals("正常", dictCache.get(0).getDictLabel());
|
assertEquals("正常", dictCache.get(0).getDictLabel());
|
||||||
assertEquals("0", dictCache.get(0).getDictValue());
|
assertEquals("0", dictCache.get(0).getDictValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnDictListWhenCacheStoresJsonArray()
|
||||||
|
{
|
||||||
|
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
|
||||||
|
RedisCache redisCache = new RedisCache(new InMemoryCacheStore());
|
||||||
|
beanFactory.registerSingleton("redisCache", redisCache);
|
||||||
|
ReflectionTestUtils.setField(SpringUtils.class, "beanFactory", beanFactory);
|
||||||
|
|
||||||
|
JSONArray jsonArray = JSONArray.parseArray("[{\"dictType\":\"sys_normal_disable\",\"dictLabel\":\"正常\",\"dictValue\":\"0\"}]");
|
||||||
|
redisCache.setCacheObject(DictUtils.getCacheKey("sys_normal_disable"), jsonArray);
|
||||||
|
|
||||||
|
List<SysDictData> dictCache = DictUtils.getDictCache("sys_normal_disable");
|
||||||
|
|
||||||
|
assertNotNull(dictCache);
|
||||||
|
assertEquals(1, dictCache.size());
|
||||||
|
assertEquals("正常", dictCache.get(0).getDictLabel());
|
||||||
|
assertEquals("0", dictCache.get(0).getDictValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user