fix: validate payload field types in /api/recommend to prevent 500 errors#434
Open
anshul23102 wants to merge 1 commit into
Open
fix: validate payload field types in /api/recommend to prevent 500 errors#434anshul23102 wants to merge 1 commit into
anshul23102 wants to merge 1 commit into
Conversation
…rors (komalharshita#408) Calling .strip() on null or non-string JSON values caused an unhandled AttributeError. Added a type check loop that returns a 400 Bad Request for any field that is not a string, and switched the extraction to use (value or "").strip() so null values fall through to the existing empty field validation. Two new tests cover null and list inputs.
|
@anshul23102 is attempting to deploy a commit to the komalsony234-1530's projects Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary [required]
The
/api/recommendendpoint crashed with a 500 Internal Server Error whenever a JSON field was sent asnullor a non-string type (e.g. a list or a number). The root cause was thatpayload.get("field", "")returnsNonewhen the key is explicitly present with anullvalue — the default only applies when the key is absent entirely. Calling.strip()onNoneraises an unhandledAttributeError.This PR adds a type-check pass over the four expected string fields before any
.strip()call. If any field is non-null but not a string, the endpoint returns a structured400 Bad Requestimmediately. For fields that arenull, the extraction switches frompayload.get(field, "").strip()to(payload.get(field) or "").strip()so they produce an empty string and fall through to the existing empty-field validation invalidate_recommendation_inputs.Related Issue [required]
Closes #408
Type of Change [required]
What Was Changed [required]
routes/main_routes.py(value or "").strip()tests/test_basic.pytest_recommend_api_null_fieldandtest_recommend_api_non_string_fieldHow to Test This PR [required]
git checkout fix/recommend-api-type-validationpip install -r requirements.txtpython app.py{"error": "'skills' must be a string value."}with status400{"error": "'skills' must be a string value."}with status400python tests/test_basic.pyExpected test output:
Test Results [required]
Self-Review Checklist [required]
feat/,fix/,docs/,data/,style/,test/python tests/test_basic.pyand all tests passprint()orconsole.log()debug statementsNotes for Reviewer
The existing
validate_recommendation_inputsfunction already handles empty strings correctly. This fix sits one layer above it in the route handler: it catches type errors before they can crash.strip(), then lets the existing validation handle missing or blank values as before. No changes to the validation logic or the scoring engine were needed.