Skip to content

feat: check for request_option mapping conflicts in individual components #328

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

Merged
merged 7 commits into from
Feb 14, 2025

Conversation

ChristoGrab
Copy link
Collaborator

@ChristoGrab ChristoGrab commented Feb 10, 2025

What

Although we check for duplicate keys when combining all request option mappings in a retriever, individual components that define more than one request option lack validations to confirm that there are no key mapping errors. Therefore, we currently override values when a conflict occurs, ie:

## Inputs ##
paginator.page_size_option: ["variables", "limit"] = 5
paginator.page_token_option: ["variables", "limit"] = 1

## Outputs ##
Expected: "ValueError: Duplicate keys found..."
Actual: {"variables": { "limit": 1 } }

This PR adds a new validation specifically for components that allow multiple request options, to ensure that mappings are combined and checked for each component individually.

How

  • Implements a new helper function _validate_component_request_option_paths. This function takes an arbitrary number of request option keys and reuses the existing combine_mappings implementation to check for duplicate keys. A dict with dummy values is constructed from the component's keys (since the actual values may not be available at time of init). Resolves #11580
  • Updates the duplicate key error message for clarity. Resolves #11581

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced configuration validation for time-based queries and pagination, ensuring request options are correctly set before execution and improving overall data retrieval accuracy.
  • Bug Fixes

    • Upgraded error messaging to provide clearer feedback when configuration issues arise, making it easier for users to identify and resolve duplicate or conflicting settings.

@github-actions github-actions bot added the enhancement New feature or request label Feb 10, 2025
@ChristoGrab ChristoGrab marked this pull request as ready for review February 10, 2025 22:27
Copy link
Contributor

coderabbitai bot commented Feb 10, 2025

📝 Walkthrough

Walkthrough

The changes introduce new validation functionality across the codebase. In the initialization methods of two classes, new import statements and calls to a validation helper have been added to verify request options. Additionally, the utility module has been modified to enhance error messages and introduce a new validation function, while the test suite has been expanded to cover these validation scenarios with additional tests and fixtures.

Changes

File(s) Change Summary
airbyte_cdk/sources/declarative/.../datetime_based_cursor.py
airbyte_cdk/sources/declarative/.../default_paginator.py
Added import of _validate_component_request_option_paths in the __post_init__ methods to validate request options. In default_paginator.py, also added validation for page_token_option.
airbyte_cdk/utils/mapping_helpers.py Updated _merge_mappings to provide detailed error messages and added a new method _validate_component_request_option_paths for validating multiple request option paths.
unit_tests/utils/test_mapping_helpers.py Expanded the test suite with new imports, parameterized tests (test_request_option_validation and test_edge_cases), and a new fixture (mock_config) to verify request option validations.
unit_tests/sources/declarative/requesters/paginators/test_default_paginator.py Added test_request_option_mapping_validator to validate behavior when page_size_option and page_token_option conflict.

Sequence Diagram(s)

sequenceDiagram
    participant DTC as DatetimeBasedCursor
    participant MH  as mapping_helpers
    DTC->>DTC: __post_init__()
    DTC->>MH: _validate_component_request_option_paths(config, start_time_option, end_time_option)
    MH-->>DTC: Return validation result
Loading
sequenceDiagram
    participant DP as DefaultPaginator
    participant MH as mapping_helpers
    DP->>DP: __post_init__()
    alt page_token_option provided & not RequestPath
        DP->>MH: _validate_component_request_option_paths(config, page_size_option, page_token_option)
        MH-->>DP: Return validation result
    else No validation required
        DP-->>DP: Continue initialization
    end
Loading

Possibly related PRs

Suggested labels

bug

Suggested reviewers

  • brianjlai
  • maxi297
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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:

‼️ 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 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 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 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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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. (Beta)
  • @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 (6)
airbyte_cdk/utils/mapping_helpers.py (3)

43-44: Consider enhancing the error message for better clarity?

The error message is good but could be even more helpful by including the conflicting values. What do you think about:

-        f"Request body collision, duplicate keys detected at key path: {'.'.join(current_path)}. Please ensure that all keys in the request are unique."
+        f"Request body collision at key path: {'.'.join(current_path)}. Found duplicate keys with values '{source_value}' and '{target_value}'. Please ensure that all keys in the request are unique."

Also applies to: 51-52


117-146: Consider adding type hints and additional error handling?

The function looks good! A few suggestions to make it even better:

  1. Type hint for grouped_options could be more specific:
-    grouped_options: Dict[RequestOptionType, List[RequestOption]] = {}
+    grouped_options: Dict[RequestOptionType, List[RequestOption]] = defaultdict(list)
  1. The error handling could be more specific:
-        except ValueError as error:
-            raise ValueError(error)
+        except ValueError as error:
+            raise ValueError(f"Invalid request option configuration: {error}")

