fix: pass request context to ProjectSerializer in ProjectStateAPI
Summary
Fix HTTP 500 on GET /api/dm/project?project={id} when project.created_by is None.
Problem
ProjectStateAPI.get() instantiates ProjectSerializer without passing a request context:
# Before
data = ProjectSerializer(project).data
ProjectSerializer.user_id is called when evaluating the queue_total / queue_done SerializerMethodFields:
@property
def user_id(self):
try:
return self.context['request'].user.id # KeyError when no context
except KeyError:
return next(iter(self.context['user_cache'])) # fallback
Why this doesn't always crash (hidden fragility)
In ProjectSerializer.Meta.fields, created_by (index 16) is serialized before queue_total (index 43). UserSimpleSerializer.to_representation() writes the project creator into self.context['user_cache'], which mutates the shared root context dict in-place. This accidentally satisfies the user_id fallback in most cases.
Why it crashes when created_by is None
When project.created_by is None, DRF short-circuits and sets ret['created_by'] = None without ever calling UserSimpleSerializer.to_representation(). As a result, user_cache is never populated and user_id raises KeyError: 'user_cache' → HTTP 500.
Silent wrong-value bug (also fixed)
Even for projects with a valid created_by, the old code computed queue_total / queue_done against the project creator's user ID rather than the requesting user's ID.
Fix
# After
data = ProjectSerializer(project, context={'request': request}).data
Passing request explicitly means user_id always resolves from the authenticated request user, regardless of whether created_by is set.
Traceback (reported on v1.23.0)
File "label_studio/data_manager/api.py", line 516, in get
data = ProjectSerializer(project).data
File "label_studio/projects/serializers.py", in user_id
return self.context['request'].user.id
KeyError: 'request'
During handling of the above exception, another exception occurred:
File "label_studio/projects/serializers.py", in user_id
return next(iter(self.context['user_cache']))
KeyError: 'user_cache'
Test Plan
- [x]
test_dm_project_returns_200— endpoint returns 200 - [x]
test_dm_project_includes_queue_fields— response includes integerqueue_totalandqueue_done - [x]
test_dm_project_on_freshly_created_project— empty project returnsqueue_total=0, queue_done=0 - [x]
test_dm_project_returns_404_for_missing_project— unknown project id returns 404 - [x]
test_dm_project_null_creator_returns_200— project withcreated_by=Nonereturns 200 instead of 500 (key regression test)
🤖 Generated with Claude Code
Deploy request for label-studio-docs-new-theme pending review.
Visit the deploys page to approve it
| Name | Link |
|---|---|
| Latest commit | fae03def32c0a9671b21c9fa127cccd452721501 |
Deploy request for heartex-docs pending review.
Visit the deploys page to approve it
| Name | Link |
|---|---|
| Latest commit | fae03def32c0a9671b21c9fa127cccd452721501 |
Deploy Preview for label-studio-storybook canceled.
| Name | Link |
|---|---|
| Latest commit | fae03def32c0a9671b21c9fa127cccd452721501 |
| Latest deploy log | https://app.netlify.com/projects/label-studio-storybook/deploys/6a0db4b96de6020008db63b0 |
Deploy Preview for label-studio-playground canceled.
| Name | Link |
|---|---|
| Latest commit | fae03def32c0a9671b21c9fa127cccd452721501 |
| Latest deploy log | https://app.netlify.com/projects/label-studio-playground/deploys/6a0db4b988bb0d0008077897 |
This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days.
This PR was closed because it has been stalled for 10 days with no activity.