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 #1

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,

re.compile(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, Python's built-in re module doesn't support recursive regular expressions, so we must use the regex module instead, which is a third-party module that provides more advanced regular expression features.

import regex

def match_outermost_brackets(s):
    pattern = r"\[(?:[^\[\]]|(?R))*\]"
    match = regex.search(pattern, s)
    return match.group() if match else None

print(match_outermost_brackets("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?

  • test_qs_issue_493
def test_qs_issue_493(self) -> None:
    """Test case for https://github.com/ljharb/qs/issues/493"""
    original: t.Dict[str, t.Any] = {"search": {"withbracket[]": "foobar"}}
    encoded: str = "search[withbracket[]]=foobar"
    assert encode(original, options=EncodeOptions(encode=False)) == encoded
    assert decode(encoded) == 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 recent changes involve enhancing the qs_codec library to better handle query strings with complex nested parameters. This includes switching to the regex library for improved pattern matching capabilities and updating dependencies to ensure compatibility and robustness in testing environments.

Changes

File(s) Change Summary
pyproject.toml, requirements_dev.txt Added/updated dependencies for regex and pytest.
src/qs_codec/decode.py Replaced re with regex for pattern matching; updated type annotations.
tests/unit/fixed_qs_issues_test.py Added new tests to ensure correct parsing of query strings with special characters in brackets.

Assessment against linked issues

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

🐰🎉
A hop, a skip, a coding fix,
Nested brackets, regex tricks.
Query strings now parse just right,
Tests ensure all is tight.
Cheers to code that ticks! 🥕
🎉🐰


Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 7fbc314 and 380513a.
Files selected for processing (4)
  • pyproject.toml (1 hunks)
  • requirements_dev.txt (1 hunks)
  • src/qs_codec/decode.py (3 hunks)
  • tests/unit/fixed_qs_issues_test.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • pyproject.toml
Additional Context Used
LanguageTool (6)
requirements_dev.txt (6)

Near line 1: This sentence does not start with an uppercase letter.
Context: regex>=2024.4.16 types-regex>=2024.4.16.20240...


Near line 2: Possible spelling mistake found.
Context: ...24.4.16 types-regex>=2024.4.16.20240423 pytest>=8.1.2 pytest-cov>=5.0.0 mypy>=1.10.0 t...


Near line 3: Possible spelling mistake found.
Context: ...regex>=2024.4.16.20240423 pytest>=8.1.2 pytest-cov>=5.0.0 mypy>=1.10.0 toml>=0.10.2 tox


Near line 4: Possible spelling mistake found.
Context: ...0240423 pytest>=8.1.2 pytest-cov>=5.0.0 mypy>=1.10.0 toml>=0.10.2 tox


Near line 5: Possible spelling mistake found.
Context: ...t>=8.1.2 pytest-cov>=5.0.0 mypy>=1.10.0 toml>=0.10.2 tox


Near line 6: Possible spelling mistake found.
Context: ...st-cov>=5.0.0 mypy>=1.10.0 toml>=0.10.2 tox

Additional comments not posted (7)
requirements_dev.txt (4)

1-1: Dependency specification for regex is correct and appropriately versioned.


2-2: Dependency specification for types-regex is correct and appropriately versioned.


3-3: Dependency specification for pytest is correct and appropriately versioned.


Line range hint 4-7: All remaining dependencies are correctly specified and appropriately versioned.

tests/unit/fixed_qs_issues_test.py (1)

6-14: The test method test_qs_issue_493 is well-implemented and covers the encoding and decoding functionalities as expected for the issue.

src/qs_codec/decode.py (2)

7-7: The import and usage of the regex module are correctly implemented to handle complex patterns with recursive regular expressions.


Line range hint 164-178: The changes in the _parse_keys function correctly utilize the regex module to handle nested brackets using recursive regular expressions. This is a key improvement for resolving the parsing issue.


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.

@techouse techouse changed the title 🐛 fix https://github.com/ljharb/qs/issues/493 🐛 fix incorrect parsing of nested params with closing square bracket ] in the property name Apr 27, 2024
Copy link

codecov bot commented Apr 27, 2024

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@techouse techouse merged commit 1740271 into main Apr 27, 2024
11 of 12 checks passed
@techouse techouse deleted the fix/qs-issue-493 branch April 27, 2024 18:28
techouse added a commit that referenced this pull request Apr 27, 2024
## 0.1.4

* [FIX] incorrect parsing of nested params with closing square bracket `]` in the property name ([#1](#1))
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