Skip to content

fix: handle non-numeric organisation query parameter gracefully - Issue #7426#7443

Open
lukman48 wants to merge 3 commits intoFlagsmith:mainfrom
lukman48:fix/api-crash-non-numeric-organisation-id
Open

fix: handle non-numeric organisation query parameter gracefully - Issue #7426#7443
lukman48 wants to merge 3 commits intoFlagsmith:mainfrom
lukman48:fix/api-crash-non-numeric-organisation-id

Conversation

@lukman48
Copy link
Copy Markdown

@lukman48 lukman48 commented May 6, 2026

Fixes Issue #7426: API crash on /api/v1/projects/ with non-numeric organisation query parameter

Problem

The API endpoint /api/v1/projects/ crashes with a ValueError when the organisation query parameter is provided with a non-numeric value.

Error Details

ValueError: invalid literal for int() with base 10: 'A60ZG9cp5WC53RRZHDkIlUiWuAL57Jhi'
File "django/db/models/fields/__init__.py", line 2128, in get_prep_value
    return int(value)
File "api/projects/views.py", line 103, in get_queryset
    queryset = queryset.filter(organisation__id=organisation_id)

Root Cause

The ProjectViewSet.get_queryset() method directly uses the organisation query parameter for database filtering without validating that it's a valid integer. Django's ORM attempts to convert it to an integer for the field lookup, which fails with a traceback instead of returning a proper 4xx response.

Solution

Added input validation to check that the organisation_id parameter is numeric before using it for filtering:

  1. Attempt to convert organisation_id to int
  2. If conversion succeeds, proceed with the filter
  3. If conversion fails (ValueError/TypeError), return an empty queryset
  4. This returns a 200 response with an empty array instead of a 500 error

Implementation

organisation_id = self.request.query_params.get("organisation")
if organisation_id:
    try:
        int(organisation_id)  # Validate it's numeric
        queryset = queryset.filter(organisation__id=organisation_id)
    except (ValueError, TypeError):
        return Project.objects.none()  # Invalid ID, return empty queryset

Behavior Changes

  • Before: /api/v1/projects/?organisation=invalid → 500 Internal Server Error
  • After: /api/v1/projects/?organisation=invalid → 200 OK with empty array []

Testing Checklist

  • Verify fix compiles/lints successfully
  • Valid numeric organisation IDs still work correctly
  • Invalid non-numeric organisation IDs return empty queryset (not crash)
  • Empty organisation parameter works as before
  • No organisation parameter works as before
  • Other query parameters (uuid, etc.) still work
  • Response is valid JSON for all scenarios
  • No database errors in test suite

…Flagsmith#7426)

The API was crashing with ValueError when the 'organisation' query parameter
on /api/v1/projects/ was provided with a non-numeric value.

This happened because Django was trying to convert the parameter directly to
an integer for the database filter without validation.

The fix validates that the organisation_id parameter is numeric before using it
for filtering. If it's not numeric, the endpoint returns an empty queryset
(no projects match the invalid filter criteria) instead of crashing.

This returns a proper 200 response with an empty array instead of a 500 error,
allowing client applications to handle the response gracefully.

Fixes Flagsmith#7426
@lukman48 lukman48 requested a review from a team as a code owner May 6, 2026 14:30
@lukman48 lukman48 requested review from Zaimwa9 and removed request for a team May 6, 2026 14:30
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 6, 2026

@lukman48 is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the api Issue related to the REST API label May 6, 2026
lukman48 and others added 2 commits May 6, 2026 21:30
Add test cases to verify that:
1. Non-numeric organisation parameters return 200 with empty list (not 500)
2. Invalid organisation parameters are handled gracefully even when projects exist
3. API responds correctly with proper HTTP status code

These tests ensure the fix for issue Flagsmith#7426 works correctly.
@lukman48
Copy link
Copy Markdown
Author

lukman48 commented May 6, 2026

@claude review

@emyller
Copy link
Copy Markdown
Contributor

emyller commented May 6, 2026

@lukman48 Thanks for this contribution! Can you please explain your decision to go for a 200, instead of the issue's acceptance criterion's 4xx?

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

Labels

api Issue related to the REST API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants