-
Notifications
You must be signed in to change notification settings - Fork 547
Closed
Labels
Milestone
Description
Description:
Codegen incorrectly serializes fields and produces an invalid json because it calls writeName...Raw methods without checking if it's possible to write the value for that field. If field is null, value won't be written, but key remains already written, hence the resulting json won't have any value at all for that field (for example ... "format":, ...).
Generated code looks like this:
...
jsonWriter.writeName6Raw(nameDirect ? 2482716417287218722L : 2843004387476858407L);
String string2 = ((MyDto)object).format;
if (string2 != null) {
jsonWriter.writeString(string2);
} else if ((contextFeatures & 8388688L) != 0L) {
if ((contextFeatures & 8388672L) != 0L) {
jsonWriter.writeString("");
} else {
jsonWriter.writeStringNull();
}
}
...
but should be something like:
...
String string2 = ((MyDto)object).format;
if (string2 != null) {
jsonWriter.writeName6Raw(nameDirect ? 2482716417287218722L : 2843004387476858407L);
jsonWriter.writeString(string2);
} else if ((contextFeatures & 8388688L) != 0L) {
jsonWriter.writeName6Raw(nameDirect ? 2482716417287218722L : 2843004387476858407L);
if ((contextFeatures & 8388672L) != 0L) {
jsonWriter.writeString("");
} else {
jsonWriter.writeStringNull();
}
}
...
Environment:
- OS: Linux
- JDK: Openjdk-21.0.5+11
- Fastjson2 and codegen: 2.0.59
- Quarkus: 3.28.4
Reproduction:
- Create a field in your DTO, for example
String format:
@JSONCompiled
@JSONType
public class MyDto {
...
public String format;
...- Serialize your dto to string.
Actual
Your json will have ... "name":, ... so no value written at all, invalid json.
Expected
Either of these:
... "format": null, ...... "format": "something", ...- or no "format" field at all