label-studio icon indicating copy to clipboard operation
label-studio copied to clipboard

fix: pass request context to ProjectSerializer in ProjectStateAPI

Open caost opened this issue 6 months ago • 4 comments

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 integer queue_total and queue_done
  • [x] test_dm_project_on_freshly_created_project — empty project returns queue_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 with created_by=None returns 200 instead of 500 (key regression test)

🤖 Generated with Claude Code

caost avatar May 20 '26 13:05 caost

Deploy request for label-studio-docs-new-theme pending review.

Visit the deploys page to approve it

Name Link
Latest commit fae03def32c0a9671b21c9fa127cccd452721501

netlify avatar May 20 '26 13:05 netlify

Deploy request for heartex-docs pending review.

Visit the deploys page to approve it

Name Link
Latest commit fae03def32c0a9671b21c9fa127cccd452721501

netlify avatar May 20 '26 13:05 netlify

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

netlify avatar May 20 '26 13:05 netlify

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

netlify avatar May 20 '26 13:05 netlify

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.

robot-ci-heartex avatar Jul 05 '26 02:07 robot-ci-heartex

This PR was closed because it has been stalled for 10 days with no activity.

robot-ci-heartex avatar Jul 16 '26 02:07 robot-ci-heartex