Skip to content

fix(api): don't scope serializer querysets by AnonymousUser during schema generation#15344

Merged
Maffooch merged 1 commit into
bugfixfrom
fix/openapi-schema-anonymoususer-scoping
Jul 23, 2026
Merged

fix(api): don't scope serializer querysets by AnonymousUser during schema generation#15344
Maffooch merged 1 commit into
bugfixfrom
fix/openapi-schema-anonymoususer-scoping

Conversation

@Maffooch

@Maffooch Maffooch commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

[sc-13920]

What

Fixes OpenAPI schema generation crashing when the Finding / import serializers are introspected with an unauthenticated request.

Why

The Finding serializer (dojo/finding/api/serializer.py) and the import serializer (dojo/api_v2/serializers.py) scope their endpoint/location PrimaryKeyRelatedField querysets to request.user. During OpenAPI schema generation drf-spectacular builds serializers with an AnonymousUser request. AnonymousUser is truthy, so the if user branch called get_authorized_*(user=AnonymousUser), and filtering a PK field by the AnonymousUser instance raised:

Field 'id' expected a number but got <AnonymousUser object>

drf-spectacular then ignored the affected views (FindingViewSet/importers), which produced an operationId "findings_retrieve" collision and a KeyError: 'content' in the prefetch postprocessing hook — breaking schema-integrity tests for downstream (Pro) consumers.

Fix

Treat an unauthenticated user as "no user" so the field falls back to an empty queryset, matching the prior behavior for anonymous / schema-time contexts:

if user is not None and not user.is_authenticated:
    user = None

Applied at both scoping sites.

Verification

Generated the full OpenAPI schema and ran the schema-integrity suite: no schema-generation errors/warnings, no operationId collisions, no prefetch KeyError; full mock-based unit phase green (15,441 passed).

🤖 Generated with Claude Code

@Maffooch
Maffooch requested a review from blakeaowens as a code owner July 23, 2026 06:13
@github-actions github-actions Bot added the apiv2 label Jul 23, 2026
…hema generation

The Finding and import serializers scope their endpoint/location
PrimaryKeyRelatedField querysets to request.user. During OpenAPI schema
generation drf-spectacular instantiates serializers with an AnonymousUser
request, which is truthy, so the `if user` branch called
get_authorized_*(user=AnonymousUser); filtering a PK field by the AnonymousUser
instance raised "Field 'id' expected a number but got AnonymousUser".
drf-spectacular then dropped the affected views, producing an operationId
collision and a KeyError in the prefetch postprocessing hook.

Treat an unauthenticated user as no user so the field falls back to an empty
queryset, matching pre-existing behavior for anonymous/schema-time contexts.

Add a regression test that renders the full OpenAPI v3 schema and gates that
generation produces zero errors (drf-spectacular logs, rather than raises, when
it drops a view -- so nothing caught this before).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Maffooch
Maffooch force-pushed the fix/openapi-schema-anonymoususer-scoping branch from 89a111b to 1e14f6a Compare July 23, 2026 06:18
@Maffooch Maffooch added this to the 3.1.300 milestone Jul 23, 2026
@devGregA
devGregA self-requested a review July 23, 2026 16:03
@Maffooch
Maffooch merged commit 0a1d757 into bugfix Jul 23, 2026
150 checks passed
@Maffooch
Maffooch deleted the fix/openapi-schema-anonymoususer-scoping branch July 23, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants