Skip to content

Conversation

@Prateeks16
Copy link

@Prateeks16 Prateeks16 commented Nov 1, 2025

This PR improves the search API by preventing empty or whitespace-only queries from being processed, saving resources and providing clear feedback to clients. It enforces validation at the schema level, ensures a consistent HTTP 400 response for invalid queries, and adds robust, isolated tests.

Changes
Validation
Enforces non-empty, non-whitespace query in SearchRequest using Pydantic’s constr(min_length=1, strip_whitespace=True) and a custom validator.
Error Handling
Adds an app-level RequestValidationError handler that returns HTTP 400 with a clear message for /api/v1/search validation errors (default 422 for other endpoints is preserved).
Keeps a defensive runtime check in the router for extra safety.
Testing
Adds test_search_router_isolated.py: a fast, self-contained test that mounts only the search router, stubs auth/db dependencies, and asserts 400 for empty/whitespace queries.
Adds schema validation unit tests (test_search_schema_validation.py) and a direct runner script (run_schema_validation_direct.py) for local validation.
Developer Experience
Isolated tests do not require a running database or external services, making local development and PR review faster and more reliable.
How to Test
Isolated Router Test (no DB/external deps):

Schema Validation Test:

Direct Schema Validation Script:

Integration/CI:

Ensure CI sets required environment variables (POSTGRES_SERVER, GH_TOKEN_LIST, etc.).
If dependency installation fails due to blar-graph, consider removing or replacing it in requirements.txt.
Changelog
Reject empty/whitespace search queries with HTTP 400 and clear error message
Enforce schema-level validation for SearchRequest.query
Add app-level handler to map search validation errors to 400
Add isolated router test for empty/whitespace query rejection
Add schema validation unit tests

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced search functionality with improved input validation: the application now rejects empty or whitespace-only search queries and returns a clear, descriptive error message to users.
  • Tests

    • Added comprehensive test coverage for search query validation to ensure robust and reliable input handling.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 1, 2025

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

📥 Commits

Reviewing files that changed from the base of the PR and between 56adc4f and a5d374b.

📒 Files selected for processing (7)
  • app/main.py (2 hunks)
  • app/modules/search/search_router.py (2 hunks)
  • app/modules/search/search_schema.py (1 hunks)
  • tests/run_schema_validation_direct.py (1 hunks)
  • tests/test_search_schema_validation.py (1 hunks)
  • tests/test_search_validation.py (1 hunks)
  • tests_isolated/test_search_router_isolated.py (1 hunks)
 _______________________________________
< For a good time, call 1-800-COD-RABT. >
 ---------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).

Tip

You can generate walkthrough in a markdown collapsible section to save space.

Enable the reviews.collapse_walkthrough setting in your project's settings in CodeRabbit to generate walkthrough in a markdown collapsible section.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 1, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant