Skip to content

Commit b30f384

Browse files
authored
Pass in correct schema type for explicit and autodiscovered types (#2259)
* Wrappers can now discern if to use AdditiveExplicit or AdditiveDiscovered * Test added, but need to rename exception * Added RealmSchemaValidationException * Fixed small issues * Added entry in the Changelog and fixed a line in documentation of the new exception * Slight improvement in the unit test * Applied PR feedback * Grammar fix * More english grammar fixes * Removed a space * Feedback from PR applied
1 parent 2277bd5 commit b30f384

15 files changed

Lines changed: 100 additions & 12 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Fixed
44
* Fixed an issue that would result in UWP apps being rejected from the Microsoft Store due to an unsupported API (`__C_specific_handler`) being used. (Issue [#2235](https://github.com/realm/realm-dotnet/issues/2235))
55
* Fixed a bug where applying multiple `OrderBy` clauses on a query would result in the clauses being appended to each other as if they were `.ThenBy` rather than the last clause replacing the preceding ones. (PR [#2255](https://github.com/realm/realm-dotnet/issues/2255))
6+
* When explicitly specifying `SyncConfiguration.ObjectTypes`, added a check to validate the schema and ensure all `EmbeddedObject` classes are reachable from a class inheriting from `RealmObject`. More info about this subject can be found [here](https://docs.mongodb.com/realm/dotnet/objects/#provide-a-subset-of-classes-to-your-realm-schema). (PR [#2259](https://github.com/realm/realm-dotnet/pull/2259))
67

78
### Enhancements
89
* Add support for the `Guid` data type. It can be used as primary key and is indexable. (PR [#2120](https://github.com/realm/realm-dotnet/pull/2120))

Realm/Realm/Configurations/InMemoryConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public InMemoryConfiguration(string identifier) : base(identifier)
4949

5050
internal override Realm CreateRealm(RealmSchema schema)
5151
{
52-
var configuration = CreateConfiguration();
52+
var configuration = CreateNativeConfiguration();
5353
configuration.in_memory = true;
5454

5555
var srPtr = SharedRealmHandle.Open(configuration, schema, EncryptionKey);

Realm/Realm/Configurations/RealmConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public RealmConfiguration ConfigWithPath(string newConfigPath)
125125

126126
internal override Realm CreateRealm(RealmSchema schema)
127127
{
128-
var configuration = CreateConfiguration();
128+
var configuration = CreateNativeConfiguration();
129129
configuration.delete_if_migration_needed = ShouldDeleteIfMigrationNeeded;
130130
configuration.read_only = IsReadOnly;
131131

Realm/Realm/Configurations/RealmConfigurationBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ internal RealmConfigurationBase Clone()
150150

151151
internal abstract Task<Realm> CreateRealmAsync(RealmSchema schema, CancellationToken cancellationToken);
152152

153-
internal Native.Configuration CreateConfiguration()
153+
internal Native.Configuration CreateNativeConfiguration()
154154
{
155155
return new Native.Configuration
156156
{

Realm/Realm/Configurations/SyncConfiguration.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ private SyncConfiguration(object partition, User user, string path)
122122

123123
internal override Realm CreateRealm(RealmSchema schema)
124124
{
125-
var configuration = CreateConfiguration();
125+
var configuration = CreateNativeConfiguration();
126+
var syncConfiguration = CreateNativeSyncConfiguration();
126127

127-
var srHandle = SharedRealmHandle.OpenWithSync(configuration, ToNative(), schema, EncryptionKey);
128+
var srHandle = SharedRealmHandle.OpenWithSync(configuration, syncConfiguration, schema, EncryptionKey);
128129
if (IsDynamic && !schema.Any())
129130
{
130131
srHandle.GetSchema(nativeSchema => schema = RealmSchema.CreateFromObjectStoreSchema(nativeSchema));
@@ -136,14 +137,15 @@ internal override Realm CreateRealm(RealmSchema schema)
136137
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The Realm instance will own its handle")]
137138
internal override async Task<Realm> CreateRealmAsync(RealmSchema schema, CancellationToken cancellationToken)
138139
{
139-
var configuration = CreateConfiguration();
140+
var configuration = CreateNativeConfiguration();
141+
var syncConfiguration = CreateNativeSyncConfiguration();
140142

141143
var tcs = new TaskCompletionSource<ThreadSafeReferenceHandle>();
142144
var tcsHandle = GCHandle.Alloc(tcs);
143145
ProgressNotificationToken progressToken = null;
144146
try
145147
{
146-
using var handle = SharedRealmHandle.OpenWithSyncAsync(configuration, ToNative(), schema, EncryptionKey, tcsHandle);
148+
using var handle = SharedRealmHandle.OpenWithSyncAsync(configuration, syncConfiguration, schema, EncryptionKey, tcsHandle);
147149
cancellationToken.Register(() =>
148150
{
149151
if (!handle.IsClosed)
@@ -187,14 +189,15 @@ internal override async Task<Realm> CreateRealmAsync(RealmSchema schema, Cancell
187189
}
188190
}
189191

190-
internal Native.SyncConfiguration ToNative()
192+
internal Native.SyncConfiguration CreateNativeSyncConfiguration()
191193
{
192194
return new Native.SyncConfiguration
193195
{
194196
SyncUserHandle = User.Handle,
195197
Partition = Partition.ToNativeJson(),
196198
session_stop_policy = SessionStopPolicy,
199+
schema_mode = ObjectClasses == null ? SchemaMode.AdditiveDiscovered : SchemaMode.AdditiveExplicit,
197200
};
198201
}
199202
}
200-
}
203+
}

Realm/Realm/Exceptions/RealmException.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ internal static Exception Create(RealmExceptionCodes exceptionCode, string messa
104104
case RealmExceptionCodes.RealmClosed:
105105
return new RealmClosedException(message);
106106

107+
case RealmExceptionCodes.RealmSchemaValidation:
108+
return new RealmSchemaValidationException(message);
109+
107110
case RealmExceptionCodes.NotNullableProperty:
108111
case RealmExceptionCodes.PropertyMismatch:
109112
return new RealmException(message);

Realm/Realm/Exceptions/RealmExceptionCodes.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ internal enum RealmExceptionCodes : sbyte
3636
RealmInvalidTransaction = 10,
3737
RealmFormatUpgradeRequired = 13,
3838
RealmSchemaMismatch = 14,
39+
RealmSchemaValidation = 15,
40+
3941
RealmRowDetached = 21,
4042
RealmTableHasNoPrimaryKey = 22,
4143
RealmDuplicatePrimaryKeyValue = 23,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright 2021 Realm Inc.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
////////////////////////////////////////////////////////////////////////////
18+
19+
namespace Realms.Exceptions
20+
{
21+
/// <summary>
22+
/// Exception thrown when the schema specified in your C# models doesn't pass validation for the type of Realm you're trying to open.
23+
/// The message contains information about the validation errors and how to correct them.
24+
/// </summary>
25+
/// <seealso href="https://docs.mongodb.com/realm/dotnet/realms/#std-label-dotnet-realm-schema">
26+
/// General information about Realm's schema.
27+
/// </seealso>
28+
public class RealmSchemaValidationException : RealmException
29+
{
30+
internal RealmSchemaValidationException(string detailMessage) : base(detailMessage)
31+
{
32+
}
33+
}
34+
}

Realm/Realm/Native/SyncConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,7 @@ internal string Partition
4949
}
5050

5151
internal SessionStopPolicy session_stop_policy;
52+
53+
internal SchemaMode schema_mode;
5254
}
5355
}

Realm/Realm/Sync/SchemaMode.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright 2021 Realm Inc.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
////////////////////////////////////////////////////////////////////////////
18+
19+
namespace Realms.Sync
20+
{
21+
/// <summary>
22+
/// To see details of each mode check its mirroring definition in core.
23+
/// </summary>
24+
internal enum SchemaMode : byte
25+
{
26+
AdditiveDiscovered = 4,
27+
AdditiveExplicit = 5
28+
}
29+
}

0 commit comments

Comments
 (0)