Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RDART-950: Update nullability annotations for base url API #1652

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## vNext (TBD)

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

### Fixed
* Avoid: Attempt to execute code removed by Dart AOT compiler (TFA). (Issue [#1647](https://github.com/realm/realm-dart/issues/1647))
* 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))

### Compatibility
* Realm Studio: 15.0.0 or later.
Expand Down
16 changes: 12 additions & 4 deletions packages/realm_dart/lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,25 @@ class App implements Finalizable {
}

/// Returns the current value of the base URL used to communicate with the server.
nirinchev marked this conversation as resolved.
Show resolved Hide resolved
///
/// If an [updateBaseUrl] operation is currently in progress, this value will not
/// be updated with the new value until that operation has completed.
@experimental
Uri? get baseUrl {
return Uri.tryParse(realmCore.getBaseUrl(this) ?? '');
Uri get baseUrl {
return Uri.parse(realmCore.getBaseUrl(this));
}

/// Temporarily overrides the [baseUrl] value from [AppConfiguration] with a new [baseUrl] value
/// used for communicating with the server.
/// used for communicating with the server. If set to `null`, the app will revert to the default
/// base url.
nielsenko marked this conversation as resolved.
Show resolved Hide resolved
///
nirinchev marked this conversation as resolved.
Show resolved Hide resolved
/// If this operation fails, the app will continue to use the original base URL. If another [App]
/// operation is started while this function is in progress, that request will use the original
/// base URL location information.
///
/// The App will revert to using the value in [AppConfiguration] when it is restarted.
@experimental
Future<void> updateBaseUrl(Uri baseUrl) async {
Future<void> updateBaseUrl(Uri? baseUrl) async {
return await realmCore.updateBaseUrl(this, baseUrl);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/realm_dart/lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2420,18 +2420,18 @@ class _RealmCore {
);
}

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

Future<void> updateBaseUrl(App app, Uri baseUrl) {
Future<void> updateBaseUrl(App app, Uri? baseUrl) {
final completer = Completer<void>();
using((arena) {
_realmLib.invokeGetBool(
() => _realmLib.realm_app_update_base_url(
app.handle._pointer,
baseUrl.toString().toCharPtr(arena),
baseUrl?.toString().toCharPtr(arena) ?? nullptr,
_realmLib.addresses.realm_dart_void_completion_callback,
_createAsyncCallbackUserdata(completer),
_realmLib.addresses.realm_dart_userdata_async_free,
Expand Down
23 changes: 8 additions & 15 deletions packages/realm_dart/test/app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -299,24 +299,17 @@ void main() {

baasTest('App get Base URL', (configuration) async {
final app = App(configuration);
final credentials = Credentials.anonymous();
await app.logIn(credentials);
final baseUrl = app.baseUrl;
expect(baseUrl, isNotNull);
expect(baseUrl, configuration.baseUrl);
expect(app.baseUrl, configuration.baseUrl);
});

baasTest('App update Base URL', (configuration) async {
final app = App(configuration);
final credentials = Credentials.anonymous();
await app.logIn(credentials);
final baseUrl = app.baseUrl;
expect(baseUrl, isNotNull);
baasTest('App update Base URL', (appConfig) async {
final config = await baasHelper!.getAppConfig(customBaseUrl: 'https://services.cloud.mongodb.com');
final app = App(config);
expect(app.baseUrl, Uri.parse('https://services.cloud.mongodb.com'));
// Set it to the same thing to confirm the function works, it's not actually going to update the location
await app.updateBaseUrl(baseUrl!);
final newBaseUrl = app.baseUrl;
expect(newBaseUrl, isNotNull);
expect(newBaseUrl, baseUrl);
await app.updateBaseUrl(Uri.parse(baasHelper!.baseUrl));
expect(app.baseUrl, appConfig.baseUrl);
expect(app.baseUrl, isNot(Uri.parse('https://services.cloud.mongodb.com')));
});

test('bundleId is salted, hashed and encoded', () {
Expand Down
7 changes: 4 additions & 3 deletions packages/realm_dart/test/baas_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ class BaasHelper {
testing.printOnFailure("Splunk logs: $splunk");
}

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

Future<AppConfiguration> _getAppConfig(String appName) async {
Future<AppConfiguration> _getAppConfig(String appName, {String? customBaseUrl}) async {
final app = _baasApps[appName] ??
_baasApps.values.firstWhere((element) => element.name == BaasClient.defaultAppName, orElse: () => throw RealmError("No BAAS apps"));
if (app.error != null) {
Expand All @@ -189,7 +190,7 @@ class BaasHelper {
final temporaryDir = await Directory.systemTemp.createTemp('realm_test_');
return AppConfiguration(
app.clientAppId,
baseUrl: Uri.parse(baseUrl),
baseUrl: Uri.parse(customBaseUrl ?? baseUrl),
baseFilePath: temporaryDir,
maxConnectionTimeout: Duration(minutes: 10),
defaultRequestTimeout: Duration(minutes: 7),
Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ Future<void> baasTest(

skip = shouldSkip(skip);

test('[BAAS] $name', () async {
nielsenko marked this conversation as resolved.
Show resolved Hide resolved
test(name, () async {
baasHelper!.printSplunkLogLink(appName, baasHelper?.baseUrl);
final config = await baasHelper!.getAppConfig(appName: appName);
await testFunction(config);
Expand Down
Loading