From 150fffb6c37aff11097dd64aa7d37637ec426f84 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Thu, 6 Jun 2024 10:44:09 +0200 Subject: [PATCH 1/2] Add more validation to .Filter arguments --- CHANGELOG.md | 2 +- Realm/Realm/DatabaseTypes/QueryArgument.cs | 14 +++++++-- Tests/Realm.Tests/Database/CollectionTests.cs | 29 +++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f74c98e53..253b60ae49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ * None ### Fixed -* None +* Passing in a deleted object as a substitution argument to `.Filter()` would throw a confusing error with a message starting with `invalid RQL for table`. It now throws a more descriptive error instead. (Issue [#3619](https://github.com/realm/realm-dotnet/issues/3619)) ### Compatibility * Realm Studio: 15.0.0 or later. diff --git a/Realm/Realm/DatabaseTypes/QueryArgument.cs b/Realm/Realm/DatabaseTypes/QueryArgument.cs index d92e1c4958..8c954ee9a3 100644 --- a/Realm/Realm/DatabaseTypes/QueryArgument.cs +++ b/Realm/Realm/DatabaseTypes/QueryArgument.cs @@ -19,7 +19,6 @@ using System; using System.Linq; using MongoDB.Bson; -using Realms.Exceptions; using Realms.Native; namespace Realms @@ -318,9 +317,18 @@ private QueryArgument(RealmValue? realmValue = null, GeoShapeBase? geoValue = nu if (RealmValue != null) { var primitive = RealmValue.Value; - if (primitive.Type == RealmValueType.Object && !primitive.AsIRealmObject().IsManaged) + if (primitive.Type == RealmValueType.Object) { - throw new RealmException("Can't use unmanaged object as argument of Filter"); + var obj = primitive.AsIRealmObject(); + if (!obj.IsManaged) + { + throw new ArgumentException("Can't use unmanaged object as argument of Filter"); + } + + if (!obj.IsValid) + { + throw new ArgumentException("Can't use removed object as argument of Filter"); + } } var (primitiveValue, handles) = primitive.ToNative(); diff --git a/Tests/Realm.Tests/Database/CollectionTests.cs b/Tests/Realm.Tests/Database/CollectionTests.cs index d375801a63..93aefd9490 100644 --- a/Tests/Realm.Tests/Database/CollectionTests.cs +++ b/Tests/Realm.Tests/Database/CollectionTests.cs @@ -1120,6 +1120,35 @@ public void Results_GetFiltered_SanityTest() Assert.That(query.ToArray().All(i => i.Int >= 5)); } + [Test] + public void Results_Filter_WhenArgumentIsDeletedObject_Throws() + { + var targetObj = _realm.Write(() => + { + var intPropertyObject = _realm.Add(new IntPropertyObject()); + + for (var i = 0; i < 10; i++) + { + _realm.Add(new ObjectWithObjectProperties + { + StandaloneObject = i % 2 == 0 ? intPropertyObject : null + }); + } + + return intPropertyObject; + }); + + var query = _realm.All().Filter($"{nameof(ObjectWithObjectProperties.StandaloneObject)} = $0", targetObj); + Assert.That(query.Count(), Is.EqualTo(5)); + + _realm.Write(() => + { + _realm.Remove(targetObj); + }); + + Assert.Throws(() => _realm.All().Filter($"{nameof(ObjectWithObjectProperties.StandaloneObject)} = $0", targetObj)); + } + public readonly struct StringQueryNumericData { public readonly string PropertyName; From 8e0b226e445d66992e4ec6cab4d259e9603bc441 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Wed, 12 Jun 2024 14:48:36 +0200 Subject: [PATCH 2/2] Fix expected exception type --- Tests/Realm.Tests/Database/CollectionTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Realm.Tests/Database/CollectionTests.cs b/Tests/Realm.Tests/Database/CollectionTests.cs index 93aefd9490..8f518c1cce 100644 --- a/Tests/Realm.Tests/Database/CollectionTests.cs +++ b/Tests/Realm.Tests/Database/CollectionTests.cs @@ -1404,7 +1404,7 @@ public void QueryFilter_WithArgumentsUnmanagedObjects_ShouldThrow() _realm.Add(new Owner { TopDog = new Dog { Name = "Doge", Color = "almost yellow", Vaccinated = true } }); }); - Assert.Throws(() => _realm.All().Filter("TopDog = $0", new Dog { Name = "Doge", Color = "almost yellow", Vaccinated = true })); + Assert.Throws(() => _realm.All().Filter("TopDog = $0", new Dog { Name = "Doge", Color = "almost yellow", Vaccinated = true })); } [Test]