Skip to content

fix: NameFilter returning null causes Map keys to serialize as null (#7644)#7658

Open
1919chichi wants to merge 1 commit into
alibaba:mainfrom
1919chichi:fix/name-filter-null-map-key-7644
Open

fix: NameFilter returning null causes Map keys to serialize as null (#7644)#7658
1919chichi wants to merge 1 commit into
alibaba:mainfrom
1919chichi:fix/name-filter-null-map-key-7644

Conversation

@1919chichi

Copy link
Copy Markdown

What this PR does / why we need it?

When NameFilter.process() returns null for a Map entry key in ObjectWriterImplMap.writeWithFilter(), the key is directly overwritten with null and serialized as null:"value" instead of preserving the original key name.

Fixes #7644

Summary of your change

Added a null guard in ObjectWriterImplMap.writeWithFilter() so that when NameFilter.process() returns null, the original key is preserved — consistent with the existing behavior in:

  • ObjectWriterAdapter (POJO fields): boolean nameChanged = filteredName != null && ...
  • JSONObject.nameFilter(): if (processName != null && !processName.equals(key))

Before:

if (nameFilter != null) {
    key = nameFilter.process(object, key, value); // null overwrites key directly
}

After:

if (nameFilter != null) {
    String filteredKey = nameFilter.process(object, key, value);
    if (filteredKey != null) {
        key = filteredKey; // null → preserve original key
    }
}

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed.

…libaba#7644)

When NameFilter.process() returns null for a Map entry key in
ObjectWriterImplMap, the key was overwritten with null and serialized
as `null:value` instead of preserving the original key.

Add a null guard so that a null return value falls back to the original
key, consistent with the behavior in ObjectWriterAdapter (POJO fields)
and JSONObject.nameFilter().

Fixes alibaba#7644

@wenshao wenshao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix is correct and minimal. The null guard in ObjectWriterImplMap.writeWithFilter() properly preserves the original Map key when NameFilter.process() returns null, consistent with the existing behavior in JSONObject.nameFilter(). Tests cover the core scenarios well. LGTM! ✅

— qwen3.7-max via Qwen Code /review

@wenshao wenshao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found. LGTM! ✅

— Qwen Code via Qwen Code /review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] json序列化有bug,key 序列化为 null

2 participants