Skip to content

Commit

Permalink
fix not root class name when specified in @jsontype, for issue #2662
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 authored and wenshao committed Jun 21, 2024
1 parent cbb8b5f commit 810b240
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ private static void isWriteTypeInfo(
int FEILD_FEATURE,
Label notWriteType
) {
if ((objectFeatures & JSONWriter.Feature.WriteClassName.mask) == 0) {
if ((objectFeatures & JSONWriter.Feature.WriteClassName.mask) == 0 || (objectFeatures & NotWriteRootClassName.mask) != 0) {
mw.visitVarInsn(Opcodes.ALOAD, OBJECT);
mw.visitJumpInsn(Opcodes.IFNULL, notWriteType);

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

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONType;
import org.junit.jupiter.api.Test;

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

public class Issue2662 {
@Test
public void test() {
final WrapperClassA wrapperClassA = new WrapperClassA();
final PojoClassC pojoClassC = new PojoClassC();
pojoClassC.setInt1(1);
pojoClassC.setStr2("str");
wrapperClassA.setPojo(pojoClassC);
final String jsonStr = JSON.toJSONString(wrapperClassA);
assertEquals("{\"pojo\":{\"@type\":\"com.alibaba.fastjson2.issues_2600.Issue2662$PojoClassC\",\"int1\":1,\"str2\":\"str\"}}", jsonStr);
}

@JSONType(serializeFeatures = {JSONWriter.Feature.WriteClassName, JSONWriter.Feature.NotWriteRootClassName})
public static class WrapperClassA {
private AbsPojoClassB pojo;

public AbsPojoClassB getPojo() {
return pojo;
}

public void setPojo(AbsPojoClassB pojo) {
this.pojo = pojo;
}
}

public static class AbsPojoClassB {
private Integer int1;
private String str2;

public Integer getInt1() {
return int1;
}

public void setInt1(Integer int1) {
this.int1 = int1;
}

public String getStr2() {
return str2;
}

public void setStr2(String str2) {
this.str2 = str2;
}
}

public static class PojoClassC
extends AbsPojoClassB {
}
}

0 comments on commit 810b240

Please sign in to comment.