What do you think about these changes? They would make the code more type-safe and provide better error context.


120-123: Consider adding usage examples to the docstring?

The docstring explains the purpose well, but would it be helpful to add an example showing how the function is used? Something like:

"""
Validates that a component with multiple request options does not have conflicting paths.
Uses dummy values for validation since actual values might not be available at init time.

Example:
    >>> config = {"api_key": "123"}
    >>> option1 = RequestOption(field_name="page", inject_into=RequestOptionType.request_parameter)
    >>> option2 = RequestOption(field_name="limit", inject_into=RequestOptionType.request_parameter)
    >>> _validate_component_request_option_paths(config, option1, option2)  # No conflict
"""

What do you think about adding this example to help future developers?

unit_tests/utils/test_mapping_helpers.py (1)

194-219: Consider adding more edge cases to test?

The edge case tests look good! Would it be valuable to add a few more cases:

  1. Mixed request option types with same field names
  2. Deeply nested JSON paths
  3. Special characters in field names

Something like:

@pytest.mark.parametrize(
    "test_name, options",
    [
        (
            "mixed_types_same_field",
            [
                RequestOption(field_name="page", inject_into=RequestOptionType.header),
                RequestOption(field_name="page", inject_into=RequestOptionType.request_parameter),
            ],
        ),
        (
            "deep_nested_json",
            [
                RequestOption(field_path=["data", "meta", "pagination", "next"], inject_into=RequestOptionType.body_json),
                RequestOption(field_path=["data", "meta", "pagination", "prev"], inject_into=RequestOptionType.body_json),
            ],
        ),
        (
            "special_chars",
            [
                RequestOption(field_name="x-api-key", inject_into=RequestOptionType.header),
                RequestOption(field_name="x-client-id", inject_into=RequestOptionType.header),
            ],
        ),
    ],
)

What do you think about adding these cases to make the tests more robust?

airbyte_cdk/sources/declarative/requesters/paginators/default_paginator.py (1)

120-125: Consider adding a comment explaining the validation?

The validation looks good! Would it be helpful to add a brief comment explaining why we validate only when page_token_option is not a RequestPath? Something like:

# Validate request options only when page_token_option is a RequestOption
# RequestPath options are handled differently and don't need path conflict validation
if self.page_token_option and not isinstance(self.page_token_option, RequestPath):
    _validate_component_request_option_paths(
        self.config,
        self.page_size_option,
        self.page_token_option,
    )

What do you think about adding this context for future maintainers?

airbyte_cdk/sources/declarative/incremental/datetime_based_cursor.py (1)

126-128: Consider adding a test for the validation?

The validation looks good! Would it be valuable to add a test case in the corresponding test file to verify this validation? Something like:

def test_datetime_based_cursor_validation():
    """Test that the cursor properly validates request options."""
    with pytest.raises(ValueError, match="duplicate keys detected"):
        DatetimeBasedCursor(
            start_datetime="2021-01-01",
            cursor_field="created_at",
            datetime_format="%Y-%m-%d",
            config={},
            parameters={},
            start_time_option=RequestOption(field_name="time", inject_into=RequestOptionType.request_parameter),
            end_time_option=RequestOption(field_name="time", inject_into=RequestOptionType.request_parameter),
        )

What do you think about adding this test to ensure the validation works as expected?

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 74631d8 and e00aed2.

📒 Files selected for processing (4)
  • airbyte_cdk/sources/declarative/incremental/datetime_based_cursor.py (2 hunks)
  • airbyte_cdk/sources/declarative/requesters/paginators/default_paginator.py (2 hunks)
  • airbyte_cdk/utils/mapping_helpers.py (3 hunks)
  • unit_tests/utils/test_mapping_helpers.py (4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Check: 'source-the-guardian-api' (skip=false)
  • GitHub Check: Check: 'source-shopify' (skip=false)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)

@ChristoGrab ChristoGrab requested a review from maxi297 February 10, 2025 22:34
Copy link
Contributor

@maxi297 maxi297 left a comment

Choose a reason for hiding this comment

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

nit: it feels like we should have a test for datetime_based_cursor and default_paginator. I don't mind not doing the cursor one because we are slowly moving away from this implementation. I'll let you decide if it's worth for default_paginator

The rest LGTM! Thanks @ChristoGrab

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)
unit_tests/sources/declarative/requesters/paginators/test_default_paginator.py (1)

452-475: The test looks good! Here are some suggestions to make it even better.

The test effectively validates the request option mapping conflict detection. Would you consider these improvements? wdyt?

  1. Rename the test to be more descriptive, e.g., test_raises_error_when_page_options_have_conflicting_paths
  2. Add more test cases using @pytest.mark.parametrize to cover different field path conflicts
  3. Assert the error message content to ensure it's helpful for debugging

