Skip to content

Commit 840bfbd

Browse files
authored
Merge pull request #732 from python-openapi/fix/docs-integrations-restructure
Docs restructure
2 parents 88c78dc + c4d1ef2 commit 840bfbd

29 files changed

+759
-717
lines changed

docs/api.rst

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

docs/conf.py

Lines changed: 1 addition & 0 deletions
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

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
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)
Lines changed: 27 additions & 0 deletions
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)
Lines changed: 25 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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
Lines changed: 22 additions & 0 deletions
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.
Lines changed: 22 additions & 0 deletions
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.
Lines changed: 20 additions & 0 deletions
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.

0 commit comments

Comments
 (0)