Skip to content
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
29 changes: 21 additions & 8 deletions lib/src/core/model/trait.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import 'package:json_annotation/json_annotation.dart';
import 'identity.dart';
part 'trait.g.dart';

// Default for copyWith's value, so that an explicit null can be told apart.
const _unset = Object();

@JsonSerializable()
class Trait {
@JsonKey(includeIfNull: false)
Expand Down Expand Up @@ -32,12 +35,12 @@ class Trait {
Trait copyWith({
int? id,
String? key,
dynamic value,
dynamic value = _unset,
}) =>
Trait(
id: id ?? this.id,
key: key ?? this.key,
value: value ?? this.value,
value: identical(value, _unset) ? this.value : value,
);

static dynamic _fromJson(dynamic jsonValue) {
Expand All @@ -47,14 +50,19 @@ class Trait {
} else {
return jsonValue.toDouble(); // Treat as double if it's a decimal
}
} else if (jsonValue is String || jsonValue is bool) {
} else if (jsonValue == null || jsonValue is String || jsonValue is bool) {
return jsonValue;
}
throw ArgumentError('Invalid value type');
}

static dynamic _toJson(dynamic value) {
if (value is double || value is int || value is String || value is bool) {
// A null value deletes the trait in Flagsmith.
if (value == null ||
Comment thread
Zaimwa9 marked this conversation as resolved.
value is double ||
value is int ||
value is String ||
value is bool) {
return value;
}
throw ArgumentError('Invalid value type');
Expand Down Expand Up @@ -89,12 +97,12 @@ class TraitWithIdentity {
TraitWithIdentity copyWith({
Identity? identity,
String? key,
dynamic value,
dynamic value = _unset,
}) =>
TraitWithIdentity(
identity: identity ?? this.identity,
key: key ?? this.key,
value: value ?? this.value,
value: identical(value, _unset) ? this.value : value,
);

static dynamic _fromJson(dynamic jsonValue) {
Expand All @@ -104,14 +112,19 @@ class TraitWithIdentity {
} else {
return jsonValue.toDouble(); // Treat as double if it's a decimal
}
} else if (jsonValue is String || jsonValue is bool) {
} else if (jsonValue == null || jsonValue is String || jsonValue is bool) {
return jsonValue;
}
throw ArgumentError('Invalid value type');
}

static dynamic _toJson(dynamic value) {
if (value is double || value is int || value is String || value is bool) {
// A null value deletes the trait in Flagsmith.
if (value == null ||
value is double ||
value is int ||
value is String ||
value is bool) {
return value;
}
throw ArgumentError('Invalid value type');
Expand Down
39 changes: 39 additions & 0 deletions test/core/models/trait_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,45 @@ void main() {
expect(mapped['trait_value'], 'value');
});
});
group('[Trait] - null value', () {
test('When trait value is null, then it is parsed as null', () {
final trait = Trait.fromJson({'trait_key': 'plan', 'trait_value': null});
expect(trait.value, isNull);
});

test('When trait value is null, then it is serialised as null', () {
final mapped = Trait(key: 'plan', value: null).toJson();
expect(mapped, {'trait_key': 'plan', 'trait_value': null});
expect(Trait.fromJson(jsonDecode(jsonEncode(mapped))).value, isNull);
});

test('When trait with identity value is null, then it round-trips', () {
final trait = TraitWithIdentity(
identity: Identity(identifier: 'user'), key: 'plan', value: null);
final mapped = trait.toJson();
expect(mapped['trait_value'], isNull);
expect(mapped.containsKey('trait_value'), isTrue);
expect(TraitWithIdentity.fromJson(mapped).value, isNull);
});

test('When copyWith sets value to null, then value is null', () {
final trait = Trait(key: 'plan', value: 'premium');
expect(trait.copyWith(value: null).value, isNull);
expect(trait.copyWith(key: 'tier').value, 'premium');
});

test(
'When trait with identity copyWith sets value to null, then value '
'is null', () {
final trait = TraitWithIdentity(
identity: Identity(identifier: 'user'),
key: 'plan',
value: 'premium');
expect(trait.copyWith(value: null).value, isNull);
expect(trait.copyWith(key: 'tier').value, 'premium');
});
});

group('[Trait] - copyWith', () {
test('as a developer want to use with new data', () {
final trait = Trait.fromJson(decodedTraitStringValue);
Expand Down
18 changes: 18 additions & 0 deletions test/fg/flagsmith_traits_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,24 @@ void main() {
expect(result, isNotEmpty);
});

test('When get flags with a null trait, then it is sent as null', () async {
var user = Identity(identifier: 'test_another_user');
fs = await setupClientAdapter(StorageType.inMemory);
setupEmptyAdapter(fs, cb: (config, adapter) {
adapter.onPost(fs.config.identitiesURI, (server) {
return server.reply(200, jsonDecode(identitiesResponseData));
}, data: {
'identifier': 'test_another_user',
'traits': [
{'trait_key': 'plan', 'trait_value': null},
],
});
});
final result = await fs.getFeatureFlags(
user: user, traits: [Trait(key: 'plan', value: null)]);
expect(result, isNotEmpty);
});

test('When get flags for transient identity request expected', () async {
var user = Identity(identifier: 'test_another_user', transient: true);
fs = await setupClientAdapter(StorageType.inMemory);
Expand Down
Loading