Skip to content

fix(validation): reject whitespace-only text in sentiment analyze endpoint#29

Open
Kunal-Somani wants to merge 1 commit intoruxailab:mainfrom
Kunal-Somani:fix/sentiment-route-whitespace-validation
Open

fix(validation): reject whitespace-only text in sentiment analyze endpoint#29
Kunal-Somani wants to merge 1 commit intoruxailab:mainfrom
Kunal-Somani:fix/sentiment-route-whitespace-validation

Conversation

@Kunal-Somani
Copy link
Copy Markdown

Summary

Found a input validation bug in the /sentiment/analyze endpoint.

The Bug

The original guard clause:

if not text:

correctly rejects None and "" but silently passes whitespace-only strings like " " straight to the ML model as valid input. The model runs inference on meaningless content and returns a confident but meaningless prediction — with no error signalled to the caller.

The Fix

if not text or not text.strip():

Whitespace-only strings now return a 400 Bad Request — consistent with how missing and empty text is already handled.

Tests Added

Two new tests in TestSentimentAnalyze:

  • test_sentiment_analyze_whitespace_only_text: verifies " " returns 400 and the service layer is never called
  • test_sentiment_analyze_empty_string_text: verifies "" returns 400 and the service layer is never called

Also fixes the logger.error() f-string anti-pattern in the exception handler — consistent with fixes applied across the service layer in PRs #27 and #28.

Files Changed

  • app/routes/sentiment_routes.py
  • tests/unit/test_routes/test_sentiment_routes.py

…point

The original input guard used 'if not text' which correctly rejects
None and empty strings but silently passes whitespace-only input
(e.g. '   ') straight to the ML model as valid text. This causes the
model to run inference on meaningless input with no error returned to
the caller.

Fix: replace 'if not text' with 'if not text or not text.strip()' so
whitespace-only strings are rejected with a 400 Bad Request — consistent
with how missing and empty text is already handled.

Also fix logger.error() f-string anti-pattern in the exception handler,
replacing it with % formatting for consistency with the rest of the
service layer (PRs ruxailab#27, ruxailab#28).

Tests added:
- test_sentiment_analyze_whitespace_only_text: verifies '   ' returns
  400 and service is never called
- test_sentiment_analyze_empty_string_text: verifies '' returns 400
  and service is never called
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