Quality: Map equality treats missing keys as equal when mapped value is null#1080
Conversation
… 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>
|
Hi @Nam0101! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
✨ Code Quality
Problem
areMapsEqualcompares values vianext[key]without verifying key presence. Ifprevhas{a: null}andnexthas{b: null}(same size, different keys),next[a]returnsnull, the value comparison passes, and the method incorrectly returnstrue. This can cause silent diffing errors (e.g., skipping updates when maps actually changed), leading to stale UI/state.Severity:
highFile:
litho-core/src/main/java/com/facebook/litho/utils/MapDiffUtils.ktSolution
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:
If you have questions or feedback about this PR, please comment below.
We appreciate your time reviewing this contribution!
Closes #1079