Skip to content

Commit

Permalink
🐛 fix ljharb/qs#493
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Apr 27, 2024
1 parent 160ece1 commit c57af24
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/src/extensions/decode.dart
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/qs.dart
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions 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<String, dynamic> 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));
});
});
}

0 comments on commit c57af24

Please sign in to comment.