Skip to content

Commit c4d1ef2

Browse files
committed
Docs integrations restructure
1 parent 88c78dc commit c4d1ef2

29 files changed

+759
-717
lines changed

docs/api.rst

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
API
2+
===
3+
4+
.. autosummary::
5+
:toctree: generated

docs/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
# ones.
3434
extensions = [
3535
"sphinx.ext.autodoc",
36+
"sphinx.ext.autosummary",
3637
"sphinx.ext.doctest",
3738
"sphinx.ext.intersphinx",
3839
"sphinx.ext.coverage",

docs/customizations.rst

-102
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Format unmarshallers
2+
====================
3+
4+
Based on ``format`` keyword, openapi-core can also unmarshal values to specific formats.
5+
6+
Openapi-core comes with a set of built-in format unmarshallers, but it's also possible to add custom ones.
7+
8+
Here's an example with the ``usdate`` format that converts a value to date object:
9+
10+
.. code-block:: python
11+
:emphasize-lines: 11
12+
13+
from datetime import datetime
14+
15+
def unmarshal_usdate(value):
16+
return datetime.strptime(value, "%m/%d/%y").date
17+
18+
extra_format_unmarshallers = {
19+
'usdate': unmarshal_usdate,
20+
}
21+
22+
config = Config(
23+
extra_format_unmarshallers=extra_format_unmarshallers,
24+
)
25+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
26+
27+
result = openapi.unmarshal_response(request, response)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Format validators
2+
=================
3+
4+
OpenAPI defines a ``format`` keyword that hints at how a value should be interpreted, e.g. a ``string`` with the type ``date`` should conform to the RFC 3339 date format.
5+
6+
OpenAPI comes with a set of built-in format validators, but it's also possible to add custom ones.
7+
8+
Here's how you could add support for a ``usdate`` format that handles dates of the form MM/DD/YYYY:
9+
10+
.. code-block:: python
11+
:emphasize-lines: 11
12+
13+
import re
14+
15+
def validate_usdate(value):
16+
return bool(re.match(r"^\d{1,2}/\d{1,2}/\d{4}$", value))
17+
18+
extra_format_validators = {
19+
'usdate': validate_usdate,
20+
}
21+
22+
config = Config(
23+
extra_format_validators=extra_format_validators,
24+
)
25+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
26+
27+
openapi.validate_response(request, response)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Media type deserializers
2+
========================
3+
4+
OpenAPI comes with a set of built-in media type deserializers such as: ``application/json``, ``application/xml``, ``application/x-www-form-urlencoded`` or ``multipart/form-data``.
5+
6+
You can also define your own ones. Pass custom defined media type deserializers dictionary with supported mimetypes as a key to `unmarshal_response` function:
7+
8+
.. code-block:: python
9+
:emphasize-lines: 11
10+
11+
def protobuf_deserializer(message):
12+
feature = route_guide_pb2.Feature()
13+
feature.ParseFromString(message)
14+
return feature
15+
16+
extra_media_type_deserializers = {
17+
'application/protobuf': protobuf_deserializer,
18+
}
19+
20+
config = Config(
21+
extra_media_type_deserializers=extra_media_type_deserializers,
22+
)
23+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
24+
25+
result = openapi.unmarshal_response(request, response)

docs/customizations/index.rst

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Customizations
2+
==============
3+
4+
OpenAPI accepts ``Config`` object that allows users to customize the behavior validation and unmarshalling processes.
5+
6+
.. toctree::
7+
:maxdepth: 1
8+
9+
spec_validator_cls
10+
request_validator_cls
11+
response_validator_cls
12+
request_unmarshaller_cls
13+
response_unmarshaller_cls
14+
extra_media_type_deserializers
15+
extra_format_validators
16+
extra_format_unmarshallers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Request unmarshaller
2+
====================
3+
4+
By default, request unmarshaller is selected based on detected specification version.
5+
6+
In order to explicitly validate and unmarshal a:
7+
8+
* OpenAPI 3.0 spec, import ``V30RequestUnmarshaller``
9+
* OpenAPI 3.1 spec, import ``V31RequestUnmarshaller`` or ``V31WebhookRequestUnmarshaller``
10+
11+
.. code-block:: python
12+
:emphasize-lines: 1,4
13+
14+
from openapi_core import V31RequestUnmarshaller
15+
16+
config = Config(
17+
request_unmarshaller_cls=V31RequestUnmarshaller,
18+
)
19+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
20+
result = openapi.unmarshal_request(request)
21+
22+
You can also explicitly import ``V3RequestUnmarshaller`` which is a shortcut to the latest OpenAPI v3 version.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Request validator
2+
=================
3+
4+
By default, request validator is selected based on detected specification version.
5+
6+
In order to explicitly validate a:
7+
8+
* OpenAPI 3.0 spec, import ``V30RequestValidator``
9+
* OpenAPI 3.1 spec, import ``V31RequestValidator`` or ``V31WebhookRequestValidator``
10+
11+
.. code-block:: python
12+
:emphasize-lines: 1,4
13+
14+
from openapi_core import V31RequestValidator
15+
16+
config = Config(
17+
request_validator_cls=V31RequestValidator,
18+
)
19+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
20+
openapi.validate_request(request)
21+
22+
You can also explicitly import ``V3RequestValidator`` which is a shortcut to the latest OpenAPI v3 version.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Response unmarshaller
2+
=====================
3+
4+
In order to explicitly validate and unmarshal a:
5+
6+
* OpenAPI 3.0 spec, import ``V30ResponseUnmarshaller``
7+
* OpenAPI 3.1 spec, import ``V31ResponseUnmarshaller`` or ``V31WebhookResponseUnmarshaller``
8+
9+
.. code-block:: python
10+
:emphasize-lines: 1,4
11+
12+
from openapi_core import V31ResponseUnmarshaller
13+
14+
config = Config(
15+
response_unmarshaller_cls=V31ResponseUnmarshaller,
16+
)
17+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
18+
result = openapi.unmarshal_response(request, response)
19+
20+
You can also explicitly import ``V3ResponseUnmarshaller`` which is a shortcut to the latest OpenAPI v3 version.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Response validator
2+
==================
3+
4+
By default, response validator is selected based on detected specification version.
5+
6+
In order to explicitly validate a:
7+
8+
* OpenAPI 3.0 spec, import ``V30ResponseValidator``
9+
* OpenAPI 3.1 spec, import ``V31ResponseValidator`` or ``V31WebhookResponseValidator``
10+
11+
.. code-block:: python
12+
:emphasize-lines: 1,4
13+
14+
from openapi_core import V31ResponseValidator
15+
16+
config = Config(
17+
response_validator_cls=V31ResponseValidator,
18+
)
19+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
20+
openapi.validate_response(request, response)
21+
22+
You can also explicitly import ``V3ResponseValidator`` which is a shortcut to the latest OpenAPI v3 version.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Specification validation
2+
========================
3+
4+
By default, on OpenAPI creation time, the provided specification is also validated.
5+
6+
If you know you have a valid specification already, disabling the validator can improve the performance.
7+
8+
.. code-block:: python
9+
:emphasize-lines: 1,4,6
10+
11+
from openapi_core import Config
12+
13+
config = Config(
14+
spec_validator_cls=None,
15+
)
16+
openapi = OpenAPI.from_file_path('openapi.json', config=config)

docs/extensions.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ By default, objects are unmarshalled to dictionaries. You can use dynamically cr
99
.. code-block:: yaml
1010
:emphasize-lines: 5
1111
12-
...
12+
# ...
1313
components:
1414
schemas:
1515
Coordinates:

docs/index.rst

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ openapi-core
33

44
.. toctree::
55
:hidden:
6-
:maxdepth: 2
6+
:maxdepth: 3
77

88
unmarshalling
99
validation
10-
integrations
11-
customizations
10+
integrations/index
11+
customizations/index
1212
security
1313
extensions
1414
contributing
15+
api
1516

1617
Openapi-core is a Python library that adds client-side and server-side support
1718
for the `OpenAPI v3.0 <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md>`__
@@ -21,8 +22,8 @@ Key features
2122
------------
2223

2324
* :doc:`validation` and :doc:`unmarshalling <unmarshalling>` of request and response data (including webhooks)
24-
* :doc:`Integrations <integrations>` with popular libraries (Requests, Werkzeug) and frameworks (Django, Falcon, Flask, Starlette)
25-
* :doc:`Customization <customizations>` with **media type deserializers** and **format unmarshallers**
25+
* :doc:`Integrations <integrations/index>` with popular libraries (Requests, Werkzeug) and frameworks (Django, Falcon, Flask, Starlette)
26+
* :doc:`Customization <customizations/index>` with **media type deserializers** and **format unmarshallers**
2627
* :doc:`Security <security>` data providers (API keys, Cookie, Basic and Bearer HTTP authentications)
2728

2829
Installation
@@ -74,7 +75,7 @@ Retrieve validated and unmarshalled request data
7475
# get security data
7576
security = result.security
7677
77-
Request object should implement OpenAPI Request protocol. Check :doc:`integrations` to find oficially supported implementations.
78+
Request object should implement OpenAPI Request protocol. Check :doc:`integrations/index` to find oficially supported implementations.
7879

7980
For more details read about :doc:`unmarshalling` process.
8081

0 commit comments

Comments
 (0)