Skip to content

Commit 8128b8a

Browse files
authored
fix(intent): 防止 Redis 缓存 JSON 反序列化失败 (#103)
1 parent 17eaaa6 commit 8128b8a

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

bootstrap/src/main/java/com/nageoffer/ai/ragent/rag/core/intent/IntentNode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.nageoffer.ai.ragent.rag.core.intent;
1919

2020
import cn.hutool.core.util.StrUtil;
21+
import com.fasterxml.jackson.annotation.JsonIgnore;
2122
import com.nageoffer.ai.ragent.rag.enums.IntentKind;
2223
import com.nageoffer.ai.ragent.rag.enums.IntentLevel;
2324
import lombok.Builder;
@@ -167,6 +168,7 @@ public boolean isSystem() {
167168
* 返回当前意图实际参与检索的 Collection
168169
* 新字段优先,旧的单 Collection 字段仅作平滑升级兜底
169170
*/
171+
@JsonIgnore
170172
public List<String> getEffectiveCollectionNames() {
171173
LinkedHashSet<String> normalized = new LinkedHashSet<>();
172174
if (collectionNames != null) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.nageoffer.ai.ragent.rag.core.intent;
19+
20+
import com.fasterxml.jackson.databind.DeserializationFeature;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.util.List;
25+
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertFalse;
28+
29+
class IntentNodeJsonTest {
30+
31+
private final ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules()
32+
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
33+
34+
@Test
35+
void roundTripsCollectionNamesWithoutSerializingComputedProperty() throws Exception {
36+
IntentNode node = IntentNode.builder()
37+
.id("insurance")
38+
.collectionNames(List.of("insurance", " claims ", "insurance"))
39+
.build();
40+
41+
String json = objectMapper.writeValueAsString(node);
42+
IntentNode restored = objectMapper.readValue(json, IntentNode.class);
43+
44+
assertFalse(json.contains("effectiveCollectionNames"));
45+
assertEquals(List.of("insurance", "claims"), restored.getEffectiveCollectionNames());
46+
}
47+
48+
@Test
49+
void ignoresComputedPropertyFromExistingCacheEntry() throws Exception {
50+
String json = """
51+
{
52+
"id": "insurance",
53+
"collectionNames": ["insurance"],
54+
"effectiveCollectionNames": ["stale"]
55+
}
56+
""";
57+
58+
IntentNode restored = objectMapper.readValue(json, IntentNode.class);
59+
60+
assertEquals(List.of("insurance"), restored.getEffectiveCollectionNames());
61+
}
62+
}

0 commit comments

Comments
 (0)