Skip to content

Commit

Permalink
🐛 fix decode: properly account for strictNullHandling when `allowEm…
Browse files Browse the repository at this point in the history
…ptyLists` (#21)
  • Loading branch information
techouse authored Jul 16, 2024
1 parent 887af0e commit d565c16
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/src/extensions/decode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ extension _$Decode on QS {
final String root = chain[i];

if (root == '[]' && options.parseLists) {
obj = options.allowEmptyLists && leaf == ''
obj = options.allowEmptyLists &&
(leaf == '' || (options.strictNullHandling && leaf == null))
? List<dynamic>.empty(growable: true)
: [if (leaf is Iterable) ...leaf else leaf];
} else {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/decode_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@ void main() {
);
});

test(
'allowEmptyLists + strictNullHandling',
() {
expect(
QS.decode(
'testEmptyList[]',
const DecodeOptions(
strictNullHandling: true,
allowEmptyLists: true,
),
),
equals({'testEmptyList': []}),
);
},
);

test('parses a single nested string', () {
expect(
QS.decode('a[b]=c'),
Expand Down
16 changes: 16 additions & 0 deletions test/unit/encode_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,22 @@ void main() {
},
);

test(
'allowEmptyLists + strictNullHandling',
() {
expect(
QS.encode(
{'testEmptyList': []},
const EncodeOptions(
strictNullHandling: true,
allowEmptyLists: true,
),
),
equals('testEmptyList[]'),
);
},
);

group('encodes a list value with one item vs multiple items', () {
test(
'non-list item',
Expand Down

0 comments on commit d565c16

Please sign in to comment.