realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Apache License 2.0
958 stars 61 forks source link

Realm Compiler Plugin does not correctly detect the use of nullable lists: RealmList<*>? #1540

Open ffdez opened 1 year ago

ffdez commented 1 year ago

How frequently does the bug occur?

Always

Description

With this Realm entities:

class EntityRm(
    @PrimaryKey var id: String,
    var optionalCars: RealmList<CarRm>? = realmListOf(),
): RealmObject {
    constructor() : this(id = "")
}

class CarRm(
    var name: String = "",
    var description: String = "",
): RealmObject {
    constructor() : this(name = "")
}

When I try to save a null list, it crash:

var entity = EntityRm("1", null)
dataBase.write {
    copyToRealm(entity, UpdatePolicy.ALL)
}

Moreover, this error isn't controlled by Realm, because Transaction keeps running. When this error ocurrs, If I try to perform any other operation, it throws a IllegalStateException.

Stacktrace & log output

Error: null cannot be cast to non-null type io.realm.kotlin.types.RealmList<*>
    java.lang.NullPointerException: null cannot be cast to non-null type io.realm.kotlin.types.RealmList<*>
        at io.realm.kotlin.internal.RealmObjectHelper.assignTyped$io_realm_kotlin_library(RealmObjectHelper.kt:800)
        at io.realm.kotlin.internal.RealmObjectHelper.assign$io_realm_kotlin_library(RealmObjectHelper.kt:734)
        at io.realm.kotlin.internal.RealmUtilsKt.copyToRealm(RealmUtils.kt:220)
        at io.realm.kotlin.internal.RealmUtilsKt.copyToRealm$default(RealmUtils.kt:144)
        at io.realm.kotlin.internal.InternalMutableRealm$DefaultImpls.copyToRealm(InternalMutableRealm.kt:57)
        at io.realm.kotlin.internal.SuspendableWriter$WriterRealm.copyToRealm(SuspendableWriter.kt:58)

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

1.10.0 and 1.11.1

What Atlas App Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Android 34

Build environment

Android Studio version: Giraffe | 2022.3.1 Patch Android Build Tools version: 8.1.2 Gradle version: 8.3

cmelchior commented 1 year ago
var optionalCars: RealmList<CarRm>? = realmListOf(),

This is not allowed, lists are not optional. If you should change it to var optionalCars: RealmList<CarRm> = realmListOf() it should work.

That said, I am a bit surprised we didn't catch this at compile time, which we should.

mr-gilberto commented 1 year ago

I am getting the same error, and the documentation didn't clarify this .

nxoim commented 1 month ago

That said, I am a bit surprised we didn't catch this at compile time, which we should.

any updates regarding this compile time check?