Skip to content

Commit a581542

Browse files
fix(django): Add sync_capable to SentryWrappingMiddleware (getsentry#3510)
* fix(django): Add `sync_capable` to `SentryWrappingMiddleware` Fixes getsentry#3506 * test(django): Test that `sync_capable` set on wrapped middleware
1 parent 53897ff commit a581542

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

sentry_sdk/integrations/django/middleware.py

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def sentry_wrapped_method(*args, **kwargs):
125125
class SentryWrappingMiddleware(
126126
_asgi_middleware_mixin_factory(_check_middleware_span) # type: ignore
127127
):
128+
sync_capable = getattr(middleware, "sync_capable", True)
128129
async_capable = DJANGO_SUPPORTS_ASYNC_MIDDLEWARE and getattr(
129130
middleware, "async_capable", False
130131
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import Optional
2+
3+
import pytest
4+
5+
from sentry_sdk.integrations.django.middleware import _wrap_middleware
6+
7+
8+
def _sync_capable_middleware_factory(sync_capable):
9+
# type: (Optional[bool]) -> type
10+
"""Create a middleware class with a sync_capable attribute set to the value passed to the factory.
11+
If the factory is called with None, the middleware class will not have a sync_capable attribute.
12+
"""
13+
sc = sync_capable # rename so we can set sync_capable in the class
14+
15+
class TestMiddleware:
16+
nonlocal sc
17+
if sc is not None:
18+
sync_capable = sc
19+
20+
return TestMiddleware
21+
22+
23+
@pytest.mark.parametrize(
24+
("middleware", "sync_capable"),
25+
(
26+
(_sync_capable_middleware_factory(True), True),
27+
(_sync_capable_middleware_factory(False), False),
28+
(_sync_capable_middleware_factory(None), True),
29+
),
30+
)
31+
def test_wrap_middleware_sync_capable_attribute(middleware, sync_capable):
32+
wrapped_middleware = _wrap_middleware(middleware, "test_middleware")
33+
34+
assert wrapped_middleware.sync_capable is sync_capable

0 commit comments

Comments
 (0)