From bb04b629ec248adc752390eb315c455148de37ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Tue, 16 Jul 2024 22:07:29 +0100 Subject: [PATCH] :bug: fix decode: properly account for `strictNullHandling` when `allowEmptyLists` --- lib/src/extensions/decode.dart | 3 ++- test/unit/decode_test.dart | 16 ++++++++++++++++ test/unit/encode_test.dart | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/src/extensions/decode.dart b/lib/src/extensions/decode.dart index 480e12c..edfac83 100644 --- a/lib/src/extensions/decode.dart +++ b/lib/src/extensions/decode.dart @@ -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.empty(growable: true) : [if (leaf is Iterable) ...leaf else leaf]; } else { diff --git a/test/unit/decode_test.dart b/test/unit/decode_test.dart index 1e54804..bf49d17 100644 --- a/test/unit/decode_test.dart +++ b/test/unit/decode_test.dart @@ -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'), diff --git a/test/unit/encode_test.dart b/test/unit/encode_test.dart index df751ea..96f1f1a 100644 --- a/test/unit/encode_test.dart +++ b/test/unit/encode_test.dart @@ -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',