From 243d8143fec6a26737b46086e5991390f1c3dc66 Mon Sep 17 00:00:00 2001 From: Clemente Date: Mon, 13 Nov 2023 16:10:06 +0100 Subject: [PATCH 1/2] Allow aggregators on properties annotated with @PersistedName --- .../kotlin/internal/query/ObjectQuery.kt | 2 +- .../internal/schema/CachedClassKeyMap.kt | 5 +- .../kotlin/test/common/PersistedNameTests.kt | 54 +++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/query/ObjectQuery.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/query/ObjectQuery.kt index 348f8c0f0b..fe2891a88f 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/query/ObjectQuery.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/query/ObjectQuery.kt @@ -54,7 +54,7 @@ internal class ObjectQuery constructor( RealmInterop.realm_query_find_all(queryPointer) } - private val classMetadata: ClassMetadata? = realmReference.schemaMetadata[clazz.simpleName!!] + private val classMetadata: ClassMetadata? = realmReference.schemaMetadata[classKey] internal constructor( realmReference: RealmReference, diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/schema/CachedClassKeyMap.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/schema/CachedClassKeyMap.kt index 7f6c0b1649..7da918259f 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/schema/CachedClassKeyMap.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/schema/CachedClassKeyMap.kt @@ -24,6 +24,7 @@ import io.realm.kotlin.internal.interop.PropertyKey import io.realm.kotlin.internal.interop.PropertyType import io.realm.kotlin.internal.interop.RealmInterop import io.realm.kotlin.internal.interop.RealmPointer +import io.realm.kotlin.internal.interop.SCHEMA_NO_VALUE import io.realm.kotlin.types.BaseRealmObject import io.realm.kotlin.types.TypedRealmObject import kotlin.reflect.KClass @@ -64,6 +65,7 @@ public interface ClassMetadata { public interface PropertyMetadata { public val name: String + public val publicName: String public val key: PropertyKey public val collectionType: CollectionType public val type: PropertyType @@ -154,7 +156,7 @@ public class CachedClassMetadata( primaryKeyProperty = properties.firstOrNull { it.isPrimaryKey } isEmbeddedRealmObject = classInfo.isEmbedded - nameMap = properties.associateBy { it.name } + nameMap = properties.associateBy { it.name } + properties.filterNot { it.publicName == SCHEMA_NO_VALUE }.associateBy { it.publicName } keyMap = properties.associateBy { it.key } propertyMap = properties.associateBy { it.accessor } } @@ -169,6 +171,7 @@ public class CachedPropertyMetadata( override val accessor: KProperty1? = null ) : PropertyMetadata { override val name: String = propertyInfo.name + override val publicName: String = propertyInfo.publicName override val key: PropertyKey = propertyInfo.key override val collectionType: CollectionType = propertyInfo.collectionType override val type: PropertyType = propertyInfo.type diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/PersistedNameTests.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/PersistedNameTests.kt index 07dd7069e1..603047d1ee 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/PersistedNameTests.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/PersistedNameTests.kt @@ -27,6 +27,9 @@ import io.realm.kotlin.ext.realmListOf import io.realm.kotlin.ext.realmSetOf import io.realm.kotlin.internal.asDynamicRealm import io.realm.kotlin.migration.AutomaticSchemaMigration +import io.realm.kotlin.query.max +import io.realm.kotlin.query.min +import io.realm.kotlin.query.sum import io.realm.kotlin.schema.RealmStorageType import io.realm.kotlin.test.common.utils.assertFailsWithMessage import io.realm.kotlin.test.platform.PlatformUtils @@ -76,6 +79,54 @@ class PersistedNameTests { PlatformUtils.deleteTempDir(tmpDir) } + // -------------------------------------------------- + // Aggregators + // -------------------------------------------------- + + @Test + fun aggregators_byPublicName() { + realm.writeBlocking { + copyToRealm(PersistedNameSample()) + } + + assertEquals( + expected = 10, + actual = realm.query().sum("publicNameIntField").find() + ) + + assertEquals( + expected = 10, + actual = realm.query().max("publicNameIntField").find() + ) + + assertEquals( + expected = 10, + actual = realm.query().min("publicNameIntField").find() + ) + } + + @Test + fun aggregators_byPersistedName() { + realm.writeBlocking { + copyToRealm(PersistedNameSample()) + } + + assertEquals( + expected = 10, + actual = realm.query().sum("persistedNameIntField").find() + ) + + assertEquals( + expected = 10, + actual = realm.query().max("persistedNameIntField").find() + ) + + assertEquals( + expected = 10, + actual = realm.query().min("persistedNameIntField").find() + ) + } + // -------------------------------------------------- // Query // -------------------------------------------------- @@ -479,6 +530,9 @@ class PersistedNameSample : RealmObject { // the underlying schema due to being equal to the persisted name. @PersistedName("sameName2") var sameName2 = "Realm" + + @PersistedName("persistedNameIntField") + var publicNameIntField: Int = 10 } class PersistedNameParentSample(var id: Int) : RealmObject { From 374ddce2e8554ec890815e221a6961d5fa666457 Mon Sep 17 00:00:00 2001 From: Clemente Date: Mon, 13 Nov 2023 16:19:36 +0100 Subject: [PATCH 2/2] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8a2bb01ab..99e9ed341f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixed * Fix craches caused by posting to a released scheduler. (Issue [#1543](https://github.com/realm/realm-kotlin/issues/1543)) +* Fix NPE when applying query aggregators on classes annotated with `@PersistedName`. (Issue [1569](https://github.com/realm/realm-kotlin/pull/1569)) ### Compatibility * File format: Generates Realms with file format v23.