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 @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> attrs = new HashMap<>();
attrs.put("k", 1);
assertEquals(attrs, bean.attrs);

Map<String, String> labels = new HashMap<>();
labels.put("p", "q");
assertEquals(labels, bean.labels);
}

public static class Bean {
public Map<String, Object> attrs;
public Map<String, String> labels;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<Object> items;
public Map<String, Object> attrs;
public Map<String, String> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<Object> items;
public Map<String, Object> attrs;
public Map<String, String> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ public class ContextAutoTypeBeforeHandler
public ContextAutoTypeBeforeHandler(String[] acceptNames) {
super(acceptNames);
}

public ContextAutoTypeBeforeHandler(boolean includeBasic, String[] acceptNames) {
super(includeBasic, acceptNames);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public GenericFastJsonRedisSerializer() {
}

public GenericFastJsonRedisSerializer(String[] acceptNames) {
contextFilter = new ContextAutoTypeBeforeHandler(acceptNames);
contextFilter = new ContextAutoTypeBeforeHandler(true, acceptNames);
}

@Override
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<Object> items;
public Map<String, Object> attrs;
public Map<String, String> 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;
}
}
}
Loading