firebase-android-sdk icon indicating copy to clipboard operation
firebase-android-sdk copied to clipboard

RemoteMessage.getData() should return a defensive copy of the intneral ArrayMap from its public API

Open dlam opened this issue 4 months ago • 3 comments

Context: https://issuetracker.google.com/393513316

ArrayMap is a performance-sensitive collection which does not support holding references to the returned Map.Entry when iterating through it. Many kotlin-stdlib extensions on collections do not create defensive copies (intentionally for performance), so it is quite confusing to end users that for example RemoteMessage.data.entries.sortedBy { ..} returned a list of the last entry repeated size times.

Instead, I would generally recommend that public API return defensive copies of collections to avoid this confusion and to keep the performance sensitive stuff internal. We have a similar rule for android platform + libraries here: go/android-api-guidelines#type-semantics

I believe this is the problematic call-site: https://github.com/firebase/firebase-android-sdk/blob/5e778b940e72d6ce10455a6604f2857820df1d28/firebase-messaging/src/main/java/com/google/firebase/messaging/Constants.java#L120

and the affected api in the linked buganizer issue against androidx.collection: https://github.com/firebase/firebase-android-sdk/blob/5e778b940e72d6ce10455a6604f2857820df1d28/firebase-messaging/src/main/java/com/google/firebase/messaging/RemoteMessage.java#L130

dlam avatar Aug 13 '25 22:08 dlam

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Aug 13 '25 22:08 google-oss-bot

Hi @dlam, thank you for raising the issue. Let me raise this to our engineers and get back to you! Thanks!

lehcar09 avatar Aug 14 '25 14:08 lehcar09

PR #7513

lehcar09 avatar Oct 27 '25 17:10 lehcar09