Skip to content

Commit 8522c0a

Browse files
committed
Fix support of custom error handler in Django decorator
1 parent 60a2aad commit 8522c0a

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

docs/integrations/django.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Use `DjangoOpenAPIViewDecorator` with the OpenAPI object to create the decorator
6666
``` python hl_lines="1 3 6"
6767
from openapi_core.contrib.django.decorators import DjangoOpenAPIViewDecorator
6868

69-
openapi_validated = FlaskOpenAPIViewDecorator(openapi)
69+
openapi_validated = DjangoOpenAPIViewDecorator(openapi)
7070

7171

7272
@openapi_validated

openapi_core/contrib/django/decorators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(
4848

4949
self.request_cls = request_cls
5050
self.response_cls = response_cls
51+
self.errors_handler_cls = errors_handler_cls
5152

5253
def __call__(self, view_func: Callable[..., Any]) -> Callable[..., Any]:
5354
"""

tests/integration/contrib/django/data/v3.0/djangoproject/status/views.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
from pathlib import Path
2+
from typing import Any
3+
from typing import Dict
24

35
from django.http import HttpResponse
46
from jsonschema_path import SchemaPath
57

68
from openapi_core.contrib.django.decorators import DjangoOpenAPIViewDecorator
9+
from openapi_core.contrib.django.handlers import DjangoOpenAPIErrorsHandler
10+
11+
12+
class AppDjangoOpenAPIValidRequestHandler(DjangoOpenAPIErrorsHandler):
13+
@classmethod
14+
def format_openapi_error(cls, error: BaseException) -> Dict[str, Any]:
15+
ret = DjangoOpenAPIErrorsHandler.format_openapi_error(error)
16+
ret["title"] = ret["title"].upper()
17+
return ret
18+
719

820
check_minimal_spec = DjangoOpenAPIViewDecorator.from_spec(
921
SchemaPath.from_file_path(
1022
Path("tests/integration/data/v3.0/minimal_with_servers.yaml")
11-
)
23+
),
24+
errors_handler_cls=AppDjangoOpenAPIValidRequestHandler,
1225
)
1326

1427

tests/integration/contrib/django/test_django_project.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,7 @@ def test_post_valid(self, client):
460460
)
461461

462462
assert response.status_code == 405 # Method Not Allowed
463+
assert (
464+
response.json()["errors"][0]["title"]
465+
== "OPERATION POST NOT FOUND FOR HTTP://PETSTORE.SWAGGER.IO/STATUS"
466+
)

0 commit comments

Comments
 (0)