Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
getBool should be able to handle numerical values 1 and 0
  • Loading branch information
buenaflor committed Oct 23, 2025
commit 2a0b9c77cbcd63fd677b285ab2a93c509e8331c7
8 changes: 7 additions & 1 deletion packages/dart/lib/src/utils/type_safe_map_access.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ extension TypeSafeMapExtension on Map<String, dynamic> {
return null;
}

/// Type-safe bool extraction
/// Type-safe bool extraction with support for 0/1 as false/true
bool? getBool(String key) {
final value = this[key];
if (value == null) return null;
if (value is bool) return value;

// Handle numeric 0 and 1 as boolean values
if (value is num) {
if (value == 0) return false;
if (value == 1) return true;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason for this is when using FFI objective_c conversions bool are represented as 1 or 0 nums


_logTypeMismatch(key, 'bool', value.runtimeType.toString());
return null;
}
Expand Down
28 changes: 25 additions & 3 deletions packages/dart/test/protocol/sentry_device_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,37 @@ void main() {
test('bool fields return null for non-boolean values', () {
final map = {
'online': 'true',
'charging': 1,
'simulator': 'false',
'low_memory': 0,
};
final sentryDevice = SentryDevice.fromJson(map);
expect(sentryDevice.online, isNull);
expect(sentryDevice.charging, isNull);
expect(sentryDevice.simulator, isNull);
});

test('bool fields accept numeric 0 and 1 as false and true', () {
final map = {
'charging': 1,
'low_memory': 0,
'online': 1.0,
'simulator': 0.0,
};
final sentryDevice = SentryDevice.fromJson(map);
expect(sentryDevice.charging, true);
expect(sentryDevice.lowMemory, false);
expect(sentryDevice.online, true);
expect(sentryDevice.simulator, false);
});

test('bool fields return null for other numeric values', () {
final map = {
'charging': 2,
'low_memory': -1,
'online': 0.5,
};
final sentryDevice = SentryDevice.fromJson(map);
expect(sentryDevice.charging, isNull);
expect(sentryDevice.lowMemory, isNull);
expect(sentryDevice.online, isNull);
});

test('mixed valid and invalid data deserializes partially', () {
Expand Down
30 changes: 25 additions & 5 deletions packages/dart/test/utils/type_safe_map_access_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ void main() {
expect(map.getBool('key'), false);
});

test('returns true when value is numeric 1', () {
final map = <String, dynamic>{'key': 1};
expect(map.getBool('key'), true);
});

test('returns false when value is numeric 0', () {
final map = <String, dynamic>{'key': 0};
expect(map.getBool('key'), false);
});

test('returns true when value is double 1.0', () {
final map = <String, dynamic>{'key': 1.0};
expect(map.getBool('key'), true);
});

test('returns false when value is double 0.0', () {
final map = <String, dynamic>{'key': 0.0};
expect(map.getBool('key'), false);
});

test('returns null for other numeric values', () {
final map = <String, dynamic>{'key': 2};
expect(map.getBool('key'), isNull);
});

test('returns null when key does not exist', () {
final map = <String, dynamic>{};
expect(map.getBool('key'), isNull);
Expand All @@ -119,11 +144,6 @@ void main() {
final map = <String, dynamic>{'key': 'true'};
expect(map.getBool('key'), isNull);
});

test('returns null for numeric value', () {
final map = <String, dynamic>{'key': 1};
expect(map.getBool('key'), isNull);
});
});

group('getDateTime', () {
Expand Down
Loading