Skip to content

Commit f0d3435

Browse files
committed
fix NPE when enable unnecessary ReferenceDetection, for issue #3432
1 parent fa3dac1 commit f0d3435

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

core/src/main/java/com/alibaba/fastjson2/JSONWriter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ public final String setPath(int index, Object object) {
224224
}
225225

226226
public final String setPath0(int index, Object object) {
227+
if (path == null) {
228+
return null;
229+
}
227230
this.path = index == 0
228231
? (path.child0 != null ? path.child0 : (path.child0 = new Path(path, index)))
229232
: index == 1
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.alibaba.fastjson2.issues_3400;
2+
3+
import com.alibaba.fastjson2.JSON;
4+
import com.alibaba.fastjson2.JSONWriter;
5+
import lombok.Data;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
public class Issue3432 {
15+
@Test
16+
public void test_toJSON() {
17+
List<Node> nodes = new ArrayList<>();
18+
Node node = new Node();
19+
node.setGradeId("123");
20+
node.setPrivilegeIds(Arrays.asList("1", "2"));
21+
nodes.add(node);
22+
23+
assertEquals(com.alibaba.fastjson.JSON.toJSON(nodes), JSON.toJSON(nodes, JSONWriter.Feature.ReferenceDetection));
24+
}
25+
26+
@Data
27+
public static class Node {
28+
private String gradeId;
29+
private List<String> privilegeIds;
30+
}
31+
}

0 commit comments

Comments
 (0)