Skip to content

Quality: Map equality treats missing keys as equal when mapped value is null#1080

Open
Nam0101 wants to merge 1 commit into
facebook:masterfrom
Nam0101:contribai/improve/quality/map-equality-treats-missing-keys-as-equa
Open

Quality: Map equality treats missing keys as equal when mapped value is null#1080
Nam0101 wants to merge 1 commit into
facebook:masterfrom
Nam0101:contribai/improve/quality/map-equality-treats-missing-keys-as-equa

Conversation

@Nam0101
Copy link
Copy Markdown

@Nam0101 Nam0101 commented Mar 29, 2026

✨ Code Quality

Problem

areMapsEqual compares values via next[key] without verifying key presence. If prev has {a: null} and next has {b: null} (same size, different keys), next[a] returns null, the value comparison passes, and the method incorrectly returns true. This can cause silent diffing errors (e.g., skipping updates when maps actually changed), leading to stale UI/state.

Severity: high
File: litho-core/src/main/java/com/facebook/litho/utils/MapDiffUtils.kt

Solution

Require key existence before value comparison:

@JvmStatic
fun <K, V> areMapsEqual(prev: Map<K, V>?, next: Map<K, V>?): Boolean {
if (prev === next) return true
if (prev == null || next == null) return false
if (prev.size != next.size) return false

return prev.all { (key, value) ->
next.containsKey(key) && equals(value, next[key])
}
}

Changes

  • litho-core/src/main/java/com/facebook/litho/utils/MapDiffUtils.kt (modified)

Summary

Changelog

Test Plan


🤖 About this PR

This pull request was generated by ContribAI, an AI agent
that helps improve open source projects. The change was:

  1. Discovered by automated code analysis
  2. Generated by AI with context-aware code generation
  3. Self-reviewed by AI quality checks

If you have questions or feedback about this PR, please comment below.
We appreciate your time reviewing this contribution!

Closes #1079

… is null

`areMapsEqual` compares values via `next[key]` without verifying key presence. If `prev` has `{a: null}` and `next` has `{b: null}` (same size, different keys), `next[a]` returns `null`, the value comparison passes, and the method incorrectly returns `true`. This can cause silent diffing errors (e.g., skipping updates when maps actually changed), leading to stale UI/state.


Affected files: MapDiffUtils.kt

Signed-off-by: Nguyen Van Nam <nam.nv205106@gmail.com>
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Mar 29, 2026

Hi @Nam0101!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Mar 29, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: map equality treats missing keys as equal when mapped value is null

1 participant