Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support of custom error handler in Django decorator #979

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/integrations/django.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions openapi_core/contrib/django/decorators.py
Original file line number Diff line number Diff line change
@@ -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]:
"""
Original file line number Diff line number Diff line change
@@ -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,
)


4 changes: 4 additions & 0 deletions tests/integration/contrib/django/test_django_project.py
Original file line number Diff line number Diff line change
@@ -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"
)
Loading
Oops, something went wrong.