In the Java-agent I implemented, after introducing the following dependencies:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
<scope>compile</scope>
</dependency>
It affects the normal serialization of the application, but not all, leading to some scenarios where the serialization result is {"empty":false}
.
To simplify and further pinpoint the issue:
1.First, I commented out all the places in the agent where fastjson was originally used, and retried deploying with and without the agent. The aforementioned problem was reproduced, i.e., without adding this dependency, the business application is normal when the agent is started. Therefore, it can be confirmed that the problem is caused by introducing this dependency in the java-agent.
2.For easy troubleshooting, I built a reproduction case and found that this problem occurs when serializing ArraylistMultimap
objects. Below is a simple test case::
Person person = new Person();
List<Student> students = new ArrayList<Student>(){
{
add(new Student("Tom",11));
add(new Student("Lisa",11));
add(new Student("Tom",12));
}
};
person.setStudents(students);
var itemMap = person.getStudents().stream()
.collect(toMultimap(Student::getName, v -> v, ArrayListMultimap::create));
Map<String, Object> data = new HashMap<>();
data.put("item",itemMap);
String jsonStr = JSON.toJSONString(data);
Here are the declarations of Person and Student:
@Data
@Accessors(chain = true)
public class Person {
public List<Student> students;
@Data
@Accessors(chain = true)
@AllArgsConstructor
public static class Student {
private String name;
private Integer age;
}
}
When the agent introduces the fast-json dependency,
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
<scope>compile</scope>
</dependency>
the value of the above jsonStr is {"item":{"empty":false}}
.
If this dependency is removed, the serialized value is as expected.
{"item":{"Lisa":[{"age":11,"name":"Lisa"}],"Tom":[{"age":11,"name":"Tom"},{"age":12,"name":"Tom"}]}}
The version of fast-json used in the business is also fastjson-1.2.83.
3.I tried to debug and compare the two situations, but when the code enters the ASM part, the breakpoint information is no longer visible. When the breakpoint returns, the above result is obtained.
4.My personal understanding is that it should be related to ClassLoader loading. I have customized the classLoader in the java-agent, but I did not change the action of the thread's context loader, nor did I make global configurations for fastjson.
I would appreciate any suggestions or feedback. Tanks!!!