-
-
Notifications
You must be signed in to change notification settings - Fork 132
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
Add django decorator #976
Merged
Merged
Add django decorator #976
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fea09a4
Add django decorator
mik-laj 6b45fb0
fixup! Add django decorator
mik-laj c3e4c1b
fixup! fixup! Add django decorator
mik-laj 098c0d1
fixup! fixup! fixup! Add django decorator
mik-laj 4842992
Update docs/integrations/django.md
mik-laj ca6044e
Update django.md
mik-laj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
"""OpenAPI core contrib django decorators module""" | ||
|
||
from typing import Any | ||
from typing import Callable | ||
from typing import Optional | ||
from typing import Type | ||
|
||
from django.conf import settings | ||
from django.http.request import HttpRequest | ||
from django.http.response import HttpResponse | ||
from jsonschema_path import SchemaPath | ||
|
||
from openapi_core import OpenAPI | ||
from openapi_core.contrib.django.handlers import DjangoOpenAPIErrorsHandler | ||
from openapi_core.contrib.django.handlers import ( | ||
DjangoOpenAPIValidRequestHandler, | ||
) | ||
from openapi_core.contrib.django.integrations import DjangoIntegration | ||
from openapi_core.contrib.django.providers import get_default_openapi_instance | ||
from openapi_core.contrib.django.requests import DjangoOpenAPIRequest | ||
from openapi_core.contrib.django.responses import DjangoOpenAPIResponse | ||
|
||
|
||
class DjangoOpenAPIViewDecorator(DjangoIntegration): | ||
valid_request_handler_cls = DjangoOpenAPIValidRequestHandler | ||
errors_handler_cls: Type[DjangoOpenAPIErrorsHandler] = ( | ||
DjangoOpenAPIErrorsHandler | ||
) | ||
|
||
def __init__( | ||
self, | ||
openapi: Optional[OpenAPI] = None, | ||
request_cls: Type[DjangoOpenAPIRequest] = DjangoOpenAPIRequest, | ||
response_cls: Type[DjangoOpenAPIResponse] = DjangoOpenAPIResponse, | ||
errors_handler_cls: Type[ | ||
DjangoOpenAPIErrorsHandler | ||
] = DjangoOpenAPIErrorsHandler, | ||
): | ||
if openapi is None: | ||
openapi = get_default_openapi_instance() | ||
|
||
super().__init__(openapi) | ||
|
||
# If OPENAPI_RESPONSE_CLS is defined in settings.py (for custom response classes), | ||
# set the response_cls accordingly. | ||
if hasattr(settings, "OPENAPI_RESPONSE_CLS"): | ||
response_cls = settings.OPENAPI_RESPONSE_CLS | ||
|
||
self.request_cls = request_cls | ||
self.response_cls = response_cls | ||
|
||
def __call__(self, view_func: Callable[..., Any]) -> Callable[..., Any]: | ||
""" | ||
Thanks to this method, the class acts as a decorator. | ||
Example usage: | ||
|
||
@DjangoOpenAPIViewDecorator() | ||
def my_view(request): ... | ||
|
||
""" | ||
|
||
def _wrapped_view( | ||
request: HttpRequest, *args: Any, **kwargs: Any | ||
) -> HttpResponse: | ||
# get_response is the function that we treats | ||
# as the "next step" in the chain (i.e., our original view). | ||
def get_response(r: HttpRequest) -> HttpResponse: | ||
return view_func(r, *args, **kwargs) | ||
|
||
# Create a handler that will validate the request. | ||
valid_request_handler = self.valid_request_handler_cls( | ||
request, get_response | ||
) | ||
|
||
# Validate the request (before running the view). | ||
errors_handler = self.errors_handler_cls() | ||
response = self.handle_request( | ||
request, valid_request_handler, errors_handler | ||
) | ||
|
||
# Validate the response (after the view) if should_validate_response() returns True. | ||
return self.handle_response(request, response, errors_handler) | ||
|
||
return _wrapped_view | ||
|
||
@classmethod | ||
def from_spec( | ||
cls, | ||
spec: SchemaPath, | ||
request_cls: Type[DjangoOpenAPIRequest] = DjangoOpenAPIRequest, | ||
response_cls: Type[DjangoOpenAPIResponse] = DjangoOpenAPIResponse, | ||
errors_handler_cls: Type[ | ||
DjangoOpenAPIErrorsHandler | ||
] = DjangoOpenAPIErrorsHandler, | ||
) -> "DjangoOpenAPIViewDecorator": | ||
openapi = OpenAPI(spec) | ||
return cls( | ||
openapi, | ||
request_cls=request_cls, | ||
response_cls=response_cls, | ||
errors_handler_cls=errors_handler_cls, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""OpenAPI core contrib django providers module""" | ||
|
||
import warnings | ||
from typing import cast | ||
|
||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
|
||
from openapi_core import OpenAPI | ||
|
||
|
||
def get_default_openapi_instance() -> OpenAPI: | ||
""" | ||
Retrieves or initializes the OpenAPI instance based on Django settings | ||
(either OPENAPI or OPENAPI_SPEC). | ||
This function ensures the spec is only loaded once. | ||
""" | ||
if hasattr(settings, "OPENAPI"): | ||
# Recommended (newer) approach | ||
return cast(OpenAPI, settings.OPENAPI) | ||
elif hasattr(settings, "OPENAPI_SPEC"): | ||
# Backward compatibility | ||
warnings.warn( | ||
"OPENAPI_SPEC is deprecated. Use OPENAPI in your settings instead.", | ||
DeprecationWarning, | ||
) | ||
return OpenAPI(settings.OPENAPI_SPEC) | ||
else: | ||
raise ImproperlyConfigured( | ||
"Neither OPENAPI nor OPENAPI_SPEC is defined in Django settings." | ||
) |
Empty file.
Empty file.
17 changes: 17 additions & 0 deletions
17
tests/integration/contrib/django/data/v3.0/djangoproject/status/views.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from pathlib import Path | ||
|
||
from django.http import HttpResponse | ||
from jsonschema_path import SchemaPath | ||
|
||
from openapi_core.contrib.django.decorators import DjangoOpenAPIViewDecorator | ||
|
||
check_minimal_spec = DjangoOpenAPIViewDecorator.from_spec( | ||
SchemaPath.from_file_path( | ||
Path("tests/integration/data/v3.0/minimal_with_servers.yaml") | ||
) | ||
) | ||
|
||
|
||
@check_minimal_spec | ||
def get_status(request): | ||
return HttpResponse("OK") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OPENAPI_SPEC is a depreacted, so we should suggest to use OPENAPI.