From 6a6a3df8ab9a16baa4f395894daaa71eb3d71b6a Mon Sep 17 00:00:00 2001 From: Klemen Tusar Date: Sat, 27 Apr 2024 20:09:56 +0100 Subject: [PATCH] :bug: fix https://github.com/ljharb/qs/issues/493 (#12) --- lib/src/extensions/decode.dart | 7 +++++-- lib/src/qs.dart | 1 + pubspec.yaml | 1 + test/unit/fixed_qs_issues_test.dart | 19 +++++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/unit/fixed_qs_issues_test.dart diff --git a/lib/src/extensions/decode.dart b/lib/src/extensions/decode.dart index 6a0dbd5..f16fee8 100644 --- a/lib/src/extensions/decode.dart +++ b/lib/src/extensions/decode.dart @@ -159,7 +159,10 @@ extension _$Decode on QS { : givenKey!; // The regex chunks - final RegExp brackets = RegExp(r'(\[[^[\]]*])'); + final RegExp brackets = RecursiveRegex( + startDelimiter: '[', + endDelimiter: ']', + ); // Get the parent Match? segment = options.depth > 0 ? brackets.firstMatch(key) : null; @@ -178,7 +181,7 @@ extension _$Decode on QS { i < options.depth) { i += 1; if (segment != null) { - keys.add(segment.group(1)!); + keys.add(segment.group(0)!); // Update the key to start searching from the next position key = key.slice(segment.end); } diff --git a/lib/src/qs.dart b/lib/src/qs.dart index a9ebbac..9cb90e3 100644 --- a/lib/src/qs.dart +++ b/lib/src/qs.dart @@ -10,6 +10,7 @@ import 'package:qs_dart/src/models/decode_options.dart'; import 'package:qs_dart/src/models/encode_options.dart'; import 'package:qs_dart/src/models/undefined.dart'; import 'package:qs_dart/src/utils.dart'; +import 'package:recursive_regex/recursive_regex.dart'; import 'package:weak_map/weak_map.dart'; part 'extensions/decode.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index 2cdf3ba..1f856db 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,6 +10,7 @@ dependencies: collection: ^1.18.0 equatable: ^2.0.5 meta: ^1.9.1 + recursive_regex: ^1.0.0 weak_map: ^3.0.1 dev_dependencies: diff --git a/test/unit/fixed_qs_issues_test.dart b/test/unit/fixed_qs_issues_test.dart new file mode 100644 index 0000000..a2c5e7e --- /dev/null +++ b/test/unit/fixed_qs_issues_test.dart @@ -0,0 +1,19 @@ +import 'package:qs_dart/qs_dart.dart'; +import 'package:test/test.dart'; + +void main() { + group('fixed ljharb/qs issues', () { + test('ljharb/qs#493', () { + final Map original = { + 'search': {'withbracket[]': 'foobar'} + }; + final String encoded = 'search[withbracket[]]=foobar'; + + expect( + QS.encode(original, EncodeOptions(encode: false)), + equals(encoded), + ); + expect(QS.decode(encoded), equals(original)); + }); + }); +}