diff --git a/ninja/operation.py b/ninja/operation.py index bb46f4463..268505f22 100644 --- a/ninja/operation.py +++ b/ninja/operation.py @@ -176,7 +176,13 @@ def _set_auth( self, auth: Optional[Union[Sequence[Callable], Callable, object]] ) -> None: if auth is not None and auth is not NOT_SET: - self.auth_callbacks = isinstance(auth, Sequence) and auth or [auth] + if isinstance(auth, Sequence): + if not auth: + return + auth = auth + else: + auth = [auth] + self.auth_callbacks = auth # type: ignore[assignment] def _run_checks(self, request: HttpRequest) -> Optional[HttpResponse]: "Runs security/throttle checks for each operation" diff --git a/tests/test_auth.py b/tests/test_auth.py index 42c0af977..38eaabb2f 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -104,6 +104,8 @@ def on_custom_error(request, exc): ("bearer", BearerAuth()), ("async_bearer", AsyncBearerAuth()), ("customexception", KeyHeaderCustomException()), + ("none", None), + ("empty_sequence", []), ]: api.get(f"/{path}", auth=auth, operation_id=path)(demo_operation) @@ -257,6 +259,8 @@ class MockStaffUser(str): 200, dict(auth="keyheadersecret"), ), + ("/none", {}, 200, dict(auth=None)), + ("/empty_sequence", {}, 200, dict(auth=None)), ], ) def test_auth(path, kwargs, expected_code, expected_body, settings):