Skip to content

Commit

Permalink
TypeReference support null argument, for issue #1267
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Mar 22, 2023
1 parent 1dfcaa6 commit 6cc5039
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/TypeReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public TypeReference(Type... actualTypeArguments) {
throw new NullPointerException();
}

if (actualTypeArguments.length == 1 && actualTypeArguments[0] == null) {
actualTypeArguments[0] = Object.class;
}

Class<?> thisClass = getClass();
Type superClass = thisClass.getGenericSuperclass();
ParameterizedType argType = (ParameterizedType) ((ParameterizedType) superClass).getActualTypeArguments()[0];
Expand Down
41 changes: 41 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/TypeReferenceTest4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.alibaba.fastjson2;

import lombok.Data;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TypeReferenceTest4 {
@Test
public void test() {
ExtInfoBean extInfoBean = new ExtInfoBean();
extInfoBean.setInfoId("id");
extInfoBean.setInfoName(ExtInfoBean.class.getName());
extInfoBean.setInfoData(null);

String json = JSON.toJSONString(extInfoBean);
assertEquals("{\"infoId\":\"id\",\"infoName\":\"com.alibaba.fastjson2.TypeReferenceTest4$ExtInfoBean\"}", json);

ExtInfoBean result = testTypeReference(json, null);
assertEquals(json, JSON.toJSONString(result));
}

private static <T> ExtInfoBean<T> testTypeReference(String extJson, Class<T> clz) {
ExtInfoBean extInfoBean = JSON.parseObject(extJson,
new TypeReference<ExtInfoBean<T>>(clz) {
});

return extInfoBean;
}

/**
* @author weizhiyu
* @since 2022/07/06 2:35 PM
*/
@Data
public static class ExtInfoBean<T> {
private String infoId;
private String infoName;
private T infoData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ protected TypeReference() {
* @since 1.2.9
*/
protected TypeReference(Type... actualTypeArguments) {
if (actualTypeArguments != null
&& actualTypeArguments.length == 1
&& actualTypeArguments[0] == null) {
actualTypeArguments[0] = Object.class;
}

Class<?> thisClass = this.getClass();
Type superClass = thisClass.getGenericSuperclass();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.alibaba.fastjson;

import lombok.Data;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TypeReferenceTest4 {
@Test
public void test() {
ExtInfoBean extInfoBean = new ExtInfoBean();
extInfoBean.setInfoId("id");
extInfoBean.setInfoName(ExtInfoBean.class.getName());
extInfoBean.setInfoData(null);

String json = com.alibaba.fastjson2.JSON.toJSONString(extInfoBean);
assertEquals("{\"infoId\":\"id\",\"infoName\":\"com.alibaba.fastjson.TypeReferenceTest4$ExtInfoBean\"}", json);

ExtInfoBean result = testTypeReference(json, null);
assertEquals(json, com.alibaba.fastjson2.JSON.toJSONString(result));
}

private static <T> ExtInfoBean<T> testTypeReference(String extJson, Class<T> clz) {
ExtInfoBean extInfoBean = JSON.parseObject(extJson,
new TypeReference<ExtInfoBean<T>>(clz) {
});

return extInfoBean;
}

/**
* @author weizhiyu
* @since 2022/07/06 2:35 PM
*/
@Data
public static class ExtInfoBean<T> {
private String infoId;
private String infoName;
private T infoData;
}
}

0 comments on commit 6cc5039

Please sign in to comment.