Skip to content

Commit 656866a

Browse files
committed
bug fix for seeAlso, for issue #3357
1 parent ef8c52f commit 656866a

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderSeeAlso.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public T readObject(JSONReader jsonReader, Type fieldType, Object fieldName, lon
215215
}
216216
}
217217

218-
if (i != 0) {
218+
if (i != 0 || fieldReader != null) {
219219
jsonReader.reset(savePoint);
220220
}
221221

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.alibaba.fastjson2.issues_3300;
2+
3+
import com.alibaba.fastjson2.JSON;
4+
import com.alibaba.fastjson2.annotation.JSONType;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class Issue3357 {
10+
@Test
11+
public void cat() {
12+
String s1 = "{\"age\": 20, \"type\": \"cat\"}"; // type comes second
13+
Cat c1 = (Cat) JSON.parseObject(s1, Animal.class);
14+
assertEquals("cat", c1.type); // succeeds
15+
16+
String s2 = "{\"type\": \"cat\", \"age\": 20}"; // type comes first
17+
Cat c2 = (Cat) JSON.parseObject(s2, Animal.class);
18+
assertEquals("cat", c2.type); // fails
19+
}
20+
21+
@JSONType(typeKey = "type", seeAlso = Cat.class)
22+
interface Animal {
23+
}
24+
25+
@JSONType(typeName = "cat")
26+
static class Cat {
27+
private final String type;
28+
private final int age;
29+
30+
public Cat(String type, int age) {
31+
this.type = type;
32+
this.age = age;
33+
}
34+
35+
public String getType() {
36+
return type;
37+
}
38+
39+
public int getAge() {
40+
return age;
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)