Skip to content

Commit 00e6e50

Browse files
han liangyuanhan liangyuan
authored andcommitted
fix: support autoType when keyType is String for Map typed reader
1 parent 460d2e4 commit 00e6e50

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplMapTyped.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName
318318
} else if (keyType == String.class) {
319319
name = jsonReader.readFieldName();
320320
if (index == 0
321-
&& (contextFeatures & JSONReader.Feature.SupportAutoType.mask) != 0
321+
&& (jsonReader.isEnabled(JSONReader.Feature.SupportAutoType) || jsonReader.getContext().getContextAutoTypeBeforeHandler() != null)
322322
&& name.equals(getTypeKey())
323323
) {
324324
if (seenKeys != null && !seenKeys.add(getTypeKey())) {

‎core/src/test/java/com/alibaba/fastjson2/reader/ObjectReaderImplMapTypedTest.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.alibaba.fastjson2.reader;
22

3+
import com.alibaba.fastjson2.JSON;
4+
import com.alibaba.fastjson2.JSONReader;
35
import org.junit.jupiter.api.Tag;
46
import org.junit.jupiter.api.Test;
57

68
import java.util.HashMap;
9+
import java.util.LinkedHashMap;
710
import java.util.Map;
811
import java.util.TreeMap;
912

1013
import static org.junit.jupiter.api.Assertions.assertEquals;
1114
import static org.junit.jupiter.api.Assertions.assertNull;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
1216

1317
@Tag("reader")
1418
public class ObjectReaderImplMapTypedTest {
@@ -28,4 +32,39 @@ public void test() {
2832
assertEquals(1, instance.size());
2933
assertNull(instance.get("value"));
3034
}
35+
36+
static class Param {
37+
Map<String, Val> varsMap;
38+
39+
public Map<String, Val> getVarsMap() {
40+
return varsMap;
41+
}
42+
43+
public void setVarsMap(Map<String, Val> varsMap) {
44+
this.varsMap = varsMap;
45+
}
46+
}
47+
48+
static class Val {
49+
String name;
50+
51+
public String getName() {
52+
return name;
53+
}
54+
55+
public void setName(String name) {
56+
this.name = name;
57+
}
58+
}
59+
60+
@Test
61+
void testMapStringKeyInnerAutoTypeWithWhitelist() {
62+
String json = "{\"varsMap\":{\"@type\":\"java.util.LinkedHashMap\",\"1\":{\"name\":\"x\"}}}";
63+
Param param = JSON.parseObject(
64+
json,
65+
Param.class,
66+
JSONReader.autoTypeFilter(false, "java.util.", "com.alibaba.fastjson2.reader.")
67+
);
68+
assertTrue(param.getVarsMap() instanceof LinkedHashMap);
69+
}
3170
}

0 commit comments

Comments
 (0)