Repository navigation
fix: honor field-level serializeFeatures in the ASM object writer #7889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -591,7 +591,7 @@ public void writeEnum(JSONWriter jsonWriter, Enum e) { | |
|
|
||
| public void writeBinary(JSONWriter jsonWriter, byte[] value) { | ||
| if (value == null) { | ||
| if (!jsonWriter.isWriteNulls()) { | ||
| if ((jsonWriter.getFeatures(this.features) & WriteNulls.mask) == 0) { | ||
| return; | ||
| } | ||
| writeFieldName(jsonWriter); | ||
|
|
@@ -633,7 +633,7 @@ public void writeBinary(JSONWriter jsonWriter, byte[] value) { | |
| } | ||
|
|
||
| public void writeInt16(JSONWriter jsonWriter, short[] value) { | ||
| if (value == null && !jsonWriter.isWriteNulls()) { | ||
| if (value == null && (jsonWriter.getFeatures(this.features) & WriteNulls.mask) == 0) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -683,7 +683,7 @@ public void writeString(JSONWriter jsonWriter, String value) { | |
| } | ||
|
|
||
| public void writeString(JSONWriter jsonWriter, char[] value) { | ||
| if (value == null && !jsonWriter.isWriteNulls()) { | ||
| if (value == null && (jsonWriter.getFeatures(this.features) & WriteNulls.mask) == 0) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -728,7 +728,7 @@ public void writeBool(JSONWriter jsonWriter, boolean value) { | |
| } | ||
|
|
||
| public void writeBool(JSONWriter jsonWriter, boolean[] value) { | ||
| if (value == null && !jsonWriter.isWriteNulls()) { | ||
| if (value == null && (jsonWriter.getFeatures(this.features) & WriteNulls.mask) == 0) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -741,7 +741,7 @@ public void writeBool(JSONWriter jsonWriter, boolean[] value) { | |
| } | ||
|
|
||
| public void writeFloat(JSONWriter jsonWriter, float[] value) { | ||
| if (value == null && !jsonWriter.isWriteNulls()) { | ||
| if (value == null && (jsonWriter.getFeatures(this.features) & WriteNulls.mask) == 0) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -754,7 +754,7 @@ public void writeFloat(JSONWriter jsonWriter, float[] value) { | |
| } | ||
|
|
||
| public void writeDouble(JSONWriter jsonWriter, double[] value) { | ||
| if (value == null && !jsonWriter.isWriteNulls()) { | ||
| if (value == null && (jsonWriter.getFeatures(this.features) & WriteNulls.mask) == 0) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] R1-6: field-level A bean with field-level Witness (probe on PR head): Suggested fix: pick one policy for field-level Acceptance criterion: an 中文说明字段级 建议:为「字段级 — glm-5.3-flash via Qwen Code /review (v0.25.1)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified empirically rather than by policy: under a context So this diff did not introduce a new convention; it moved ASM onto reflect's existing one, and the two now agree on both sides of the split. Changing the String/boxed policy would change the reflect creator's output for existing users — a semantic decision beyond #7773. I pinned the current parity instead ( 中文说明用实测而非策略选择来回应:在 context 也就是说本 PR 没有引入新约定,只是把 ASM 搬到 reflect 既有约定上,现在分裂的两侧都一致。改 String/装箱策略会改变 reflect creator 对现有用户的输出——那是超出 #7773 的语义决策。我选择钉住当前 parity( |
||
| return; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2552,8 +2552,10 @@ private void gwFieldValueEnum( | |
| mw.visitLabel(null_); | ||
|
|
||
| // if (!jw.isWriteNulls()) | ||
| mw.iload(mwc.var(WRITE_NULLS)); | ||
| mw.ifeq(notNull_); | ||
| if ((fieldWriter.features & (WriteNulls.mask | NullAsDefaultValue.mask)) == 0) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Critical] R1-1: [certifies-falsely] [regression] Field-level The three new guards union
Failure scenario (default creator, no context features) — a bean with
Same annotated bean, creator-dependent JSON — exactly the silent-divergence class issue #7773 reports, and this PR's own test javadoc says "the two creators must agree". The new Witness (probe on PR head vs merge base, both arms compiled): Suggested fix — align all sites on one field-level policy:
The fix must not violate: unannotated fields keep current output ( Acceptance criterion: an 中文说明新增的生成期守卫把字段级
实测(PR 头 vs merge base 双臂编译):ASM 建议:统一字段级策略——enum 侧对齐(拓宽 — glm-5.3-flash via Qwen Code /review (v0.25.1)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in The enum gate is narrowed back to Measured on the new head (both creators, no context features):
中文说明已在
|
||
| mw.iload(mwc.var(WRITE_NULLS)); | ||
| mw.ifeq(notNull_); | ||
| } | ||
|
|
||
| // writeFieldName(w); | ||
| gwFieldName(mwc, fieldWriter, i); | ||
|
|
@@ -3978,10 +3980,12 @@ private void gwFieldValueInt64VA( | |
|
|
||
| mw.ifnonnull(notNull_); | ||
|
|
||
| mw.iload(mwc.var(WRITE_NULLS)); | ||
| mw.ifne(writeNullValue_); | ||
| if ((fieldWriter.features & (WriteNulls.mask | NullAsDefaultValue.mask)) == 0) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] R1-4: the same-file sibling Consequence: one bean with Witness (probe on PR head): Suggested fix: widen Acceptance criterion: an 中文说明同文件的兄弟生成点 建议:把 — glm-5.3-flash via Qwen Code /review (v0.25.1)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The On If you want the Date split itself closed (Date honoring 中文说明
如果你希望连 Date 的分歧一并收掉(两侧都让 Date 遵循 |
||
| mw.iload(mwc.var(WRITE_NULLS)); | ||
| mw.ifne(writeNullValue_); | ||
|
|
||
| mw.goto_(endIfNull_); | ||
| mw.goto_(endIfNull_); | ||
| } | ||
|
|
||
| mw.visitLabel(writeNullValue_); | ||
|
|
||
|
|
@@ -4029,8 +4033,10 @@ private void gwFieldValueInt64V( | |
| mw.ifne(notDefaultValue_); | ||
|
|
||
| if (fieldWriter.defaultValue == null) { | ||
| mw.iload(WRITE_DEFAULT_VALUE); | ||
| mw.ifeq(notDefaultValue_); | ||
| if ((fieldWriter.features & NotWriteDefaultValue.mask) == 0) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] R1-2: the parity fix is applied only to the text-JSON generators — the JSONB twin still consults context-only runtime vars, so field-level
Witness (probe on PR head): Suggested fix: apply the same generation-time guards inside Acceptance criterion: an 中文说明parity 修复只应用到文本 JSON 生成器:JSONB 生成器 建议:在 — glm-5.3-flash via Qwen Code /review (v0.25.1)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in
Measured:
中文说明已在 实测见上(Defaults 在文本与 JSONB 下两侧一致;ForcedNulls 在 JSONB 下十个字段都以 null 写出)。 |
||
| mw.iload(WRITE_DEFAULT_VALUE); | ||
| mw.ifeq(notDefaultValue_); | ||
| } | ||
| mw.goto_(endWriteValue_); | ||
| } | ||
| mw.visitLabel(notDefaultValue_); | ||
|
|
@@ -4081,10 +4087,12 @@ void gwFieldValueIntVA( | |
|
|
||
| mw.ifnonnull(notNull_); | ||
|
|
||
| mw.iload(mwc.var(WRITE_NULLS)); | ||
| mw.ifne(writeNullValue_); | ||
| if ((fieldWriter.features & (WriteNulls.mask | NullAsDefaultValue.mask)) == 0) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] R1-5: the "should this field's null be written" decision is hand-rolled at 14+ sites across the generator and Sweep at PR head (verification): new sites 2555/3983/4090 ( Concrete cost: every future "feature X ignored for field type Y" report is a fresh per-site edit re-deciding the mask, and reviewers must diff six conventions to check one behavior. Suggested fix: extract one generator helper (e.g. Acceptance criterion: the R1-1 agreement test stays green through the extraction, 中文说明「该字段的 null 要不要写」这一决策在生成器与 建议:抽取一个生成期 helper(如 — glm-5.3-flash via Qwen Code /review (v0.25.1)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Partially addressed, and I want to be explicit about the part I did not do. Done: the array-null family now has exactly one spelling — Not done: the full 中文说明部分落实,未落实的部分说明清楚。已做:数组判空家族现在只有一种拼法—— 未做:跨 14+ 处抽取 |
||
| mw.iload(mwc.var(WRITE_NULLS)); | ||
| mw.ifne(writeNullValue_); | ||
|
|
||
| mw.goto_(endIfNull_); | ||
| mw.goto_(endIfNull_); | ||
| } | ||
|
|
||
| mw.visitLabel(writeNullValue_); | ||
|
|
||
|
|
@@ -4132,8 +4140,10 @@ private void gwFieldValueInt32V( | |
| mw.ifne(notDefaultValue_); | ||
|
|
||
| if (fieldWriter.defaultValue == null) { | ||
| mw.iload(WRITE_DEFAULT_VALUE); | ||
| mw.ifeq(notDefaultValue_); | ||
| if ((fieldWriter.features & NotWriteDefaultValue.mask) == 0) { | ||
| mw.iload(WRITE_DEFAULT_VALUE); | ||
| mw.ifeq(notDefaultValue_); | ||
| } | ||
| mw.goto_(endWriteValue_); | ||
| } | ||
| mw.visitLabel(notDefaultValue_); | ||
|
|
@@ -4165,8 +4175,10 @@ private void gwFieldValueBooleanV( | |
| mw.ifne(notDefaultValue_); | ||
|
|
||
| if (fieldWriter.defaultValue == null) { | ||
| mw.iload(WRITE_DEFAULT_VALUE); | ||
| mw.ifeq(notDefaultValue_); | ||
| if ((fieldWriter.features & NotWriteDefaultValue.mask) == 0) { | ||
| mw.iload(WRITE_DEFAULT_VALUE); | ||
| mw.ifeq(notDefaultValue_); | ||
| } | ||
| mw.goto_(endWriteValue_); | ||
| } | ||
| mw.visitLabel(notDefaultValue_); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| package com.alibaba.fastjson2.issues_7000; | ||
|
|
||
| import com.alibaba.fastjson2.JSON; | ||
| import com.alibaba.fastjson2.JSONObject; | ||
| import com.alibaba.fastjson2.JSONWriter; | ||
| import com.alibaba.fastjson2.annotation.JSONField; | ||
| import com.alibaba.fastjson2.writer.ObjectWriter; | ||
| import com.alibaba.fastjson2.writer.ObjectWriterCreator; | ||
| import com.alibaba.fastjson2.writer.ObjectWriterCreatorASM; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * A field level {@code @JSONField(serializeFeatures = ...)} must be honoured by the ASM generated | ||
| * ObjectWriter exactly as it is by the reflect one. Before the fix the ASM generator only looked at | ||
| * the writer context, so: | ||
| * <ul> | ||
| * <li>{@code WriteNulls} was ignored for primitive arrays and for enum fields</li> | ||
| * <li>{@code NotWriteDefaultValue} was ignored for primitive value fields</li> | ||
| * </ul> | ||
| * Both are compile time constants of the generated writer, so the two creators must agree. | ||
| */ | ||
| public class Issue7773 { | ||
| public enum MyEnum { | ||
| A, B | ||
| } | ||
|
|
||
| public static class ForcedNulls { | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) | ||
| public int[] intArr; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) | ||
| public long[] longArr; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) | ||
| public boolean[] boolArr; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) | ||
| public short[] shortArr; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) | ||
| public byte[] byteArr; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) | ||
| public char[] charArr; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) | ||
| public double[] doubleArr; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) | ||
| public float[] floatArr; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) | ||
| public MyEnum enumField; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) | ||
| public String strField; | ||
| } | ||
|
|
||
| public static class PlainNulls { | ||
| public int[] intArr; | ||
| public long[] longArr; | ||
| public boolean[] boolArr; | ||
| public short[] shortArr; | ||
| public byte[] byteArr; | ||
| public char[] charArr; | ||
| public double[] doubleArr; | ||
| public float[] floatArr; | ||
| public MyEnum enumField; | ||
| public String strField; | ||
| } | ||
|
|
||
| public static class Defaults { | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.NotWriteDefaultValue) | ||
| public short duration; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.NotWriteDefaultValue) | ||
| public byte b; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.NotWriteDefaultValue) | ||
| public char c; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.NotWriteDefaultValue) | ||
| public int count; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.NotWriteDefaultValue) | ||
| public long size; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.NotWriteDefaultValue) | ||
| public boolean flag; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.NotWriteDefaultValue) | ||
| public float f; | ||
| @JSONField(serializeFeatures = JSONWriter.Feature.NotWriteDefaultValue) | ||
| public double d; | ||
| public int control = 7; | ||
| } | ||
|
|
||
| private static final String[] NULL_FIELDS = { | ||
| "intArr", "longArr", "boolArr", "shortArr", "byteArr", | ||
| "charArr", "doubleArr", "floatArr", "enumField", "strField" | ||
| }; | ||
|
|
||
| private static final String[] DEFAULT_FIELDS = { | ||
| "duration", "b", "c", "count", "size", "flag", "f", "d" | ||
| }; | ||
|
|
||
| @Test | ||
| public void fieldLevelWriteNullsOnPrimitiveArraysAndEnum() { | ||
| JSONObject object = JSON.parseObject(JSON.toJSONString(new ForcedNulls())); | ||
| for (String name : NULL_FIELDS) { | ||
| assertTrue(object.containsKey(name), "missing field : " + name); | ||
| assertNull(object.get(name), "unexpected value for field : " + name); | ||
| } | ||
| assertEquals(NULL_FIELDS.length, object.size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void unannotatedNullsRemainOmitted() { | ||
| assertEquals("{}", JSON.toJSONString(new PlainNulls())); | ||
| } | ||
|
|
||
| @Test | ||
| public void contextWriteNullsStillWorks() { | ||
| JSONObject object = JSON.parseObject(JSON.toJSONString(new PlainNulls(), JSONWriter.Feature.WriteNulls)); | ||
| for (String name : NULL_FIELDS) { | ||
| assertTrue(object.containsKey(name), "missing field : " + name); | ||
| } | ||
| assertEquals(NULL_FIELDS.length, object.size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void fieldLevelNotWriteDefaultValueOnPrimitives() { | ||
| JSONObject object = JSON.parseObject(JSON.toJSONString(new Defaults())); | ||
| for (String name : DEFAULT_FIELDS) { | ||
| assertFalse(object.containsKey(name), "default value written for field : " + name); | ||
| } | ||
| assertEquals(1, object.size()); | ||
| assertEquals(7, object.getIntValue("control")); | ||
| } | ||
|
|
||
| @Test | ||
| public void nonDefaultValuesAreStillWritten() { | ||
| Defaults bean = new Defaults(); | ||
| bean.duration = 5; | ||
| bean.b = 6; | ||
| bean.c = 'x'; | ||
| bean.count = 3; | ||
| bean.size = 4L; | ||
| bean.flag = true; | ||
| bean.f = 1.5F; | ||
| bean.d = 2.5D; | ||
|
|
||
| JSONObject object = JSON.parseObject(JSON.toJSONString(bean)); | ||
| assertEquals(5, object.getIntValue("duration")); | ||
| assertEquals(6, object.getIntValue("b")); | ||
| assertEquals("x", object.getString("c")); | ||
| assertEquals(3, object.getIntValue("count")); | ||
| assertEquals(4L, object.getLongValue("size")); | ||
| assertTrue(object.getBooleanValue("flag")); | ||
| assertEquals(1.5F, object.getFloatValue("f")); | ||
| assertEquals(2.5D, object.getDoubleValue("d")); | ||
| assertEquals(7, object.getIntValue("control")); | ||
| } | ||
|
|
||
| @Test | ||
| public void asmAndReflectCreatorsAgree() { | ||
| assertEquals( | ||
| writeWith(ObjectWriterCreator.INSTANCE, new ForcedNulls()), | ||
| writeWith(ObjectWriterCreatorASM.INSTANCE, new ForcedNulls())); | ||
| assertEquals( | ||
| writeWith(ObjectWriterCreator.INSTANCE, new Defaults()), | ||
| writeWith(ObjectWriterCreatorASM.INSTANCE, new Defaults())); | ||
| } | ||
|
|
||
| private static String writeWith(ObjectWriterCreator creator, Object bean) { | ||
| ObjectWriter<Object> objectWriter = creator.createObjectWriter(bean.getClass()); | ||
| return objectWriter.toJSONString(bean); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Suggestion] R1-3: the fix never executes on the filtered write path —
ObjectWriterAdapter.write's filtered loop pre-skips nulls with the context-onlyisWriteNulls()before any per-field writer runs.Line 602 (
if (fieldValue == null && !jsonWriter.isWriteNulls()) continue;) drops annotated null fields whenever ANYNameFilter/PropertyFilter/ValueFilteris attached, while the no-filter fast path (582-589 →fieldWriter.write) honors them — adding an unrelated filter silently changes null-handling output. (Pre-existing for the fields that already worked; this diff extends the fast-path feature — and therefore this inconsistency — toint[]/long[]/enum.)Witness (probe on PR head, reflect creator; the only difference is a no-op
NameFilter):Suggested fix: merge field features into the pre-skip —
if (fieldValue == null && (jsonWriter.getFeatures(fieldWriter.features) & WriteNulls.mask) == 0) continue;— and decide/document whether null values become visible to filters. The filter chain (propertyPreFilter/nameFilter/contextNameFilter, 568-599) must keep its interposition semantics; the context-onlyisWriteNulls()atObjectWriterImplMap.java:151is non-field context and stays unchanged.Acceptance criterion: an
Issue7773test serializingForcedNullswith a pass-throughPropertyFilterasserting allNULL_FIELDSstill appear — RED while line 602 is context-only; please remove the merge and confirm it reds.中文说明
修复在「带过滤器」的写出路径上不会生效:
ObjectWriterAdapter.write的过滤循环在到达任何字段写出器之前,就用只看 context 的isWriteNulls()(602 行)把 null 字段预跳过了。只要挂了任意NameFilter/PropertyFilter/ValueFilter,字段级WriteNulls就被忽略;而无过滤器的快速路径(582-589 →fieldWriter.write)能正确生效——加一个无关过滤器会静默改变 null 处理输出(实测:plain 全部写出,filtered{})。注:对修复前就已生效的两个字段这是既有问题,本 PR 把快速路径特性扩展到int[]/long[]/enum 后,该不一致也随之扩大。建议:把字段特性并入预跳过判断(
jsonWriter.getFeatures(fieldWriter.features),参照FieldWriterEnum.java:336的合并写法),并明确/文档化 null 值是否对过滤器可见;过滤链(568-599)的拦截语义保持不变;ObjectWriterImplMap.java:151的 context-only 写法属非字段上下文,不动。— glm-5.3-flash via Qwen Code /review (v0.25.1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in
966050d94.The pre-skip now merges the field features —
(jsonWriter.getFeatures(fieldWriter.features) & WriteNulls.mask) == 0— so a field-levelWriteNullssurvives the filtered path exactly as it survives the no-filter fast path. A null value that survives is visible to the filter chain (the name/value filters see it like any other value); that is noted in the code comment, andObjectWriterImplMap's context-only gate is untouched.Issue7773#fieldLevelWriteNullsSurvivesFiltersserializesForcedNullsthrough a pass-throughPropertyFilteron both creators and asserts all ten fields are present, plus the unannotated control ({}with the same filter).中文说明
已在
966050d94修复。预跳过判断现在合并字段特性——(jsonWriter.getFeatures(fieldWriter.features) & WriteNulls.mask) == 0——字段级WriteNulls在带过滤器的路径上与无过滤器快速路径一样生效。存活下来的 null 值对过滤器链可见(name/value 过滤器像看待其他值一样看到它),代码注释中已说明;ObjectWriterImplMap的 context-only 写法未动。Issue7773#fieldLevelWriteNullsSurvivesFilters在两个 creator 上通过透传PropertyFilter序列化ForcedNulls,断言十个字段全部存在,并带未注解对照组(同样过滤器下为{})。