Skip to content

Latest commit

 

History

History
131 lines (89 loc) · 3.63 KB

django.md

File metadata and controls

131 lines (89 loc) · 3.63 KB

Django

This section describes the integration with the Django web framework. The integration supports Django version 3.0 and above.

Middleware

Django can be integrated using middleware to apply OpenAPI validation to your entire application.

Add DjangoOpenAPIMiddleware to your MIDDLEWARE list and define OPENAPI.

from openapi_core import OpenAPI

MIDDLEWARE = [
    # ...
    'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware',
]

OPENAPI = OpenAPI.from_dict(spec_dict)

After that, all your requests and responses will be validated.

You also have access to the unmarshalled result object with all unmarshalled request data through the openapi attribute of the request object.

from django.views import View

class MyView(View):
    def get(self, request):
        # Get parameters object with path, query, cookies, and headers parameters
        unmarshalled_params = request.openapi.parameters
        # Or specific location parameters
        unmarshalled_path_params = request.openapi.parameters.path

        # Get body
        unmarshalled_body = request.openapi.body

        # Get security data
        unmarshalled_security = request.openapi.security

Response validation

You can skip the response validation process by setting OPENAPI_RESPONSE_CLS to None.

from openapi_core import OpenAPI

MIDDLEWARE = [
    # ...
    'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware',
]

OPENAPI = OpenAPI.from_dict(spec_dict)
OPENAPI_RESPONSE_CLS = None

Decorator

Django can be integrated using view decorators to apply OpenAPI validation to your application's specific views.

Use DjangoOpenAPIViewDecorator with the OpenAPI object to create the decorator.

from openapi_core.contrib.django.decorators import DjangoOpenAPIViewDecorator

openapi_validated = DjangoOpenAPIViewDecorator(openapi)


@openapi_validated
def home():
    return "Welcome home"

You can skip the response validation process by setting response_cls to None.

from openapi_core.contrib.django.decorators import DjangoOpenAPIViewDecorator

openapi_validated = DjangoOpenAPIViewDecorator(
    openapi,
    response_cls=None,
)

If you want to decorate a class-based view, you can use the method_decorator decorator:

from django.utils.decorators import method_decorator

@method_decorator(openapi_validated, name='dispatch')
class MyView(View):

    def get(self, request, *args, **kwargs):
        return "Welcome home"

Low level

The integration defines classes useful for low-level integration.

Request

Use DjangoOpenAPIRequest to create an OpenAPI request from a Django request:

from openapi_core.contrib.django import DjangoOpenAPIRequest

class MyView(View):
    def get(self, request):
        openapi_request = DjangoOpenAPIRequest(request)
        openapi.validate_request(openapi_request)

Response

Use DjangoOpenAPIResponse to create an OpenAPI response from a Django response:

from openapi_core.contrib.django import DjangoOpenAPIResponse

class MyView(View):
    def get(self, request):
        response = JsonResponse({'hello': 'world'})
        openapi_request = DjangoOpenAPIRequest(request)
        openapi_response = DjangoOpenAPIResponse(response)
        openapi.validate_response(openapi_request, openapi_response)
        return response