From 8522c0a3dbf3028b0bf94fc280a92defc2bbd4c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Bregu=C5=82a?= Date: Mon, 24 Mar 2025 21:54:14 +0000 Subject: [PATCH] Fix support of custom error handler in Django decorator --- docs/integrations/django.md | 2 +- openapi_core/contrib/django/decorators.py | 1 + .../data/v3.0/djangoproject/status/views.py | 15 ++++++++++++++- .../contrib/django/test_django_project.py | 4 ++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/integrations/django.md b/docs/integrations/django.md index 00c6fef4..c983a18e 100644 --- a/docs/integrations/django.md +++ b/docs/integrations/django.md @@ -66,7 +66,7 @@ Use `DjangoOpenAPIViewDecorator` with the OpenAPI object to create the decorator ``` python hl_lines="1 3 6" from openapi_core.contrib.django.decorators import DjangoOpenAPIViewDecorator -openapi_validated = FlaskOpenAPIViewDecorator(openapi) +openapi_validated = DjangoOpenAPIViewDecorator(openapi) @openapi_validated diff --git a/openapi_core/contrib/django/decorators.py b/openapi_core/contrib/django/decorators.py index f6be3cbf..49c15260 100644 --- a/openapi_core/contrib/django/decorators.py +++ b/openapi_core/contrib/django/decorators.py @@ -48,6 +48,7 @@ def __init__( self.request_cls = request_cls self.response_cls = response_cls + self.errors_handler_cls = errors_handler_cls def __call__(self, view_func: Callable[..., Any]) -> Callable[..., Any]: """ diff --git a/tests/integration/contrib/django/data/v3.0/djangoproject/status/views.py b/tests/integration/contrib/django/data/v3.0/djangoproject/status/views.py index 10d87749..2d8703b8 100644 --- a/tests/integration/contrib/django/data/v3.0/djangoproject/status/views.py +++ b/tests/integration/contrib/django/data/v3.0/djangoproject/status/views.py @@ -1,14 +1,27 @@ from pathlib import Path +from typing import Any +from typing import Dict from django.http import HttpResponse from jsonschema_path import SchemaPath from openapi_core.contrib.django.decorators import DjangoOpenAPIViewDecorator +from openapi_core.contrib.django.handlers import DjangoOpenAPIErrorsHandler + + +class AppDjangoOpenAPIValidRequestHandler(DjangoOpenAPIErrorsHandler): + @classmethod + def format_openapi_error(cls, error: BaseException) -> Dict[str, Any]: + ret = DjangoOpenAPIErrorsHandler.format_openapi_error(error) + ret["title"] = ret["title"].upper() + return ret + check_minimal_spec = DjangoOpenAPIViewDecorator.from_spec( SchemaPath.from_file_path( Path("tests/integration/data/v3.0/minimal_with_servers.yaml") - ) + ), + errors_handler_cls=AppDjangoOpenAPIValidRequestHandler, ) diff --git a/tests/integration/contrib/django/test_django_project.py b/tests/integration/contrib/django/test_django_project.py index 8a0697e1..9baf274d 100644 --- a/tests/integration/contrib/django/test_django_project.py +++ b/tests/integration/contrib/django/test_django_project.py @@ -460,3 +460,7 @@ def test_post_valid(self, client): ) assert response.status_code == 405 # Method Not Allowed + assert ( + response.json()["errors"][0]["title"] + == "OPERATION POST NOT FOUND FOR HTTP://PETSTORE.SWAGGER.IO/STATUS" + )