Here's a possible refactor:

-def test_request_option_mapping_validator():
+@pytest.mark.parametrize(
+    "size_path,token_path,expected_error",
+    [
+        (["variables", "limit"], ["variables", "limit"], "Conflicting paths found"),
+        (["body", "page"], ["body", "page"], "Conflicting paths found"),
+        (["headers", "x-page"], ["headers", "x-page"], "Conflicting paths found"),
+    ],
+    ids=["json_body_conflict", "form_data_conflict", "header_conflict"]
+)
+def test_raises_error_when_page_options_have_conflicting_paths(size_path, token_path, expected_error):
     pagination_strategy = PageIncrement(
         config={}, page_size=1, start_from_page=0, parameters={}, inject_on_first_request=True
     )

-    with pytest.raises(ValueError):
+    with pytest.raises(ValueError, match=expected_error):
         (
             DefaultPaginator(
                 page_size_option=RequestOption(
-                    field_path=["variables", "limit"],
+                    field_path=size_path,
                     inject_into=RequestOptionType.body_json,
                     parameters={},
                 ),
                 page_token_option=RequestOption(
-                    field_path=["variables", "limit"],
+                    field_path=token_path,
                     inject_into=RequestOptionType.body_json,
                     parameters={},
                 ),
                 pagination_strategy=pagination_strategy,
                 config=MagicMock(),
                 url_base=MagicMock(),
                 parameters={},
             ),
         )
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4c13d07 and 0ef0efc.

📒 Files selected for processing (1)
  • unit_tests/sources/declarative/requesters/paginators/test_default_paginator.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Check: 'source-pokeapi' (skip=false)
  • GitHub Check: Check: 'source-the-guardian-api' (skip=false)
  • GitHub Check: Check: 'source-shopify' (skip=false)
  • GitHub Check: Check: 'source-hardcoded-records' (skip=false)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (Fast)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)
  • GitHub Check: Analyze (python)

@ChristoGrab ChristoGrab merged commit 8499e24 into main Feb 14, 2025
23 checks passed
@ChristoGrab ChristoGrab deleted the christo/fix-duplicate-mappings-check branch February 14, 2025 20:30
rpopov added a commit to rpopov/airbyte-python-cdk that referenced this pull request Mar 5, 2025
* main:
  fix: update cryptography package to latest version to address CVE (airbytehq#377)
  fix: (CDK) (HttpRequester) - Make the `HttpRequester.path` optional (airbytehq#370)
  feat: improved custom components handling (airbytehq#350)
  feat: add microseconds timestamp format (airbytehq#373)
  fix: Replace Unidecode with anyascii for permissive license (airbytehq#367)
  feat: add IncrementingCountCursor (airbytehq#346)
  feat: (low-code cdk)  datetime format with milliseconds (airbytehq#369)
  fix: (CDK) (AsyncRetriever) - Improve UX on variable naming and interpolation (airbytehq#368)
  fix: (CDK) (AsyncRetriever) - Add the `request` and `response` to each `async` operations (airbytehq#356)
  fix: (CDK) (ConnectorBuilder) - Add `auxiliary requests` to slice; support `TestRead` for AsyncRetriever (part 1/2) (airbytehq#355)
  feat(concurrent perpartition cursor): Add parent state updates (airbytehq#343)
  fix: update csv parser for builder compatibility (airbytehq#364)
  feat(low-code cdk): add interpolation for limit field in Rate (airbytehq#353)
  feat(low-code cdk): add AbstractStreamFacade processing as concurrent streams in declarative source (airbytehq#347)
  fix: (CDK) (CsvParser) - Fix the `\\` escaping when passing the `delimiter` from Builder's UI (airbytehq#358)
  feat: expose `str_to_datetime` jinja macro (airbytehq#351)
  fix: update CDK migration for 6.34.0 (airbytehq#348)
  feat: Removes `stream_state` interpolation from CDK (airbytehq#320)
  fix(declarative): Pass `extra_fields` in `global_substream_cursor` (airbytehq#195)
  feat(concurrent perpartition cursor): Refactor ConcurrentPerPartitionCursor (airbytehq#331)
  feat(HttpMocker): adding support for PUT requests and bytes responses (airbytehq#342)
  chore: use certified source for manifest-only test (airbytehq#338)
  feat: check for request_option mapping conflicts in individual components (airbytehq#328)
  feat(file-based): sync file acl permissions and identities (airbytehq#260)
  fix: (CDK) (Connector Builder) - refactor the `MessageGrouper` > `TestRead` (airbytehq#332)
  fix(low code): Fix missing cursor for ClientSideIncrementalRecordFilterDecorator (airbytehq#334)
  feat(low-code): Add API Budget (airbytehq#314)
  chore(decoder): clean decoders and make csvdecoder available (airbytehq#326)
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