Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName
} else if (keyType == String.class) {
name = jsonReader.readFieldName();
if (index == 0
&& (contextFeatures & JSONReader.Feature.SupportAutoType.mask) != 0
&& jsonReader.isSupportAutoTypeOrHandler(features)
&& name.equals(getTypeKey())
) {
if (seenKeys != null && !seenKeys.add(getTypeKey())) {
Expand All @@ -345,7 +345,7 @@ public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName
}
} else {
if (index == 0
&& (jsonReader.isEnabled(JSONReader.Feature.SupportAutoType) || jsonReader.getContext().getContextAutoTypeBeforeHandler() != null)
&& jsonReader.isSupportAutoTypeOrHandler(features)
&& jsonReader.current() == '"'
&& !(keyType instanceof Class && Enum.class.isAssignableFrom((Class) keyType))
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.alibaba.fastjson2.reader;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Tag("reader")
public class ObjectReaderImplMapTypedTest {
Expand All @@ -28,4 +32,46 @@ public void test() {
assertEquals(1, instance.size());
assertNull(instance.get("value"));
}

static class Param {
Map<String, Val> varsMap;

public Map<String, Val> getVarsMap() {
return varsMap;
}

public void setVarsMap(Map<String, Val> varsMap) {
this.varsMap = varsMap;
}
}

static class Val {
String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

@Test
void testMapStringKeyInnerAutoTypeWithWhitelist() {
String json = "{\"varsMap\":{\"@type\":\"java.util.LinkedHashMap\",\"1\":{\"name\":\"x\"}}}";
Param param = JSON.parseObject(
json,
Param.class,
JSONReader.autoTypeFilter(false, "java.util.", "com.alibaba.fastjson2.reader.")
);
assertTrue(param.getVarsMap() instanceof LinkedHashMap);
}

@Test
void testMapStringKeyInnerAutoTypeWithSupportAutoTypeFeature() {
String json = "{\"varsMap\":{\"@type\":\"java.util.LinkedHashMap\",\"1\":{\"name\":\"x\"}}}";
Param param = JSON.parseObject(json, Param.class, JSONReader.Feature.SupportAutoType);
assertTrue(param.getVarsMap() instanceof LinkedHashMap);
}
}