File: django.rst

package info (click to toggle)
python-openapi-core 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,008 kB
  • sloc: python: 18,868; makefile: 47
file content (101 lines) | stat: -rw-r--r-- 2,828 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Django
======

This section describes integration with `Django <https://www.djangoproject.com>`__ web framework.
The integration supports Django from version 3.0 and above.

Middleware
----------

Django can be integrated by `middleware <https://docs.djangoproject.com/en/5.0/topics/http/middleware/>`__ to apply OpenAPI validation to your entire application.

Add ``DjangoOpenAPIMiddleware`` to your ``MIDDLEWARE`` list and define ``OPENAPI``.

.. code-block:: python
  :emphasize-lines: 6,9

    # settings.py
    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.

Also you have access to unmarshal result object with all unmarshalled request data through ``openapi`` attribute of request object.

.. code-block:: python

    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 response validation process: by setting ``OPENAPI_RESPONSE_CLS`` to ``None``

.. code-block:: python
  :emphasize-lines: 10

    # settings.py
    from openapi_core import OpenAPI

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

    OPENAPI = OpenAPI.from_dict(spec_dict)
    OPENAPI_RESPONSE_CLS = None

Low level
---------

The integration defines classes useful for low level integration.

Request
^^^^^^^

Use ``DjangoOpenAPIRequest`` to create OpenAPI request from Django request:

.. code-block:: python

    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 OpenAPI response from Django response:

.. code-block:: python

    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