Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix incorrect parsing of nested params with closing square bracket ] in the property name #12

Merged
merged 1 commit into from Apr 27, 2024

Conversation

techouse
Copy link
Owner

@techouse techouse commented Apr 27, 2024

Description

Fixes incorrect parsing of nested params with closing square bracket ] in the property name.

The regular expression you're currently in use,

RegExp(r'(\[[^[\]]*])');

matches a left bracket [, followed by any number of characters that are not brackets, and then a right bracket ]. This is why it matches the innermost brackets.

To match the outermost brackets, including nested brackets, we must use a recursive regular expression. However, Dart's built-in RegExp module doesn't support recursive regular expressions, so we must use the recursive_regex module instead, which is a third-party module that provides more advanced regular expression features.

import 'package:recursive_regex/recursive_regex.dart';

String? matchOutermostBrackets(String s) {
  final RegExp pattern = RecursiveRegex(
    startDelimiter: '[',
    endDelimiter: ']',
  );
  final Match? match = pattern.firstMatch(s);
  return match?.group(0);
}

void main() {
  print(matchOutermostBrackets('foo[bar[]]'));  // Outputs: [bar[]]
}

Fixes ljharb/qs#493

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • tested encoding and decoding an offending String
import 'package:qs_dart/qs_dart.dart';
import 'package:test/test.dart';

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));
});

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@techouse techouse added the bug Something isn't working label Apr 27, 2024
@techouse techouse self-assigned this Apr 27, 2024
Copy link

coderabbitai bot commented Apr 27, 2024

Walkthrough

The update involves a significant change in the decoding process of the qs_dart library by introducing a RecursiveRegex class to handle complex nested parameters in query strings. This change aims to correct issues with parsing parameters that include brackets in their names, enhancing the library's accuracy and functionality.

Changes

File Change Summary
.../extensions/decode.dart Replaced RegExp with RecursiveRegex for improved delimiter handling.
.../qs.dart Added import for recursive_regex.
pubspec.yaml Added recursive_regex dependency.
.../fixed_qs_issues_test.dart Introduced a test case for encoding and decoding query strings.

Assessment against linked issues

Objective Addressed Explanation
Incorrect parsing of nested params with closing square bracket in property name (#493)

Poem

🐇 Oh hop and code, through fields we roam,
To fix the bugs where brackets roam.
A regex twist, a clever plot,
Nested queries, fear them not.
🌼 With every line, the garden's sown,
In code we trust, in fields we've grown.


Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 160ece1 and c57af24.
Files selected for processing (4)
  • lib/src/extensions/decode.dart (2 hunks)
  • lib/src/qs.dart (1 hunks)
  • pubspec.yaml (1 hunks)
  • test/unit/fixed_qs_issues_test.dart (1 hunks)
Files skipped from review due to trivial changes (1)
  • pubspec.yaml
Additional comments not posted (3)
test/unit/fixed_qs_issues_test.dart (1)

6-17: The test case effectively checks the basic scenario of encoding and decoding with nested brackets. Consider adding more complex nested structures and edge cases to ensure robustness.

lib/src/qs.dart (1)

13-13: The import of recursive_regex is correctly added to support the new parsing functionality.

lib/src/extensions/decode.dart (1)

162-165: The introduction of RecursiveRegex for handling nested brackets is correctly implemented. Consider conducting performance tests to ensure that the new parsing logic does not introduce significant overhead.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Apr 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.08%. Comparing base (160ece1) to head (c57af24).

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #12   +/-   ##
=======================================
  Coverage   98.08%   98.08%           
=======================================
  Files          13       13           
  Lines         625      625           
=======================================
  Hits          613      613           
  Misses         12       12           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@techouse techouse merged commit 6a6a3df into main Apr 27, 2024
6 checks passed
@techouse techouse deleted the fix/qs-issue-493 branch April 27, 2024 19:09
techouse added a commit that referenced this pull request Apr 27, 2024
## 1.0.9

- [FIX] incorrect parsing of nested params with closing square bracket `]` in the property name (#12)
@techouse techouse mentioned this pull request Apr 27, 2024
techouse added a commit that referenced this pull request Apr 27, 2024
## 1.0.9

- [FIX] incorrect parsing of nested params with closing square bracket `]` in the property name (#12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect parsing of nested params with closing square bracket ] in property name
1 participant