diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplMapString.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplMapString.java index be3bf19312..93d807d88c 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplMapString.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplMapString.java @@ -73,7 +73,8 @@ public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName String value = jsonReader.readString(); if (i == 0 - && (contextFeatures & JSONReader.Feature.SupportAutoType.mask) != 0 + && ((contextFeatures & JSONReader.Feature.SupportAutoType.mask) != 0 + || context.getContextAutoTypeBeforeHandler() != null) && name.equals(getTypeKey())) { continue; } diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplMapTyped.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplMapTyped.java index 1d1e54890b..4859c19367 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplMapTyped.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplMapTyped.java @@ -318,7 +318,8 @@ 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 + && ((contextFeatures & JSONReader.Feature.SupportAutoType.mask) != 0 + || jsonReader.getContext().getContextAutoTypeBeforeHandler() != null) && name.equals(getTypeKey()) ) { if (seenKeys != null && !seenKeys.add(getTypeKey())) { diff --git a/core/src/test/java/com/alibaba/fastjson2/autoType/AutoTypeFilterMapTypeKeyTest.java b/core/src/test/java/com/alibaba/fastjson2/autoType/AutoTypeFilterMapTypeKeyTest.java new file mode 100644 index 0000000000..00a121c234 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/autoType/AutoTypeFilterMapTypeKeyTest.java @@ -0,0 +1,34 @@ +package com.alibaba.fastjson2.autoType; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONReader; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class AutoTypeFilterMapTypeKeyTest { + @Test + public void test() { + String str = "{\"@type\":\"" + Bean.class.getName() + "\"" + + ",\"attrs\":{\"@type\":\"java.util.HashMap\",\"k\":1}" + + ",\"labels\":{\"@type\":\"java.util.HashMap\",\"p\":\"q\"}}"; + + Bean bean = (Bean) JSON.parseObject(str, Object.class, JSONReader.autoTypeFilter(Bean.class.getName())); + + Map attrs = new HashMap<>(); + attrs.put("k", 1); + assertEquals(attrs, bean.attrs); + + Map labels = new HashMap<>(); + labels.put("p", "q"); + assertEquals(labels, bean.labels); + } + + public static class Bean { + public Map attrs; + public Map labels; + } +} diff --git a/extension-spring5/src/main/java/com/alibaba/fastjson2/support/spring/data/redis/GenericFastJsonRedisSerializer.java b/extension-spring5/src/main/java/com/alibaba/fastjson2/support/spring/data/redis/GenericFastJsonRedisSerializer.java index cf9dce9ef2..9d32e7bb8b 100644 --- a/extension-spring5/src/main/java/com/alibaba/fastjson2/support/spring/data/redis/GenericFastJsonRedisSerializer.java +++ b/extension-spring5/src/main/java/com/alibaba/fastjson2/support/spring/data/redis/GenericFastJsonRedisSerializer.java @@ -28,7 +28,12 @@ public GenericFastJsonRedisSerializer() { public GenericFastJsonRedisSerializer(String[] acceptNames, boolean jsonb) { this(); - config.setReaderFilters(new ContextAutoTypeBeforeHandler(acceptNames)); + boolean acceptList = acceptNames.length > 0; + if (acceptList) { + // the accept list is the autoType gate, SupportAutoType would let any other type through + config.setReaderFeatures(); + } + config.setReaderFilters(new ContextAutoTypeBeforeHandler(acceptList, acceptNames)); config.setJSONB(jsonb); } diff --git a/extension-spring5/src/test/java/com/alibaba/fastjson2/spring/GenericFastJsonRedisSerializerAcceptListTest.java b/extension-spring5/src/test/java/com/alibaba/fastjson2/spring/GenericFastJsonRedisSerializerAcceptListTest.java new file mode 100644 index 0000000000..d277b020ea --- /dev/null +++ b/extension-spring5/src/test/java/com/alibaba/fastjson2/spring/GenericFastJsonRedisSerializerAcceptListTest.java @@ -0,0 +1,88 @@ +package com.alibaba.fastjson2.spring; + +import com.alibaba.fastjson2.JSONB; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.support.spring.data.redis.GenericFastJsonRedisSerializer; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.*; + +public class GenericFastJsonRedisSerializerAcceptListTest { + static final String[] ACCEPT_NAMES = {Bean.class.getName()}; + + @Test + public void rejectTypeOutsideAcceptList() { + Gadget.SETTER_CALLS.set(0); + GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(ACCEPT_NAMES); + byte[] bytes = ("{\"@type\":\"" + Gadget.class.getName() + "\",\"value\":\"x\"}") + .getBytes(StandardCharsets.UTF_8); + Object result = serializer.deserialize(bytes); + assertFalse(result instanceof Gadget); + assertEquals(0, Gadget.SETTER_CALLS.get()); + } + + @Test + public void rejectTypeOutsideAcceptListJSONB() { + Gadget.SETTER_CALLS.set(0); + GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(ACCEPT_NAMES, true); + Gadget gadget = new Gadget(); + gadget.value = "x"; + byte[] bytes = JSONB.toBytes(gadget, JSONWriter.Feature.WriteClassName); + Object result = serializer.deserialize(bytes); + assertFalse(result instanceof Gadget); + assertEquals(0, Gadget.SETTER_CALLS.get()); + } + + @Test + public void acceptedTypeRoundTrip() { + for (boolean jsonb : new boolean[]{false, true}) { + GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(ACCEPT_NAMES, jsonb); + Bean bean = new Bean(); + bean.id = 1; + // int, not long: Issue283 registers a global Long writer that renders longs as strings, + // so a long here would round-trip to a String depending on test order + bean.items = java.util.Arrays.asList(1, "a"); + bean.attrs = new HashMap<>(); + bean.attrs.put("k", 2); + bean.labels = new HashMap<>(); + bean.labels.put("p", "q"); + Map root = new HashMap<>(); + root.put("bean", bean); + + Map map = (Map) serializer.deserialize(serializer.serialize(root)); + assertEquals(HashMap.class, map.getClass()); + Bean result = (Bean) map.get("bean"); + assertEquals(1, result.id); + assertEquals(bean.items, result.items); + assertEquals(bean.attrs, result.attrs); + assertEquals(bean.labels, result.labels); + } + } + + public static class Bean { + public int id; + public List items; + public Map attrs; + public Map labels; + } + + public static class Gadget { + static final AtomicInteger SETTER_CALLS = new AtomicInteger(); + private String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + SETTER_CALLS.incrementAndGet(); + this.value = value; + } + } +} diff --git a/extension-spring6/src/main/java/com/alibaba/fastjson2/support/spring6/data/redis/GenericFastJsonRedisSerializer.java b/extension-spring6/src/main/java/com/alibaba/fastjson2/support/spring6/data/redis/GenericFastJsonRedisSerializer.java index 00e15475df..df4158796b 100644 --- a/extension-spring6/src/main/java/com/alibaba/fastjson2/support/spring6/data/redis/GenericFastJsonRedisSerializer.java +++ b/extension-spring6/src/main/java/com/alibaba/fastjson2/support/spring6/data/redis/GenericFastJsonRedisSerializer.java @@ -28,7 +28,12 @@ public GenericFastJsonRedisSerializer() { public GenericFastJsonRedisSerializer(String[] acceptNames, boolean jsonb) { this(); - config.setReaderFilters(new ContextAutoTypeBeforeHandler(acceptNames)); + boolean acceptList = acceptNames.length > 0; + if (acceptList) { + // the accept list is the autoType gate, SupportAutoType would let any other type through + config.setReaderFeatures(); + } + config.setReaderFilters(new ContextAutoTypeBeforeHandler(acceptList, acceptNames)); config.setJSONB(jsonb); } diff --git a/extension-spring6/src/test/java/com/alibaba/fastjson2/support/spring6/GenericFastJsonRedisSerializerAcceptListTest.java b/extension-spring6/src/test/java/com/alibaba/fastjson2/support/spring6/GenericFastJsonRedisSerializerAcceptListTest.java new file mode 100644 index 0000000000..b060123e8c --- /dev/null +++ b/extension-spring6/src/test/java/com/alibaba/fastjson2/support/spring6/GenericFastJsonRedisSerializerAcceptListTest.java @@ -0,0 +1,86 @@ +package com.alibaba.fastjson2.support.spring6; + +import com.alibaba.fastjson2.JSONB; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.support.spring6.data.redis.GenericFastJsonRedisSerializer; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.*; + +public class GenericFastJsonRedisSerializerAcceptListTest { + static final String[] ACCEPT_NAMES = {Bean.class.getName()}; + + @Test + public void rejectTypeOutsideAcceptList() { + Gadget.SETTER_CALLS.set(0); + GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(ACCEPT_NAMES); + byte[] bytes = ("{\"@type\":\"" + Gadget.class.getName() + "\",\"value\":\"x\"}") + .getBytes(StandardCharsets.UTF_8); + Object result = serializer.deserialize(bytes); + assertFalse(result instanceof Gadget); + assertEquals(0, Gadget.SETTER_CALLS.get()); + } + + @Test + public void rejectTypeOutsideAcceptListJSONB() { + Gadget.SETTER_CALLS.set(0); + GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(ACCEPT_NAMES, true); + Gadget gadget = new Gadget(); + gadget.value = "x"; + byte[] bytes = JSONB.toBytes(gadget, JSONWriter.Feature.WriteClassName); + Object result = serializer.deserialize(bytes); + assertFalse(result instanceof Gadget); + assertEquals(0, Gadget.SETTER_CALLS.get()); + } + + @Test + public void acceptedTypeRoundTrip() { + for (boolean jsonb : new boolean[]{false, true}) { + GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(ACCEPT_NAMES, jsonb); + Bean bean = new Bean(); + bean.id = 1; + bean.items = java.util.Arrays.asList(1, "a"); + bean.attrs = new HashMap<>(); + bean.attrs.put("k", 2); + bean.labels = new HashMap<>(); + bean.labels.put("p", "q"); + Map root = new HashMap<>(); + root.put("bean", bean); + + Map map = (Map) serializer.deserialize(serializer.serialize(root)); + assertEquals(HashMap.class, map.getClass()); + Bean result = (Bean) map.get("bean"); + assertEquals(1, result.id); + assertEquals(bean.items, result.items); + assertEquals(bean.attrs, result.attrs); + assertEquals(bean.labels, result.labels); + } + } + + public static class Bean { + public int id; + public List items; + public Map attrs; + public Map labels; + } + + public static class Gadget { + static final AtomicInteger SETTER_CALLS = new AtomicInteger(); + private String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + SETTER_CALLS.incrementAndGet(); + this.value = value; + } + } +} diff --git a/fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/ContextAutoTypeBeforeHandler.java b/fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/ContextAutoTypeBeforeHandler.java index 31ccac57e4..758c77de7e 100644 --- a/fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/ContextAutoTypeBeforeHandler.java +++ b/fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/ContextAutoTypeBeforeHandler.java @@ -6,4 +6,8 @@ public class ContextAutoTypeBeforeHandler public ContextAutoTypeBeforeHandler(String[] acceptNames) { super(acceptNames); } + + public ContextAutoTypeBeforeHandler(boolean includeBasic, String[] acceptNames) { + super(includeBasic, acceptNames); + } } diff --git a/fastjson1-compatible/src/main/java/com/alibaba/fastjson/support/spring/GenericFastJsonRedisSerializer.java b/fastjson1-compatible/src/main/java/com/alibaba/fastjson/support/spring/GenericFastJsonRedisSerializer.java index c4356ef4a6..315d7c74ce 100644 --- a/fastjson1-compatible/src/main/java/com/alibaba/fastjson/support/spring/GenericFastJsonRedisSerializer.java +++ b/fastjson1-compatible/src/main/java/com/alibaba/fastjson/support/spring/GenericFastJsonRedisSerializer.java @@ -26,7 +26,7 @@ public GenericFastJsonRedisSerializer() { } public GenericFastJsonRedisSerializer(String[] acceptNames) { - contextFilter = new ContextAutoTypeBeforeHandler(acceptNames); + contextFilter = new ContextAutoTypeBeforeHandler(true, acceptNames); } @Override @@ -47,7 +47,11 @@ public Object deserialize(byte[] bytes) throws SerializationException { return null; } try { - return JSON.parseObject(bytes, Object.class, contextFilter, Feature.SupportAutoType); + if (contextFilter != null) { + // the accept list is the autoType gate, SupportAutoType would let any other type through + return JSON.parseObject(bytes, Object.class, contextFilter); + } + return JSON.parseObject(bytes, Object.class, Feature.SupportAutoType); } catch (Exception ex) { throw new SerializationException("Could not deserialize: " + ex.getMessage(), ex); } diff --git a/fastjson1-compatible/src/test/java/com/alibaba/fastjson/support/spring/GenericFastJsonRedisSerializerAcceptListTest.java b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/support/spring/GenericFastJsonRedisSerializerAcceptListTest.java new file mode 100644 index 0000000000..208e40123c --- /dev/null +++ b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/support/spring/GenericFastJsonRedisSerializerAcceptListTest.java @@ -0,0 +1,68 @@ +package com.alibaba.fastjson.support.spring; + +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.*; + +public class GenericFastJsonRedisSerializerAcceptListTest { + static final String[] ACCEPT_NAMES = {Bean.class.getName()}; + + @Test + public void rejectTypeOutsideAcceptList() { + Gadget.SETTER_CALLS.set(0); + GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(ACCEPT_NAMES); + byte[] bytes = ("{\"@type\":\"" + Gadget.class.getName() + "\",\"value\":\"x\"}") + .getBytes(StandardCharsets.UTF_8); + Object result = serializer.deserialize(bytes); + assertFalse(result instanceof Gadget); + assertEquals(0, Gadget.SETTER_CALLS.get()); + } + + @Test + public void acceptedTypeRoundTrip() { + GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(ACCEPT_NAMES); + Bean bean = new Bean(); + bean.id = 1; + bean.items = java.util.Arrays.asList(1, "a"); + bean.attrs = new HashMap<>(); + bean.attrs.put("k", 2); + bean.labels = new HashMap<>(); + bean.labels.put("p", "q"); + Map root = new HashMap<>(); + root.put("bean", bean); + + Map map = (Map) serializer.deserialize(serializer.serialize(root)); + Bean result = (Bean) map.get("bean"); + assertEquals(1, result.id); + assertEquals(bean.items, result.items); + assertEquals(bean.attrs, result.attrs); + assertEquals(bean.labels, result.labels); + } + + public static class Bean { + public int id; + public List items; + public Map attrs; + public Map labels; + } + + public static class Gadget { + static final AtomicInteger SETTER_CALLS = new AtomicInteger(); + private String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + SETTER_CALLS.incrementAndGet(); + this.value = value; + } + } +}