fix: pass request context to ProjectSerializer in ProjectStateAPI#9755
Open
caost wants to merge 1 commit into
Open
fix: pass request context to ProjectSerializer in ProjectStateAPI#9755caost wants to merge 1 commit into
caost wants to merge 1 commit into
Conversation
ProjectSerializer was instantiated without context in
data_manager/api.py, causing HTTP 500 on GET /api/dm/project when
project.created_by is None.
Root cause: the user_id property falls back to context['user_cache'],
which is populated as a side-effect of UserSimpleSerializer serializing
the created_by field. When created_by is None, DRF skips that
serializer, user_cache is never written, and user_id raises
KeyError: 'user_cache'.
Even for projects with a creator the old code was silently wrong:
queue_total / queue_done were calculated against the project creator's
user ID instead of the requesting user's ID.
Fix: pass context={'request': request} so user_id always resolves from
the authenticated request.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
👷 Deploy request for label-studio-docs-new-theme pending review.Visit the deploys page to approve it
|
👷 Deploy request for heartex-docs pending review.Visit the deploys page to approve it
|
✅ Deploy Preview for label-studio-storybook canceled.
|
✅ Deploy Preview for label-studio-playground canceled.
|
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
Fix HTTP 500 on
GET /api/dm/project?project={id}whenproject.created_byisNone.Problem
ProjectStateAPI.get()instantiatesProjectSerializerwithout passing arequestcontext:ProjectSerializer.user_idis called when evaluating thequeue_total/queue_doneSerializerMethodFields:Why this doesn't always crash (hidden fragility)
In
ProjectSerializer.Meta.fields,created_by(index 16) is serialized beforequeue_total(index 43).UserSimpleSerializer.to_representation()writes the project creator intoself.context['user_cache'], which mutates the shared root context dict in-place. This accidentally satisfies theuser_idfallback in most cases.Why it crashes when
created_byisNoneWhen
project.created_byisNone, DRF short-circuits and setsret['created_by'] = Nonewithout ever callingUserSimpleSerializer.to_representation(). As a result,user_cacheis never populated anduser_idraisesKeyError: 'user_cache'→ HTTP 500.Silent wrong-value bug (also fixed)
Even for projects with a valid
created_by, the old code computedqueue_total/queue_doneagainst the project creator's user ID rather than the requesting user's ID.Fix
Passing
requestexplicitly meansuser_idalways resolves from the authenticated request user, regardless of whethercreated_byis set.Traceback (reported on v1.23.0)
Test Plan
test_dm_project_returns_200— endpoint returns 200test_dm_project_includes_queue_fields— response includes integerqueue_totalandqueue_donetest_dm_project_on_freshly_created_project— empty project returnsqueue_total=0, queue_done=0test_dm_project_returns_404_for_missing_project— unknown project id returns 404test_dm_project_null_creator_returns_200— project withcreated_by=Nonereturns 200 instead of 500 (key regression test)🤖 Generated with Claude Code