Skip to content

Commit 75d58ff

Browse files
committed
docs code highlight improvement
1 parent 748ecb8 commit 75d58ff

File tree

5 files changed

+72
-15
lines changed

5 files changed

+72
-15
lines changed

docs/customizations.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@ By default, the provided specification is validated on ``Spec`` object creation
99
If you know you have a valid specification already, disabling the validator can improve the performance.
1010

1111
.. code-block:: python
12+
:emphasize-lines: 5
1213
1314
from openapi_core import Spec
1415
15-
spec = Spec.from_dict(spec_dict, validator=None)
16+
spec = Spec.from_dict(
17+
spec_dict,
18+
validator=None,
19+
)
1620
1721
Media type deserializers
1822
------------------------
1923

2024
Pass custom defined media type deserializers dictionary with supported mimetypes as a key to `unmarshal_response` function:
2125

2226
.. code-block:: python
27+
:emphasize-lines: 13
2328
2429
def protobuf_deserializer(message):
2530
feature = route_guide_pb2.Feature()
@@ -46,6 +51,7 @@ OpenAPI comes with a set of built-in format validators, but it's also possible t
4651
Here's how you could add support for a ``usdate`` format that handles dates of the form MM/DD/YYYY:
4752

4853
.. code-block:: python
54+
:emphasize-lines: 13
4955
5056
import re
5157
@@ -72,6 +78,7 @@ Openapi-core comes with a set of built-in format unmarshallers, but it's also po
7278
Here's an example with the ``usdate`` format that converts a value to date object:
7379

7480
.. code-block:: python
81+
:emphasize-lines: 13
7582
7683
from datetime import datetime
7784

docs/extensions.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ Extensions
44
x-model
55
-------
66

7-
By default, objects are unmarshalled to dictionaries. You can use dynamically created dataclasses.
7+
By default, objects are unmarshalled to dictionaries. You can use dynamically created dataclasses by providing ``x-model-path`` property inside schema definition with name of the model.
88

99
.. code-block:: yaml
10+
:emphasize-lines: 5
1011
1112
...
1213
components:
@@ -23,10 +24,16 @@ By default, objects are unmarshalled to dictionaries. You can use dynamically cr
2324
lon:
2425
type: number
2526
27+
As a result of unmarshalling process, you will get ``Coordinates`` class instance with ``lat`` and ``lon`` attributes.
28+
29+
30+
x-model-path
31+
------------
2632

2733
You can use your own dataclasses, pydantic models or models generated by third party generators (i.e. `datamodel-code-generator <https://github.com/koxudaxi/datamodel-code-generator>`__) by providing ``x-model-path`` property inside schema definition with location of your class.
2834

2935
.. code-block:: yaml
36+
:emphasize-lines: 5
3037
3138
...
3239
components:
@@ -52,3 +59,5 @@ You can use your own dataclasses, pydantic models or models generated by third p
5259
class Coordinates:
5360
lat: float
5461
lon: float
62+
63+
As a result of unmarshalling process, you will get instance of your own dataclasses or model.

docs/integrations.rst

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Middleware
2121
Django can be integrated by middleware. Add ``DjangoOpenAPIMiddleware`` to your ``MIDDLEWARE`` list and define ``OPENAPI_SPEC``.
2222

2323
.. code-block:: python
24+
:emphasize-lines: 6,9
2425
2526
# settings.py
2627
from openapi_core import Spec
@@ -87,11 +88,16 @@ Middleware
8788
The Falcon API can be integrated by ``FalconOpenAPIMiddleware`` middleware.
8889

8990
.. code-block:: python
91+
:emphasize-lines: 1,3,7
9092
9193
from openapi_core.contrib.falcon.middlewares import FalconOpenAPIMiddleware
9294
9395
openapi_middleware = FalconOpenAPIMiddleware.from_spec(spec)
94-
app = falcon.App(middleware=[openapi_middleware])
96+
97+
app = falcon.App(
98+
# ...
99+
middleware=[openapi_middleware],
100+
)
95101
96102
After that you will have access to validation result object with all validated request data from Falcon view through request context.
97103

@@ -145,6 +151,7 @@ Decorator
145151
Flask views can be integrated by ``FlaskOpenAPIViewDecorator`` decorator.
146152

147153
.. code-block:: python
154+
:emphasize-lines: 1,3,6
148155
149156
from openapi_core.contrib.flask.decorators import FlaskOpenAPIViewDecorator
150157
@@ -153,44 +160,58 @@ Flask views can be integrated by ``FlaskOpenAPIViewDecorator`` decorator.
153160
@app.route('/home')
154161
@openapi
155162
def home():
156-
pass
163+
return "Welcome home"
157164
158165
If you want to decorate class based view you can use the decorators attribute:
159166

