Skip to content

Commit 3e7b36f

Browse files
committed
🐛 fix list parsing behavior
1 parent 8af5451 commit 3e7b36f

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

lib/src/qs.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,16 @@ final class QS {
3737
return <String, dynamic>{};
3838
}
3939

40-
Map<String, dynamic>? tempObj = input is String
40+
final Map<String, dynamic>? tempObj = input is String
4141
? _$Decode._parseQueryStringValues(input, options)
4242
: input;
43+
44+
if (options.parseLists &&
45+
options.listLimit > 0 &&
46+
(tempObj?.length ?? 0) > options.listLimit) {
47+
options = options.copyWith(parseLists: false);
48+
}
49+
4350
Map<String, dynamic> obj = {};
4451

4552
// Iterate over the keys and setup the new object

lib/src/utils.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ final class Utils {
4242
target_[target_.length] = source;
4343
}
4444

45-
target = target_.values.any((el) => el is Undefined)
45+
target = options?.parseLists == false &&
46+
target_.values.any((el) => el is Undefined)
4647
? SplayTreeMap.from({
4748
for (final MapEntry<int, dynamic> entry in target_.entries)
4849
if (entry.value is! Undefined) entry.key: entry.value,

test/unit/decode_test.dart

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ void main() {
505505
expect(
506506
QS.decode('a[1]=b&a=c', const DecodeOptions(listLimit: 20)),
507507
equals({
508-
'a': {1: 'b', 2: 'c'}
508+
'a': ['b', 'c']
509509
}),
510510
);
511511
expect(
@@ -600,6 +600,30 @@ void main() {
600600
'a': ['c']
601601
}),
602602
);
603+
expect(
604+
QS.decode('a[0]=b&a[2]=c', const DecodeOptions(parseLists: false)),
605+
equals({
606+
'a': {'0': 'b', '2': 'c'}
607+
}),
608+
);
609+
expect(
610+
QS.decode('a[0]=b&a[2]=c', const DecodeOptions(parseLists: true)),
611+
equals({
612+
'a': ['b', 'c'],
613+
}),
614+
);
615+
expect(
616+
QS.decode('a[1]=b&a[15]=c', const DecodeOptions(parseLists: false)),
617+
equals({
618+
'a': {'1': 'b', '15': 'c'}
619+
}),
620+
);
621+
expect(
622+
QS.decode('a[1]=b&a[15]=c', const DecodeOptions(parseLists: true)),
623+
equals({
624+
'a': ['b', 'c']
625+
}),
626+
);
603627
});
604628

605629
test('limits specific list indices to listLimit', () {
@@ -922,7 +946,7 @@ void main() {
922946
expect(
923947
QS.decode('a[10]=1&a[2]=2', const DecodeOptions(listLimit: 20)),
924948
equals({
925-
'a': {2: '2', 10: '1'}
949+
'a': ['2', '1']
926950
}),
927951
);
928952
expect(

test/unit/uri_extension_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ void main() {
439439
Uri.parse('$testUrl?a[1]=b&a=c')
440440
.queryParametersQs(const DecodeOptions(listLimit: 20)),
441441
equals({
442-
'a': {1: 'b', 2: 'c'}
442+
'a': ['b', 'c']
443443
}),
444444
);
445445
expect(
@@ -864,7 +864,7 @@ void main() {
864864
Uri.parse('$testUrl?a[10]=1&a[2]=2')
865865
.queryParametersQs(const DecodeOptions(listLimit: 20)),
866866
equals({
867-
'a': {2: '2', 10: '1'}
867+
'a': ['2', '1']
868868
}),
869869
);
870870
expect(

0 commit comments

Comments
 (0)