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
16 changes: 16 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ public final String setPath(String name, Object object) {

this.path = new Path(this.path, name);

if (context.provider.isDisableListReferenceDetect(object.getClass())) {
return null;
}

Path previous;
if (object == rootObject) {
previous = Path.ROOT;
Expand Down Expand Up @@ -274,6 +278,10 @@ public final String setPath(FieldWriter fieldWriter, Object object) {
? fieldWriter.getRootParentPath()
: fieldWriter.getPath(path);

if (context.provider.isDisableListReferenceDetect(object.getClass())) {
return null;
}

Path previous;
if (object == rootObject) {
previous = Path.ROOT;
Expand Down Expand Up @@ -313,6 +321,10 @@ public final String setPath0(FieldWriter fieldWriter, Object object) {
? fieldWriter.getRootParentPath()
: fieldWriter.getPath(path);

if (context.provider.isDisableListReferenceDetect(object.getClass())) {
return null;
}

Path previous;
if (object == rootObject) {
previous = Path.ROOT;
Expand Down Expand Up @@ -400,6 +412,10 @@ public final String setPath0(int index, Object object) {
? (path.child1 != null ? path.child1 : (path.child1 = new Path(path, index)))
: new Path(path, index);

if (context.provider.isDisableListReferenceDetect(object.getClass())) {
return null;
}

Path previous;
if (object == rootObject) {
previous = Path.ROOT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class ObjectWriterProvider
PropertyNamingStrategy namingStrategy;

boolean disableReferenceDetect = JSONFactory.isDisableReferenceDetect();
boolean disableListReferenceDetect;
boolean disableArrayMapping = JSONFactory.isDisableArrayMapping();
boolean disableJSONB = JSONFactory.isDisableJSONB();
boolean disableAutoType = JSONFactory.isDisableAutoType();
Expand Down Expand Up @@ -1028,6 +1029,17 @@ public static boolean isNotReferenceDetect(final Class<?> clazz) {
|| ((clazz.getModifiers() & ENUM) != 0 && clazz.getSuperclass() == Enum.class);
}

/**
* Checks whether reference detection is disabled for the specified {@link java.util.Collection}
* type under this provider's configuration.
*
* @param clazz the class to check
* @return true if collection reference detection is disabled for the class, false otherwise
*/
public boolean isDisableListReferenceDetect(Class<?> clazz) {
return disableListReferenceDetect && Collection.class.isAssignableFrom(clazz);
}

/**
* Clears all cached ObjectWriters and mixin mappings.
*
Expand Down Expand Up @@ -1144,6 +1156,15 @@ public boolean isDisableReferenceDetect() {
return disableReferenceDetect;
}

/**
* Checks whether reference detection is disabled for {@link java.util.Collection} values.
*
* @return true if collection reference detection is disabled, false otherwise
*/
public boolean isDisableListReferenceDetect() {
return disableListReferenceDetect;
}

/**
* Checks if auto-type support is disabled.
*
Expand Down Expand Up @@ -1180,6 +1201,16 @@ public void setDisableReferenceDetect(boolean disableReferenceDetect) {
this.disableReferenceDetect = disableReferenceDetect;
}

/**
* Sets whether reference detection is disabled for {@link java.util.Collection} values.
* The default is {@code false}.
*
* @param disableListReferenceDetect true to disable collection reference detection, false to enable it
*/
public void setDisableListReferenceDetect(boolean disableListReferenceDetect) {
this.disableListReferenceDetect = disableListReferenceDetect;
}

/**
* Sets whether array mapping is disabled.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.alibaba.fastjson2.writer;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONWriter;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ObjectWriterProviderDisableListReferenceDetectTest {
@Test
public void testIsDisableListReferenceDetect() {
ObjectWriterProvider provider = new ObjectWriterProvider();
assertFalse(provider.isDisableListReferenceDetect());
assertFalse(provider.isDisableListReferenceDetect(ArrayList.class));
assertFalse(provider.isDisableListReferenceDetect(Bean.class));

provider.setDisableListReferenceDetect(true);
assertTrue(provider.isDisableListReferenceDetect());
assertTrue(provider.isDisableListReferenceDetect(ArrayList.class));
assertTrue(provider.isDisableListReferenceDetect(List.class));
// non-collection types are not affected by the list switch
assertFalse(provider.isDisableListReferenceDetect(Bean.class));
}

@Test
public void testDefaultDisabled() {
ObjectWriterProvider provider = new ObjectWriterProvider();
JSONWriter.Context context = new JSONWriter.Context(provider, JSONWriter.Feature.ReferenceDetection);

Bean bean = beanWithSharedList();
String json = JSON.toJSONString(bean, context);
assertTrue(json.contains("$ref"), json);
assertTrue(json.contains("$.items"), json);
}

@Test
public void testEnabled() {
ObjectWriterProvider provider = new ObjectWriterProvider();
provider.setDisableListReferenceDetect(true);
JSONWriter.Context context = new JSONWriter.Context(provider, JSONWriter.Feature.ReferenceDetection);

Bean bean = beanWithSharedList();
String json = JSON.toJSONString(bean, context);
assertFalse(json.contains("$ref"), json);
assertEquals("{\"items\":[\"a\",\"b\"],\"items2\":[\"a\",\"b\"]}", json);

Bean parsed = JSON.parseObject(json, Bean.class);
assertNotSame(parsed.items, parsed.items2);
assertEquals(Arrays.asList("a", "b"), parsed.items);
assertEquals(Arrays.asList("a", "b"), parsed.items2);
}

@Test
public void testDefaultDisabledJSONB() {
ObjectWriterProvider provider = new ObjectWriterProvider();
JSONWriter.Context context = new JSONWriter.Context(provider, JSONWriter.Feature.ReferenceDetection);

byte[] bytes = JSONB.toBytes(beanWithSharedList(), context);
assertTrue(JSONB.toJSONString(bytes).contains("$ref"));
}

@Test
public void testEnabledJSONB() {
ObjectWriterProvider provider = new ObjectWriterProvider();
provider.setDisableListReferenceDetect(true);
JSONWriter.Context context = new JSONWriter.Context(provider, JSONWriter.Feature.ReferenceDetection);

byte[] bytes = JSONB.toBytes(beanWithSharedList(), context);
assertFalse(JSONB.toJSONString(bytes).contains("$ref"));
}

@Test
public void testNonCollectionReferenceDetectionStillEnabled() {
ObjectWriterProvider provider = new ObjectWriterProvider();
provider.setDisableListReferenceDetect(true);
JSONWriter.Context context = new JSONWriter.Context(provider, JSONWriter.Feature.ReferenceDetection);

Item shared = new Item();
shared.id = 101;

Holder holder = new Holder();
holder.first = shared;
holder.second = shared;

String json = JSON.toJSONString(holder, context);
assertTrue(json.contains("$ref"), json);
}

@Test
public void testDefaultKeepsCollectionCircularReferenceProtection() {
ObjectWriterProvider provider = new ObjectWriterProvider();
JSONWriter.Context context = new JSONWriter.Context(provider, JSONWriter.Feature.ReferenceDetection);

List<Object> self = new ArrayList<>();
self.add(self);

String json = JSON.toJSONString(self, context);
assertTrue(json.contains("$ref"), json);
}

private static Bean beanWithSharedList() {
List<String> shared = new ArrayList<>(Arrays.asList("a", "b"));

Bean bean = new Bean();
bean.items = shared;
bean.items2 = shared;
return bean;
}

public static class Bean {
public List<String> items;
public List<String> items2;
}

public static class Item {
public int id;
}

public static class Holder {
public Item first;
public Item second;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,7 @@ public static JSONWriter.Context createWriteContext(

ObjectWriterProvider provider = config.getProvider();
provider.setCompatibleWithFieldName(TypeUtils.compatibleWithFieldName);
provider.setDisableListReferenceDetect(true);

JSONWriter.Context context = new JSONWriter.Context(provider);

Expand Down