Skip to content

fix: dpath_validator should not raise when key does not exist #591

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 2 commits into from
Jun 9, 2025

Conversation

pnilan
Copy link
Contributor

@pnilan pnilan commented Jun 9, 2025

What

  • Fixes bug in DpathValidator where KeyErrors were not properly skipped.

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling during validation by logging warnings instead of raising exceptions when keys are missing, resulting in a smoother user experience.
  • Tests

    • Updated and simplified validation tests to reflect the new error handling approach, removing tests that expected exceptions for missing keys.

Important

Auto-merge enabled.

This PR is set to merge automatically when all requirements are met.

@pnilan pnilan marked this pull request as ready for review June 9, 2025 19:46
@github-actions github-actions bot added bug Something isn't working security labels Jun 9, 2025
@Copilot Copilot AI review requested due to automatic review settings June 9, 2025 19:47
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR ensures that DpathValidator no longer raises on missing keys, and adjusts tests accordingly.

  • Replace ValueError on KeyError with a warning + early return in DpathValidator.
  • Update unit tests to verify successful validation on top-level paths and remove obsolete error-raising tests.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
unit_tests/sources/declarative/validators/test_dpath_validator.py Restructure test class, add top-level path test, remove KeyError tests
airbyte_cdk/sources/declarative/validators/dpath_validator.py Catch KeyError, log a warning, and skip missing keys instead of raising
Comments suppressed due to low confidence (3)

unit_tests/sources/declarative/validators/test_dpath_validator.py:34

  • This test method appears unindented (outside of any TestCase subclass) after the refactor. It should be nested under class TestDpathValidator or adjusted to run as a standalone pytest function.
def test_given_valid_path_and_input_validate_is_successful(self):

unit_tests/sources/declarative/validators/test_dpath_validator.py:38

  • TestCase is not imported in this file. Please add from unittest import TestCase at the top to avoid a NameError.
class TestDpathValidator(TestCase):

Copy link
Contributor

coderabbitai bot commented Jun 9, 2025

📝 Walkthrough

Walkthrough

The changes update the DpathValidator to log warnings instead of raising exceptions when key errors occur during validation. Corresponding unit tests were modified: tests expecting exceptions were removed or adjusted, and a new test was added to verify successful validation when a top-level key is present.

Changes

File(s) Change Summary
airbyte_cdk/sources/declarative/validators/dpath_validator.py Replaced exceptions with logging for missing keys during validation; added logging setup; removed early returns.
unit_tests/sources/declarative/validators/test_dpath_validator.py Removed tests expecting exceptions for missing keys; added/modified tests for successful validation scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant DpathValidator
    participant Logger

    User->>DpathValidator: validate(input_data)
    DpathValidator->>DpathValidator: Check for path/wildcard in input_data
    alt Path not found or wildcard missing
        DpathValidator->>Logger: Log warning
        DpathValidator-->>User: Return (no exception)
    else Path found
        DpathValidator->>DpathValidator: Proceed with validation
        DpathValidator-->>User: Complete validation
    end
Loading

Possibly related PRs

  • airbytehq/airbyte-python-cdk#590: Both PRs modify DpathValidator.validate regarding handling of missing or empty values, but with opposite strategies—one adds early returns, the other removes them and logs warnings.

Suggested labels

airbyte-python-cdk

Suggested reviewers

  • brianjlai

Would you like to consider further tests for nested paths or more granular logging, wdyt?

✨ 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

🔭 Outside diff range comments (1)
unit_tests/sources/declarative/validators/test_dpath_validator.py (1)

25-49: ⚠️ Potential issue

Fix the duplicate class definition!

There are two TestDpathValidator classes defined (lines 25 and 38), which will cause the first class to be overwritten. This is a critical issue that needs immediate attention.

Could you merge these into a single class or rename one of them? The second class (line 38) appears to contain a new test method that should be part of the main test class. wdyt about consolidating them?

-class TestDpathValidator(TestCase):
-    def test_given_valid_path_and_input_validate_is_successful(self):
-        strategy = MockValidationStrategy()
-        validator = DpathValidator(field_path=["user", "profile", "email"], strategy=strategy)
-
-        test_data = {"user": {"profile": {"email": "test@example.com", "name": "Test User"}}}
-
-        validator.validate(test_data)
-
-        assert strategy.validate_called
-        assert strategy.validated_value
-
-
-class TestDpathValidator(TestCase):
-    def test_given_valid_top_level_path_and_input_validate_is_successful(self):
+class TestDpathValidator(TestCase):
+    def test_given_valid_path_and_input_validate_is_successful(self):
+        strategy = MockValidationStrategy()
+        validator = DpathValidator(field_path=["user", "profile", "email"], strategy=strategy)
+
+        test_data = {"user": {"profile": {"email": "test@example.com", "name": "Test User"}}}
+
+        validator.validate(test_data)
+
+        assert strategy.validate_called
+        assert strategy.validated_value
+
+    def test_given_valid_top_level_path_and_input_validate_is_successful(self):
🧰 Tools
🪛 Pylint (3.3.7)

