Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(api): skip debug response validation for polymorphic proxy serializers (#98531)

Harley Alexanderef75ddd84e05
diff --git a/posthog/api/mixins.py b/posthog/api/mixins.py
index 8ced65ca218d..7667e2a5311c 100644
--- a/posthog/api/mixins.py
+++ b/posthog/api/mixins.py
@@ -5,7 +5,7 @@ from typing import Any, Generic, TypeVar, cast
from django.conf import settings
import structlog
-from drf_spectacular.utils import OpenApiResponse, extend_schema
+from drf_spectacular.utils import OpenApiResponse, PolymorphicProxySerializer, extend_schema
from pydantic import BaseModel, ValidationError
from rest_framework import serializers
from rest_framework.exceptions import ParseError
@@ -218,7 +218,12 @@ def validated_request(
if response_config is None:
return result
response_serializer = response_config.response
- if response_serializer is None:
+ # A PolymorphicProxySerializer only describes the schema; it cannot validate data.
+ # `many=True` wraps one in a plain ListSerializer, so the child needs the same check.
+ declares_polymorphic_proxy = isinstance(response_serializer, PolymorphicProxySerializer) or isinstance(
+ getattr(response_serializer, "child", None), PolymorphicProxySerializer
+ )
+ if response_serializer is None or declares_polymorphic_proxy:
return result
context: dict[str, Any] = getattr(self, "get_serializer_context", lambda: {})()
diff --git a/posthog/api/test/test_mixins.py b/posthog/api/test/test_mixins.py
index 29dc165b1f95..3ad529381fa9 100644
--- a/posthog/api/test/test_mixins.py
+++ b/posthog/api/test/test_mixins.py
@@ -5,7 +5,10 @@ import pytest
from posthog.test.base import APIBaseTest
from unittest.mock import Mock, patch
-from drf_spectacular.utils import OpenApiResponse
+from django.test import override_settings
+
+from drf_spectacular.utils import OpenApiResponse, PolymorphicProxySerializer
+from parameterized import parameterized
from rest_framework import serializers, status
from rest_framework.response import Response
@@ -279,6 +282,39 @@ class TestValidatedRequestDecorator(APIBaseTest):
assert response.status_code == status.HTTP_200_OK
assert response.data["custom_response"] == "anything goes"
+ @parameterized.expand(
+ [
+ ("single", False, {"anything": True}),
+ ("many", True, [{"anything": True}]),
+ ]
+ )
+ @override_settings(DEBUG=True)
+ def test_polymorphic_proxy_response_bypasses_validation(self, _name, many, payload):
+ @validated_request(
+ request_serializer=EventCaptureRequestSerializer,
+ responses={
+ 200: OpenApiResponse(
+ response=PolymorphicProxySerializer(
+ component_name="Either",
+ serializers=[EventCaptureResponseSerializer],
+ resource_type_field_name=None,
+ many=many,
+ )
+ ),
+ },
+ )
+ def mock_endpoint(view_self, request):
+ return Response(payload, status=status.HTTP_200_OK)
+
+ mock_request = Mock()
+ mock_request._full_data = {}
+ mock_request.data = {"event": "$pageview", "distinct_id": "user_123"}
+
+ response = mock_endpoint(Mock(), mock_request)
+
+ assert response.status_code == status.HTTP_200_OK
+ assert response.data == payload
+
def test_non_response_object_logs_warning(self):
"""Non-Response object return, should log warning and return result"""