Skip to content

Commit

Permalink
fix bug where collection being cast to List when readingReferences, f…
Browse files Browse the repository at this point in the history
…or issue #2302
  • Loading branch information
rowstop authored and wenshao committed Mar 9, 2024
1 parent 23b1b9e commit a0be963
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ public int startArray() {
public abstract String readReference();

public boolean readReference(List list, int i) {
return readReference((Collection) list, i);
}

public boolean readReference(Collection list, int i) {
if (!isReference()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@ public String readReference() {
throw new JSONException("reference not support input " + error(type));
}

public boolean readReference(List list, int i) {
public boolean readReference(Collection list, int i) {
if (bytes[offset] != BC_REFERENCE) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void readFieldValue(JSONReader jsonReader, T object) {
break;
}

if (jsonReader.readReference((List) list, i)) {
if (jsonReader.readReference(list, i)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.alibaba.fastjson2.issues_2300;

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

import java.util.HashSet;
import java.util.Set;

/**
* @author 张治保
* @since 2024/3/8
*/
public class Issue2302 {
@Test
void test() {
TestA a = new TestA();
a.areaIds.add(1000);
String jsonStr = JSON.toJSONString(a);
TestA newTest = JSON.parseObject(jsonStr, TestA.class);
Assertions.assertEquals(newTest.areaIds, a.areaIds);
}

private static class TestA {
public Set<Integer> areaIds = new HashSet<>();
}
}

0 comments on commit a0be963

Please sign in to comment.