Skip to content

Commit

Permalink
Merge branch 'releases'
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
Christian Melchior committed Nov 29, 2023
2 parents 75b35dc + 30c3bfd commit 79b76ef
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
* None.

### Fixed
* Fix crashes caused by posting to a released scheduler. (Issue [#1543](https://github.com/realm/realm-kotlin/issues/1543))
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal class ObjectQuery<E : BaseRealmObject> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 }
}
Expand All @@ -169,6 +171,7 @@ public class CachedPropertyMetadata(
override val accessor: KProperty1<BaseRealmObject, Any?>? = 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.RealmSet
import io.realm.kotlin.types.RealmUUID
import io.realm.kotlin.types.annotations.PersistedName
import org.mongodb.kbson.BsonObjectId
import org.mongodb.kbson.Decimal128

Expand Down Expand Up @@ -178,6 +179,9 @@ class Sample : RealmObject {
val listBacklinks by backlinks(Sample::objectListField)
val setBacklinks by backlinks(Sample::objectSetField)

@PersistedName("persistedStringField")
var publicStringField = "Realm"

// For verification that references inside class is also using our modified accessors and are
// not optimized to use the backing field directly.
fun stringFieldGetter(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
package io.realm.kotlin.entities.migration

import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PersistedName

@Suppress("MagicNumber")
class Sample : RealmObject {
var name: String = "Migration"
var stringField: String = "Realm"
var intField: Int = 42

@PersistedName("persistedStringField")
var publicStringField = ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,6 +79,54 @@ class PersistedNameTests {
PlatformUtils.deleteTempDir(tmpDir)
}

// --------------------------------------------------
// Aggregators
// --------------------------------------------------

@Test
fun aggregators_byPublicName() {
realm.writeBlocking {
copyToRealm(PersistedNameSample())
}

assertEquals(
expected = 10,
actual = realm.query<PersistedNameSample>().sum<Int>("publicNameIntField").find()
)

assertEquals(
expected = 10,
actual = realm.query<PersistedNameSample>().max<Int>("publicNameIntField").find()
)

assertEquals(
expected = 10,
actual = realm.query<PersistedNameSample>().min<Int>("publicNameIntField").find()
)
}

@Test
fun aggregators_byPersistedName() {
realm.writeBlocking {
copyToRealm(PersistedNameSample())
}

assertEquals(
expected = 10,
actual = realm.query<PersistedNameSample>().sum<Int>("persistedNameIntField").find()
)

assertEquals(
expected = 10,
actual = realm.query<PersistedNameSample>().max<Int>("persistedNameIntField").find()
)

assertEquals(
expected = 10,
actual = realm.query<PersistedNameSample>().min<Int>("persistedNameIntField").find()
)
}

// --------------------------------------------------
// Query
// --------------------------------------------------
Expand Down Expand Up @@ -181,9 +232,9 @@ class PersistedNameTests {

assertNotNull(dynamicSample)
assertEquals("Realm", dynamicSample.getValue("persistedNameStringField"))
assertFailsWithMessage<IllegalArgumentException>("Schema for type 'AlternativePersistedNameSample' doesn't contain a property named 'publicNameStringField'") {
dynamicSample.getValue("publicNameStringField")
}
// We can access property via the public name because the dynamic Realm is build upon a typed
// Realm via the extension function `asDynamicRealm`.
assertEquals("Realm", dynamicSample.getValue("publicNameStringField"))
}

// --------------------------------------------------
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ class RealmMigrationTests {
PlatformUtils.deleteTempDir(tmpDir)
}

@Test
fun migrationContext_publicNamesNotAvailable() {
migration(
initialSchema = setOf(
io.realm.kotlin.entities.schema.SchemaVariations::class,
io.realm.kotlin.entities.Sample::class
),
migratedSchema = setOf(io.realm.kotlin.entities.migration.Sample::class),
migration = { context ->
val oldRealm = context.oldRealm
val newRealm = context.newRealm

assertNotNull(oldRealm.schema()["Sample"]?.get("persistedStringField"))
assertNull(oldRealm.schema()["Sample"]?.get("publicStringField"))

assertNotNull(newRealm.schema()["Sample"]?.get("persistedStringField"))
assertNull(newRealm.schema()["Sample"]?.get("publicStringField"))
}
)
}

@Test
fun migrationContext_schemaVerification() {
migration(
Expand Down

0 comments on commit 79b76ef

Please sign in to comment.