160167
.. code-block:: python
168+
:emphasize-lines: 2
161169
162170
class MyView(View):
163171
decorators = [openapi]
164172
173+
def dispatch_request(self):
174+
return "Welcome home"
175+
176+
app.add_url_rule('/home', view_func=MyView.as_view('home'))
177+
165178
View
166179
~~~~
167180

168181
As an alternative to the decorator-based integration, a Flask method based views can be integrated by inheritance from ``FlaskOpenAPIView`` class.
169182

170183
.. code-block:: python
184+
:emphasize-lines: 1,3,8
171185
172186
from openapi_core.contrib.flask.views import FlaskOpenAPIView
173187
174188
class MyView(FlaskOpenAPIView):
175-
pass
189+
def get(self):
190+
return "Welcome home"
176191
177-
app.add_url_rule('/home', view_func=MyView.as_view('home', spec))
192+
app.add_url_rule(
193+
'/home',
194+
view_func=MyView.as_view('home', spec),
195+
)
178196
179197
Request parameters
180198
~~~~~~~~~~~~~~~~~~
181199

182200
In Flask, all unmarshalled request data are provided as Flask request object's ``openapi.parameters`` attribute
183201

184202
.. code-block:: python
203+
:emphasize-lines: 6,7
185204
186205
from flask.globals import request
187206
188207
@app.route('/browse/<id>/')
189208
@openapi
190-
def home():
209+
def browse(id):
191210
browse_id = request.openapi.parameters.path['id']
192211
page = request.openapi.parameters.query.get('page', 1)
193212
213+
return f"Browse {browse_id}, page {page}"
214+
194215
Low level
195216
~~~~~~~~~
196217

docs/unmarshalling.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,16 @@ In order to explicitly validate and unmarshal a:
6161
* OpenAPI 3.0 spec, import ``V30RequestUnmarshaller``
6262
* OpenAPI 3.1 spec, import ``V31RequestUnmarshaller`` or ``V31WebhookRequestUnmarshaller``
6363

64-
.. code:: python
64+
.. code-block:: python
65+
:emphasize-lines: 1,6
6566
6667
from openapi_core import V31RequestUnmarshaller
6768
68-
result = unmarshal_request(request, response, spec=spec, cls=V31RequestUnmarshaller)
69+
result = unmarshal_request(
70+
request, response,
71+
spec=spec,
72+
cls=V31RequestUnmarshaller,
73+
)
6974
7075
You can also explicitly import ``V3RequestUnmarshaller`` which is a shortcut to the latest OpenAPI v3 version.
7176

@@ -108,10 +113,15 @@ In order to explicitly validate and unmarshal a:
108113
* OpenAPI 3.0 spec, import ``V30ResponseUnmarshaller``
109114
* OpenAPI 3.1 spec, import ``V31ResponseUnmarshaller`` or ``V31WebhookResponseUnmarshaller``
110115

111-
.. code:: python
116+
.. code-block:: python
117+
:emphasize-lines: 1,6
112118
113119
from openapi_core import V31ResponseUnmarshaller
114120
115-
result = unmarshal_response(request, response, spec=spec, cls=V31ResponseUnmarshaller)
121+
result = unmarshal_response(
122+
request, response,
123+
spec=spec,
124+
cls=V31ResponseUnmarshaller,
125+
)
116126
117127
You can also explicitly import ``V3ResponseUnmarshaller`` which is a shortcut to the latest OpenAPI v3 version.

docs/validation.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ In order to explicitly validate and unmarshal a:
4343
* OpenAPI 3.0 spec, import ``V30RequestValidator``
4444
* OpenAPI 3.1 spec, import ``V31RequestValidator`` or ``V31WebhookRequestValidator``
4545

46-
.. code:: python
46+
.. code-block:: python
47+
:emphasize-lines: 1,6
4748
4849
from openapi_core import V31RequestValidator
4950
50-
validate_request(request, response, spec=spec, cls=V31RequestValidator)
51+
validate_request(
52+
request, response,
53+
spec=spec,
54+
cls=V31RequestValidator,
55+
)
5156
5257
You can also explicitly import ``V3RequestValidator`` which is a shortcut to the latest OpenAPI v3 version.
5358

@@ -81,10 +86,15 @@ In order to explicitly validate a:
8186
* OpenAPI 3.0 spec, import ``V30ResponseValidator``
8287
* OpenAPI 3.1 spec, import ``V31ResponseValidator`` or ``V31WebhookResponseValidator``
8388

84-
.. code:: python
89+
.. code-block:: python
90+
:emphasize-lines: 1,6
8591
8692
from openapi_core import V31ResponseValidator
8793
88-
validate_response(request, response, spec=spec, cls=V31ResponseValidator)
94+
validate_response(
95+
request, response,
96+
spec=spec,
97+
cls=V31ResponseValidator,
98+
)
8999
90100
You can also explicitly import ``V3ResponseValidator`` which is a shortcut to the latest OpenAPI v3 version.

0 commit comments

Comments
 (0)