[error] 38-38: class already defined line 25

(E0102)

🧹 Nitpick comments (1)
airbyte_cdk/sources/declarative/validators/dpath_validator.py (1)

55-58: Great improvement in wildcard path error handling!

The change from raising ValueError to logging a warning and returning early is exactly what the PR objectives called for. This makes the validator more robust when dealing with missing keys in wildcard scenarios.

One small question - should we consider logging at debug level instead of warning level for missing keys, since this might be expected behavior in some cases? wdyt?

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 99a1a96 and b1d20f3.

📒 Files selected for processing (2)
  • airbyte_cdk/sources/declarative/validators/dpath_validator.py (2 hunks)
  • unit_tests/sources/declarative/validators/test_dpath_validator.py (2 hunks)
🧰 Additional context used
🪛 Pylint (3.3.7)
unit_tests/sources/declarative/validators/test_dpath_validator.py

[error] 38-38: class already defined line 25

(E0102)

⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: Check: 'source-pokeapi' (skip=false)
  • GitHub Check: Check: 'source-shopify' (skip=false)
  • GitHub Check: Check: 'source-amplitude' (skip=false)
  • GitHub Check: Check: 'source-hardcoded-records' (skip=false)
  • GitHub Check: SDM Docker Image Build
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)
  • GitHub Check: Pytest (Fast)
  • GitHub Check: Analyze (python)
🔇 Additional comments (5)
airbyte_cdk/sources/declarative/validators/dpath_validator.py (3)

5-5: LGTM on the import additions!

The logging import and typing import changes look good and support the new warning-based approach. The imports are clean and necessary for the functionality.

Also applies to: 7-7


15-16: Logger setup looks good!

Using the "airbyte" logger name maintains consistency with the project's logging conventions. The logger placement is appropriate for the module scope.


63-66: Consistent error handling for regular paths!

The KeyError handling here mirrors the wildcard path logic perfectly, maintaining consistency in how missing keys are handled throughout the validator. The early return approach is clean and prevents unnecessary processing.

unit_tests/sources/declarative/validators/test_dpath_validator.py (2)

39-48: Nice addition of top-level path test coverage!

This new test method provides good coverage for validating top-level keys, which complements the existing nested path tests. The test logic is sound and follows the same pattern as other tests.


57-57: Good simplification of the exception test!

Removing the specific exception message check makes sense since the validator now logs warnings instead of raising detailed error messages. The test still properly verifies that strategy failures bubble up as ValueError.

Copy link

github-actions bot commented Jun 9, 2025

PyTest Results (Fast)

3 656 tests   - 2   3 646 ✅  - 2   5m 45s ⏱️ -1s
    1 suites ±0      10 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit b1d20f3. ± Comparison against base commit 99a1a96.

This pull request removes 3 and adds 1 tests. Note that renamed tests count towards both.
unit_tests.sources.declarative.validators.test_dpath_validator.TestDpathValidator ‑ test_given_empty_input_data_when_validate_then_validate_raises_exception
unit_tests.sources.declarative.validators.test_dpath_validator.TestDpathValidator ‑ test_given_invalid_path_when_validate_then_raise_key_error
unit_tests.sources.declarative.validators.test_dpath_validator.TestDpathValidator ‑ test_given_valid_path_and_input_validate_is_successful
unit_tests.sources.declarative.validators.test_dpath_validator.TestDpathValidator ‑ test_given_valid_top_level_path_and_input_validate_is_successful

@pnilan pnilan enabled auto-merge (squash) June 9, 2025 19:53
Copy link

github-actions bot commented Jun 9, 2025

PyTest Results (Full)

3 659 tests   - 2   3 649 ✅  - 2   17m 17s ⏱️ +7s
    1 suites ±0      10 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit b1d20f3. ± Comparison against base commit 99a1a96.

This pull request removes 3 and adds 1 tests. Note that renamed tests count towards both.
unit_tests.sources.declarative.validators.test_dpath_validator.TestDpathValidator ‑ test_given_empty_input_data_when_validate_then_validate_raises_exception
unit_tests.sources.declarative.validators.test_dpath_validator.TestDpathValidator ‑ test_given_invalid_path_when_validate_then_raise_key_error
unit_tests.sources.declarative.validators.test_dpath_validator.TestDpathValidator ‑ test_given_valid_path_and_input_validate_is_successful
unit_tests.sources.declarative.validators.test_dpath_validator.TestDpathValidator ‑ test_given_valid_top_level_path_and_input_validate_is_successful

@pnilan pnilan merged commit 8e4d946 into main Jun 9, 2025
31 of 32 checks passed
@pnilan pnilan deleted the pnilan/fix/fix-dpath-validator-bypass branch June 9, 2025 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working security
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant