Repository navigation
Applying immutable map in SentryEvent#setExtras causes UnsupportedOperationException #1357
Description
Activity
@wzieba thanks for reporting and steps with repro, that's great.
I'm not entirely sure that we should project our setters with
UnsupportedOperationException, the SDK itself does not useCollections.unmodifiableListnor Kotlin immutable types, let me gather some feedback internally before doing such changes.We could check its type if
UnmodifiableCollectionand copy it to a nonimmutable type, I'll need to check whats the type for the Kotlin one though.Obviously a workaround would be using only non-immutable types for now, or appending the values using
SentryEvent#setExtra(key, value).Reacted by Wojciech Ziębanot sure if I should tag this as an
enhancementorbug, I'll figure this out.I'll add my 2 cents: In general calling
sentry-javaclasses from Kotlin seems to be a bit inconvenient and rather error prone. There are a lot of cases where it is not clear whether a field can be null, issues resulting in runtime crashes caused by immutability (as the one described in this ticket), or simply invonvenient constructor calls.In my case I tried to write custom
EventProcessor, and I had to struggle with multiple runtime crashes, before I reached a working solution.
I wish I could writesentryOptions.addEventProcessor { event, _ -> event.debugMeta.images += DebugImage(type = "proguard", uuid = "xxx") event }instead of guessing what should I do if
eventis null, how to create validDebugMetaand what's the difference between empty and nullimageslist, always remembering I sometimes can't use immutable objects. I always have to look into source code and unterstand the data flow and infer nullability and mutability.I don't know what's the proper way to address all concenrs, I just wanted to share you may want to treat this issue as making
sentry-javamore Kotlin friendly in general, not focusing on immutability in mentionesetExtrascase.Reacted by Manoel Aranda Neto, Wojciech Zięba and Łukasz Wasylkowski@mateuszkwiecinski I don't disagree, although some of the problems you've mentioned are already known and in the backlog.
eg for better
null-safetyusing Kotlin, #1137 adding @nullable and @NotNullable annotations
eg for addingproguard, #1236ideally, you'd not need to fiddle with these things, the SDK should do it automatically, in our protocol pretty much everything is nullable by default, we want to be as cheap as possible (memory footprint and JSON ser/deser, payload size over the wire etc).
thanks for the feedback.
Reacted by Mateusz Kwieciński, Wojciech Zięba, Łukasz Wasylkowski and Bruno GarciaReacted by Mateusz Kwiecińskiafter discussed internally, we'll work on that but along with other changes that improve the Kotlin experience overall, during Q2.
fwiw the App does not crash if applying an immutable list/map/etc to SentryEvent, it drops the event though and it fails silently (it logs out if debug is enabled).
Reacted by Wojciech Zięba and Mateusz Kwieciński- added a commit that references this issue
on May 12, 2021
Platform:
Device
API: 26compileSdkVersion: 30targetSdkVersion: 30buildTools: applied by Android Gradle PluginIDE:
4.2 beta 3Build system:
6.7.1Android Gradle Plugin:
4.2.0-beta03Sentry Android Gradle Plugin:
Proguard/R8:
Platform installed with:
The version of the SDK:
4.3.0
I have the following issue:
When applying extra data by
Sentry#setExtraand then applying extra data for a single event bySentryEvent#setExtraswithimmutable map(fromKotlin), I'm experiencingUnsupportedOperationExceptionas SDK tries to edit the immutable map that is passed.It crashes on the runtime as Java's method signatures will accept
immutableKotlin's map.Steps to reproduce:
I've prepared a reproduction repository.
To check it, please go to
SentryProxyTest. Both of the use cases are tested there.You can also check the results of those tests on CI.
The test with mutable map passes while test with immutable map throws an exception
Actual result:
When applying extra with
MutableMapeverything works fine, the exception is not thrown. When applying extra withMap(which is immutable),UnsupportedOperationExceptionis thrown.Expected result:
Sentry SDK should accept both the
mutableandimmutabletypes. Forimmutabletypes, if editing is required, it should map aMaptomutabletype.