Skip to content

Commit aa503f1

Browse files
authored
Merge pull request #104 from ghazi-git/schema-endpoint-error-when-server-permissions-is-restricted-to-admins
schema-endpoint-error-when-server-permissions-is-restricted-to-admins
2 parents c6f0e96 + 8b0491f commit aa503f1

File tree

8 files changed

+44
-13
lines changed

8 files changed

+44
-13
lines changed

docs/changelog.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ message:
3434
"EXCEPTION_HANDLER_CLASS": "path.to.MyExceptionHandler"
3535
}
3636
```
37+
- set minimum version of drf-spectacular to 0.27.1
38+
- `drf_standardized_errors.types.ErrorType` is now the following type hint
39+
```python
40+
from typing import Literal
41+
ErrorType = Literal["validation_error", "client_error", "server_error"]
42+
```
43+
`ErrorType` was previously an enum. If you referenced its members in your code, make sure to replace their
44+
use cases with the newly added constants:
45+
```
46+
from drf_standardized_errors.types import VALIDATION_ERROR, CLIENT_ERROR, SERVER_ERROR
47+
ErrorType.VALIDATION_ERROR --> VALIDATION_ERROR
48+
ErrorType.CLIENT_ERROR --> CLIENT_ERROR
49+
ErrorType.SERVER_ERROR --> SERVER_ERROR
50+
```
3751

3852
## [0.14.1] - 2024-08-10
3953
### Added

docs/openapi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ REST_FRAMEWORK = {
196196

197197
### Already using a custom `AutoSchema` class
198198
If you're already overriding the `AutoSchema` class provided by drf-spectacular, be sure to inherit from the
199-
AutoSchema class provided by this package instead. Also, if you're overriding `get_examples` and/or
199+
AutoSchema class provided by this package instead. Also, if you're overriding `_get_examples` and/or
200200
`_get_response_bodies`, be sure to call `super`.
201201

202202

drf_standardized_errors/formatter.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@
55
from rest_framework.status import is_client_error
66

77
from .settings import package_settings
8-
from .types import Error, ErrorResponse, ErrorType, ExceptionHandlerContext
8+
from .types import (
9+
CLIENT_ERROR,
10+
SERVER_ERROR,
11+
VALIDATION_ERROR,
12+
Error,
13+
ErrorResponse,
14+
ErrorType,
15+
ExceptionHandlerContext,
16+
)
917

1018

1119
class ExceptionFormatter:
@@ -42,11 +50,11 @@ def run(self) -> Any:
4250

4351
def get_error_type(self) -> ErrorType:
4452
if isinstance(self.exc, exceptions.ValidationError):
45-
return ErrorType.VALIDATION_ERROR
53+
return VALIDATION_ERROR
4654
elif is_client_error(self.exc.status_code):
47-
return ErrorType.CLIENT_ERROR
55+
return CLIENT_ERROR
4856
else:
49-
return ErrorType.SERVER_ERROR
57+
return SERVER_ERROR
5058

5159
def get_errors(self) -> List[Error]:
5260
"""

drf_standardized_errors/types.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from dataclasses import dataclass
2-
from enum import Enum
3-
from typing import List, Optional, TypedDict
2+
from typing import Final, List, Literal, Optional, TypedDict
43

54
from rest_framework.request import Request
65
from rest_framework.views import APIView
@@ -13,10 +12,10 @@ class ExceptionHandlerContext(TypedDict):
1312
request: Optional[Request]
1413

1514

16-
class ErrorType(str, Enum):
17-
VALIDATION_ERROR = "validation_error"
18-
CLIENT_ERROR = "client_error"
19-
SERVER_ERROR = "server_error"
15+
VALIDATION_ERROR: Final = "validation_error"
16+
CLIENT_ERROR: Final = "client_error"
17+
SERVER_ERROR: Final = "server_error"
18+
ErrorType = Literal["validation_error", "client_error", "server_error"]
2019

2120

2221
@dataclass

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ release = [
5959
"tbump",
6060
]
6161
openapi = [
62-
"drf-spectacular>=0.27.0",
62+
"drf-spectacular>=0.27.1",
6363
"inflection",
6464
]
6565

tests/test_openapi.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,11 @@ def test_schema_generated(api_client):
630630
assert response.status_code == 200
631631

632632

633+
def test_protected_schema_response(api_client):
634+
response = api_client.get("/protected-schema/")
635+
assert response.status_code == 403
636+
637+
633638
@extend_schema_serializer(
634639
examples=[
635640
OpenApiExample(

tests/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.urls import path
22
from drf_spectacular.views import SpectacularAPIView
3+
from rest_framework.permissions import IsAdminUser
34

45
from .views import (
56
AuthErrorView,
@@ -18,4 +19,8 @@
1819
path("rate-limit-error/", RateLimitErrorView.as_view()),
1920
path("recursion-error/", RecursionView.as_view()),
2021
path("schema/", SpectacularAPIView.as_view(), name="api-schema"),
22+
path(
23+
"protected-schema/",
24+
SpectacularAPIView.as_view(permission_classes=[IsAdminUser]),
25+
),
2126
]

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ python =
2828
deps =
2929
pytest
3030
pytest-django
31-
drf-spectacular>=0.27.0
31+
drf-spectacular>=0.27.1
3232
django-filter
3333
dj32: Django>=3.2,<4.0
3434
dj40: Django>=4.0,<4.1

0 commit comments

Comments
 (0)