Skip to content

Commit

Permalink
🦺 prevent Utils.encode from calling toString on complex types
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Apr 5, 2024
1 parent 5d2a214 commit e2bdcf5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
10 changes: 10 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ final class Utils {
Encoding charset = utf8,
Format? format = Format.rfc3986,
}) {
// these can not be encoded
if (value is Iterable ||
value is Map ||
value is Symbol ||
value is Record ||
value is Future ||
value is Undefined) {
return '';
}

final String? str = value is ByteBuffer
? charset.decode(value.asUint8List())
: value?.toString();
Expand Down
35 changes: 16 additions & 19 deletions test/unit/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,25 @@ void main() {
);
expect(DummyEnum.lorem, isA<Enum>());
expect(Utils.encode(DummyEnum.lorem), equals('lorem'));

// does not encode
// Iterable
expect(Utils.encode([1, 2]), equals(''));
// Map
expect(Utils.encode({'a': 'b'}), equals(''));
// Symbol
expect(Utils.encode(#a), equals(''));
// Record
expect(Utils.encode(('a', 'b')), equals(''));
// Future
expect(
Utils.encode({
'foo': 'bar',
'baz': [
{'a': 'b'},
{'c': DummyEnum.dolor},
],
}),
equals(
'%7Bfoo%3A%20bar%2C%20baz%3A%20%5B%7Ba%3A%20b%7D%2C%20%7Bc%3A%20dolor%7D%5D%7D',
),
Utils.encode(Future.value('b')),
equals(''),
);
// Undefined
expect(
Utils.encode({
'filters': {
'name': 'foo',
'example': DummyEnum.lorem,
}
}),
equals(
'%7Bfilters%3A%20%7Bname%3A%20foo%2C%20example%3A%20lorem%7D%7D',
),
Utils.encode(const Undefined()),
equals(''),
);
});

Expand Down

0 comments on commit e2bdcf5

Please sign in to comment.