Skip to content

Commit cf61e32

Browse files
nirinchevelle-j
andauthored
RDART-950: Update nullability annotations for base url API (#1652)
* Update nullability annotations for base url API * Update packages/realm_dart/lib/src/app.dart * Update packages/realm_dart/lib/src/app.dart --------- Co-authored-by: LJ <[email protected]>
1 parent 6111633 commit cf61e32

File tree

6 files changed

+31
-28
lines changed

6 files changed

+31
-28
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## vNext (TBD)
22

33
### Enhancements
4-
* Allow configuration of generator per model class. Currently support specifying the constructor style to use.
4+
* Allow configuration of generator per model class. Currently support specifying the constructor style to use.
55
```dart
66
const config = GeneratorConfig(ctorStyle: CtorStyle.allNamed);
77
const realmModel = RealmModel.using(baseType: ObjectType.realmObject, generatorConfig: config);
@@ -24,6 +24,7 @@
2424

2525
### Fixed
2626
* Avoid: Attempt to execute code removed by Dart AOT compiler (TFA). (Issue [#1647](https://github.com/realm/realm-dart/issues/1647))
27+
* Fixed nullability annotations for the experimental API `App.baseUrl` and `App.updateBaseUrl`. The former is guaranteed not to be `null`, while the latter will now accept a `null` argument, in which case the base url will be restored to its default value. (Issue [#1523](https://github.com/realm/realm-dart/issues/1523))
2728

2829
### Compatibility
2930
* Realm Studio: 15.0.0 or later.

packages/realm_dart/lib/src/app.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,25 @@ class App implements Finalizable {
218218
}
219219

220220
/// Returns the current value of the base URL used to communicate with the server.
221+
///
222+
/// If an [updateBaseUrl] operation is currently in progress, this value will not
223+
/// be updated with the new value until that operation has completed.
221224
@experimental
222-
Uri? get baseUrl {
223-
return Uri.tryParse(realmCore.getBaseUrl(this) ?? '');
225+
Uri get baseUrl {
226+
return Uri.parse(realmCore.getBaseUrl(this));
224227
}
225228

226229
/// Temporarily overrides the [baseUrl] value from [AppConfiguration] with a new [baseUrl] value
227-
/// used for communicating with the server.
230+
/// used for communicating with the server. If set to `null`, the app will revert to the default
231+
/// base url.
232+
///
233+
/// If this operation fails, the app will continue to use the original base URL. If another [App]
234+
/// operation is started while this function is in progress, that request will use the original
235+
/// base URL location information.
228236
///
229237
/// The App will revert to using the value in [AppConfiguration] when it is restarted.
230238
@experimental
231-
Future<void> updateBaseUrl(Uri baseUrl) async {
239+
Future<void> updateBaseUrl(Uri? baseUrl) async {
232240
return await realmCore.updateBaseUrl(this, baseUrl);
233241
}
234242

packages/realm_dart/lib/src/native/realm_core.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,18 +2420,18 @@ class _RealmCore {
24202420
);
24212421
}
24222422

2423-
String? getBaseUrl(App app) {
2423+
String getBaseUrl(App app) {
24242424
final customDataPtr = _realmLib.realm_app_get_base_url(app.handle._pointer);
2425-
return customDataPtr.cast<Utf8>().toRealmDartString(freeRealmMemory: true);
2425+
return customDataPtr.cast<Utf8>().toRealmDartString(freeRealmMemory: true)!;
24262426
}
24272427

2428-
Future<void> updateBaseUrl(App app, Uri baseUrl) {
2428+
Future<void> updateBaseUrl(App app, Uri? baseUrl) {
24292429
final completer = Completer<void>();
24302430
using((arena) {
24312431
_realmLib.invokeGetBool(
24322432
() => _realmLib.realm_app_update_base_url(
24332433
app.handle._pointer,
2434-
baseUrl.toString().toCharPtr(arena),
2434+
baseUrl?.toString().toCharPtr(arena) ?? nullptr,
24352435
_realmLib.addresses.realm_dart_void_completion_callback,
24362436
_createAsyncCallbackUserdata(completer),
24372437
_realmLib.addresses.realm_dart_userdata_async_free,

packages/realm_dart/test/app_test.dart

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -299,24 +299,17 @@ void main() {
299299

300300
baasTest('App get Base URL', (configuration) async {
301301
final app = App(configuration);
302-
final credentials = Credentials.anonymous();
303-
await app.logIn(credentials);
304-
final baseUrl = app.baseUrl;
305-
expect(baseUrl, isNotNull);
306-
expect(baseUrl, configuration.baseUrl);
302+
expect(app.baseUrl, configuration.baseUrl);
307303
});
308304

309-
baasTest('App update Base URL', (configuration) async {
310-
final app = App(configuration);
311-
final credentials = Credentials.anonymous();
312-
await app.logIn(credentials);
313-
final baseUrl = app.baseUrl;
314-
expect(baseUrl, isNotNull);
305+
baasTest('App update Base URL', (appConfig) async {
306+
final config = await baasHelper!.getAppConfig(customBaseUrl: 'https://services.cloud.mongodb.com');
307+
final app = App(config);
308+
expect(app.baseUrl, Uri.parse('https://services.cloud.mongodb.com'));
315309
// Set it to the same thing to confirm the function works, it's not actually going to update the location
316-
await app.updateBaseUrl(baseUrl!);
317-
final newBaseUrl = app.baseUrl;
318-
expect(newBaseUrl, isNotNull);
319-
expect(newBaseUrl, baseUrl);
310+
await app.updateBaseUrl(Uri.parse(baasHelper!.baseUrl));
311+
expect(app.baseUrl, appConfig.baseUrl);
312+
expect(app.baseUrl, isNot(Uri.parse('https://services.cloud.mongodb.com')));
320313
});
321314

322315
test('bundleId is salted, hashed and encoded', () {

packages/realm_dart/test/baas_helper.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,10 @@ class BaasHelper {
177177
testing.printOnFailure("Splunk logs: $splunk");
178178
}
179179

180-
Future<AppConfiguration> getAppConfig({AppName appName = AppName.flexible}) => _getAppConfig(appName.name);
180+
Future<AppConfiguration> getAppConfig({AppName appName = AppName.flexible, String? customBaseUrl}) =>
181+
_getAppConfig(appName.name, customBaseUrl: customBaseUrl);
181182

182-
Future<AppConfiguration> _getAppConfig(String appName) async {
183+
Future<AppConfiguration> _getAppConfig(String appName, {String? customBaseUrl}) async {
183184
final app = _baasApps[appName] ??
184185
_baasApps.values.firstWhere((element) => element.name == BaasClient.defaultAppName, orElse: () => throw RealmError("No BAAS apps"));
185186
if (app.error != null) {
@@ -189,7 +190,7 @@ class BaasHelper {
189190
final temporaryDir = await Directory.systemTemp.createTemp('realm_test_');
190191
return AppConfiguration(
191192
app.clientAppId,
192-
baseUrl: Uri.parse(baseUrl),
193+
baseUrl: Uri.parse(customBaseUrl ?? baseUrl),
193194
baseFilePath: temporaryDir,
194195
maxConnectionTimeout: Duration(minutes: 10),
195196
defaultRequestTimeout: Duration(minutes: 7),

packages/realm_dart/test/test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ Future<void> baasTest(
597597

598598
skip = shouldSkip(skip);
599599

600-
test('[BAAS] $name', () async {
600+
test(name, () async {
601601
baasHelper!.printSplunkLogLink(appName, baasHelper?.baseUrl);
602602
final config = await baasHelper!.getAppConfig(appName: appName);
603603
await testFunction(config);

0 commit comments

Comments
 (0)