Skip to content

feat: add sanitize_url jinja macro#571

Merged
Patrick Nilan (pnilan) merged 2 commits intomainfrom
pnilan/feat/add-sanitize-jinja-macro
May 30, 2025
Merged

feat: add sanitize_url jinja macro#571
Patrick Nilan (pnilan) merged 2 commits intomainfrom
pnilan/feat/add-sanitize-jinja-macro

Conversation

@pnilan
Copy link
Contributor

@pnilan Patrick Nilan (pnilan) commented May 29, 2025

What

  • Adds sanitize jinja macro which defaults to urllib.parse quote_plus sanitization.

Summary by CodeRabbit

  • New Features

    • Introduced a new macro for URL sanitization, allowing users to easily encode strings for safe URL usage in Jinja interpolation contexts.
  • Tests

    • Added comprehensive tests to ensure correct URL encoding and handling of special characters by the new macro.

@github-actions github-actions bot added the enhancement New feature or request label May 29, 2025
Copy link
Contributor

@brianjlai Brian Lai (brianjlai) left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@pnilan Patrick Nilan (pnilan) marked this pull request as ready for review May 30, 2025 03:20
@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 30, 2025

📝 Walkthrough

Walkthrough

A new macro function, sanitize_url, was introduced to the macros module for URL encoding strings using urllib.parse.quote_plus. The function is now available in Jinja interpolation contexts. Corresponding parameterized tests were added to verify its behavior with various input cases.

Changes

File(s) Change Summary
airbyte_cdk/sources/declarative/interpolation/macros.py Added sanitize_url macro function for URL encoding; updated macro exports to include it.
unit_tests/sources/declarative/interpolation/test_macros.py Added parameterized tests for sanitize_url covering multiple encoding scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant JinjaContext
    participant sanitize_url

    User->>JinjaContext: Uses {{ sanitize_url(value) }} in template
    JinjaContext->>sanitize_url: Calls sanitize_url(value)
    sanitize_url-->>JinjaContext: Returns URL-encoded string
    JinjaContext-->>User: Renders template with sanitized value
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 explain this code block.
    • @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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
airbyte_cdk/sources/declarative/interpolation/macros.py (1)

186-194: Consider some refinements to improve clarity and consistency, wdyt?

A few suggestions for the function implementation:

  1. Naming consistency: The function is called sanitize_url but it works on any string (as shown in the tests with "hello world"). Would sanitize_string or url_encode be more accurate?

  2. Simplify implementation: The intermediate sanitization_strategy variable seems unnecessary. Could we directly return quote_plus(value)?

  3. Fix docstring grammar: "Sanitizes a value by via urllib.parse.quote_plus" has redundant words. How about "Sanitizes a value using urllib.parse.quote_plus"?

Here's a potential refinement:

-def sanitize_url(value: str) -> str:
+def url_encode(value: str) -> str:
     """
-    Sanitizes a value by via urllib.parse.quote_plus
+    URL-encodes a string using urllib.parse.quote_plus
 
     Usage:
-    `"{{ sanitize_url('https://example.com/path?query=value') }}"`
+    `"{{ url_encode('https://example.com/path?query=value') }}"`
     """
-    sanitization_strategy = quote_plus
-    return sanitization_strategy(value)
+    return quote_plus(value)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7818b33 and 8314c66.

📒 Files selected for processing (2)
  • airbyte_cdk/sources/declarative/interpolation/macros.py (3 hunks)
  • unit_tests/sources/declarative/interpolation/test_macros.py (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
unit_tests/sources/declarative/interpolation/test_macros.py (1)
airbyte_cdk/sources/declarative/interpolation/macros.py (1)
  • sanitize_url (186-194)
🔇 Additional comments (3)
airbyte_cdk/sources/declarative/interpolation/macros.py (2)

9-9: Import addition looks good!

The import of quote_plus is correctly placed and necessary for the new functionality.


208-208: Function registration is correct!

Properly added to the _macros_list following the existing pattern.

unit_tests/sources/declarative/interpolation/test_macros.py (1)

227-251: Excellent test coverage!

The parameterized tests cover the key scenarios well:

  • Basic URL encoding
  • Space handling (converted to +)
  • Special character encoding (&, +, #)
  • Non-URL string handling

The expected outputs match quote_plus behavior correctly. One small question: would it be worth adding an edge case test for empty string input, wdyt? Though the current coverage is quite comprehensive.

@pnilan Patrick Nilan (pnilan) merged commit 5f98bd2 into main May 30, 2025
29 checks passed
@pnilan Patrick Nilan (pnilan) deleted the pnilan/feat/add-sanitize-jinja-macro branch May 30, 2025 03:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants