Skip to content

Comments

Skip empty operands in search query builder#376

Closed
tuomas777 wants to merge 1 commit intomainfrom
PL-210-search-empty-operands
Closed

Skip empty operands in search query builder#376
tuomas777 wants to merge 1 commit intomainfrom
PL-210-search-empty-operands

Conversation

@tuomas777
Copy link
Contributor

Refs: PL-210

@tuomas777 tuomas777 force-pushed the PL-210-search-empty-operands branch from 6865d0d to e49ffee Compare January 29, 2026 10:33
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 29, 2026

@azure-pipelines
Copy link

PALVELUKARTTA-API branch is deployed to platta: https://palvelukartta-api-pr376.dev.hel.ninja 🚀🚀🚀

@tuomas777
Copy link
Contributor Author

@sentry review

Comment on lines +244 to +246
if not and_operand.strip():
# Skip empty or whitespace-only operands
continue
Copy link
Contributor

Choose a reason for hiding this comment

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

Bug: Queries containing only operators like | or & pass validation but cause build_search_query to return an empty string, leading to a generic database error.
Severity: MEDIUM

Suggested Fix

Add a validation step after calling build_search_query. If the function returns an empty string, either raise a specific ParseError indicating an invalid query or return an empty search result set gracefully, instead of proceeding to the database execution step.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: services/search/api.py#L244-L246

Potential issue: A search query containing only operators, spaces, or commas (e.g.,
`?q=|` or `?q=&`) will pass initial validation checks. However, the `build_search_query`
function will process this input and return an empty string. This empty string is then
passed to a PostgreSQL function (`to_tsquery` or `websearch_to_tsquery`), which cannot
handle empty input and raises a database error. The application catches this exception
and returns a generic `ParseError("Search query failed.")` to the user, rather than a
specific validation error or an empty result set.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link

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 enhances the search query builder to handle empty operands gracefully by skipping whitespace-only strings before processing, preventing the generation of invalid :* syntax in the resulting query.

Changes:

  • Added validation to skip empty or whitespace-only operands in the query builder
  • Added comprehensive test cases covering various scenarios with empty operands

Reviewed changes

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

File Description
services/search/api.py Added check to skip empty/whitespace-only operands before processing
services/search/tests/test_api.py Added 9 test cases to verify correct handling of empty operands in different contexts

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@tuomas777
Copy link
Contributor Author

Replaced by #387

@tuomas777 tuomas777 closed this Feb 12, 2026
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