fix parse error when seeAlsoDefault is specified and `@type` is missing, for issue #3540

This commit is contained in:
yanxutao89 2025-05-09 01:13:28 +08:00
parent 436db2bbc5
commit 7fcc538012
2 changed files with 74 additions and 1 deletions

View File

@ -243,7 +243,7 @@ final class ObjectReaderSeeAlso<T>
JSONReader.Context context = jsonReader.getContext();
long features3, hash = jsonReader.readFieldNameHashCode();
JSONReader.AutoTypeBeforeHandler autoTypeFilter = context.getContextAutoTypeBeforeHandler();
if (hash == getTypeKeyHash()
if ((hash == getTypeKeyHash() || seeAlsoDefault != null)
&& ((((features3 = (features | getFeatures() | context.getFeatures())) & JSONReader.Feature.SupportAutoType.mask) != 0) || autoTypeFilter != null)
) {
ObjectReader reader = null;

View File

@ -0,0 +1,73 @@
package com.alibaba.fastjson2.issues_3500;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONType;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Issue3540 {
@Test
public void exceptUseSeeAlsoDefault() {
final String json = " {\n" +
" \"name\": \"tom\",\n" +
" \"age\": 18\n" +
" }";
IAnimal animal = JSON.parseObject(json, IAnimal.class);
assertEquals(Dog.class, animal.getClass());
}
@Test
public void exceptUseSeeAlsoDefault2() {
final String json = " {\n" +
" \"@type\": \"\",\n" +
" \"name\": \"tom\",\n" +
" \"age\": 18\n" +
" }";
IAnimal animal = JSON.parseObject(json, IAnimal.class);
assertEquals(Dog.class, animal.getClass());
}
@Test
public void useTypeForSeeAlso() {
final String json = " {\n" +
" \"@type\": \"Dog\",\n" +
" \"name\": \"tom\",\n" +
" \"age\": 18\n" +
" }";
IAnimal animal = JSON.parseObject(json, IAnimal.class);
assertEquals(Dog.class, animal.getClass());
}
@JSONType(seeAlso = Dog.class, seeAlsoDefault = Dog.class)
public interface IAnimal {
String getName();
int getAge();
}
@JSONType(typeName = "Dog")
public static class Dog
implements IAnimal {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String getName() {
return null;
}
@Override
public int getAge() {
return 0;
}
}
}