Skip to content

Commit 9e3c06f

Browse files
committed
Reference docs
1 parent d3deb81 commit 9e3c06f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+874
-450
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Byte-compiled / optimized / DLL files
2-
__pycache__/
2+
**/__pycache__/
33
*.py[cod]
44
*$py.class
55
.pytest_cache/
@@ -63,7 +63,7 @@ instance/
6363
.scrapy
6464

6565
# Sphinx documentation
66-
docs/_build/
66+
docs_build/
6767

6868
# PyBuilder
6969
target/

README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@
2121

2222
## About
2323

24-
Openapi-core is a Python library that adds client-side and server-side support
24+
Openapi-core is a Python library that provides client-side and server-side support
2525
for the [OpenAPI v3.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md)
26-
and [OpenAPI v3.1](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md) specification.
26+
and [OpenAPI v3.1](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md) specifications.
2727

2828

2929
## Key features
3030

3131
- **Validation** and **unmarshalling** of request and response data (including webhooks)
3232
- **Integration** with popular libraries (Requests, Werkzeug) and frameworks (Django, Falcon, Flask, Starlette)
3333
- Customization with media type **deserializers** and format **unmarshallers**
34-
- **Security** data providers (API keys, Cookie, Basic and Bearer HTTP authentications)
34+
- **Security** data providers (API keys, Cookie, Basic, and Bearer HTTP authentications)
3535

3636

3737
## Documentation
@@ -56,7 +56,7 @@ pip install -e git+https://github.com/python-openapi/openapi-core.git#egg=openap
5656

5757
## First steps
5858

59-
Firstly create your OpenAPI object.
59+
First, create your OpenAPI object.
6060

6161
``` python
6262
from openapi_core import OpenAPI
@@ -67,11 +67,11 @@ openapi = OpenAPI.from_file_path('openapi.json')
6767
Now you can use it to validate and unmarshal against requests and/or responses.
6868

6969
``` python
70-
# raises error if request is invalid
70+
# raises an error if the request is invalid
7171
result = openapi.unmarshal_request(request)
7272
```
7373

74-
Retrieve validated and unmarshalled request data
74+
Retrieve validated and unmarshalled request data.
7575

7676
``` python
7777
# get parameters
@@ -85,19 +85,19 @@ body = result.body
8585
security = result.security
8686
```
8787

88-
Request object should implement OpenAPI Request protocol. Check [Integrations](https://openapi-core.readthedocs.io/en/latest/integrations.html) to find officially supported implementations.
88+
The request object should implement the OpenAPI Request protocol. Check [Integrations](https://openapi-core.readthedocs.io/en/latest/integrations.html) to find officially supported implementations.
8989

90-
For more details read about [Unmarshalling](https://openapi-core.readthedocs.io/en/latest/unmarshalling.html) process.
90+
For more details read about the [Unmarshalling](https://openapi-core.readthedocs.io/en/latest/unmarshalling.html) process.
9191

9292
If you just want to validate your request/response data without unmarshalling, read about [Validation](https://openapi-core.readthedocs.io/en/latest/validation.html) instead.
9393

9494

9595
## Related projects
9696

9797
- [openapi-spec-validator](https://github.com/python-openapi/openapi-spec-validator)
98-
: Python library that validates OpenAPI Specs against the OpenAPI 2.0 (aka Swagger), OpenAPI 3.0 and OpenAPI 3.1 specification. The validator aims to check for full compliance with the Specification.
98+
: A Python library that validates OpenAPI Specs against the OpenAPI 2.0 (aka Swagger), OpenAPI 3.0, and OpenAPI 3.1 specification. The validator aims to check for full compliance with the Specification.
9999
- [openapi-schema-validator](https://github.com/python-openapi/openapi-schema-validator)
100-
: Python library that validates schema against the OpenAPI Schema Specification v3.0 and OpenAPI Schema Specification v3.1.
100+
: A Python library that validates schema against the OpenAPI Schema Specification v3.0 and OpenAPI Schema Specification v3.1.
101101
- [bottle-openapi-3](https://github.com/cope-systems/bottle-openapi-3)
102102
: OpenAPI 3.0 Support for the Bottle Web Framework
103103
- [pyramid_openapi3](https://github.com/niteoweb/pyramid_openapi3)
@@ -107,4 +107,4 @@ If you just want to validate your request/response data without unmarshalling, r
107107

108108
## License
109109

110-
The project is under the terms of BSD 3-Clause License.
110+
The project is under the terms of the BSD 3-Clause License.

docs/configuration.md

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
---
2+
hide:
3+
- navigation
4+
---
5+
6+
# Configuration
7+
8+
OpenAPI accepts a `Config` object that allows users to customize the behavior of validation and unmarshalling processes.
9+
10+
## Specification Validation
11+
12+
By default, when creating an OpenAPI instance, the provided specification is also validated.
13+
14+
If you know that you have a valid specification already, disabling the validator can improve performance.
15+
16+
``` python hl_lines="1 4 6"
17+
from openapi_core import Config
18+
19+
config = Config(
20+
spec_validator_cls=None,
21+
)
22+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
23+
```
24+
25+
## Request Validator
26+
27+
By default, the request validator is selected based on the detected specification version.
28+
29+
To explicitly validate a:
30+
31+
- OpenAPI 3.0 spec, import `V30RequestValidator`
32+
- OpenAPI 3.1 spec, import `V31RequestValidator` or `V31WebhookRequestValidator`
33+
34+
``` python hl_lines="1 4"
35+
from openapi_core import V31RequestValidator
36+
37+
config = Config(
38+
request_validator_cls=V31RequestValidator,
39+
)
40+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
41+
openapi.validate_request(request)
42+
```
43+
44+
You can also explicitly import `V3RequestValidator`, which is a shortcut to the latest OpenAPI v3 version.
45+
46+
## Response Validator
47+
48+
By default, the response validator is selected based on the detected specification version.
49+
50+
To explicitly validate a:
51+
52+
- OpenAPI 3.0 spec, import `V30ResponseValidator`
53+
- OpenAPI 3.1 spec, import `V31ResponseValidator` or `V31WebhookResponseValidator`
54+
55+
``` python hl_lines="1 4"
56+
from openapi_core import V31ResponseValidator
57+
58+
config = Config(
59+
response_validator_cls=V31ResponseValidator,
60+
)
61+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
62+
openapi.validate_response(request, response)
63+
```
64+
65+
You can also explicitly import `V3ResponseValidator`, which is a shortcut to the latest OpenAPI v3 version.
66+
67+
## Request Unmarshaller
68+
69+
By default, the request unmarshaller is selected based on the detected specification version.
70+
71+
To explicitly validate and unmarshal a request for:
72+
73+
- OpenAPI 3.0 spec, import `V30RequestUnmarshaller`
74+
- OpenAPI 3.1 spec, import `V31RequestUnmarshaller` or `V31WebhookRequestUnmarshaller`
75+
76+
``` python hl_lines="1 4"
77+
from openapi_core import V31RequestUnmarshaller
78+
79+
config = Config(
80+
request_unmarshaller_cls=V31RequestUnmarshaller,
81+
)
82+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
83+
result = openapi.unmarshal_request(request)
84+
```
85+
86+
You can also explicitly import `V3RequestUnmarshaller`, which is a shortcut to the latest OpenAPI v3 version.
87+
88+
## Response Unmarshaller
89+
90+
To explicitly validate and unmarshal a response:
91+
92+
- For OpenAPI 3.0 spec, import `V30ResponseUnmarshaller`
93+
- For OpenAPI 3.1 spec, import `V31ResponseUnmarshaller` or `V31WebhookResponseUnmarshaller`
94+
95+
``` python hl_lines="1 4"
96+
from openapi_core import V31ResponseUnmarshaller
97+
98+
config = Config(
99+
response_unmarshaller_cls=V31ResponseUnmarshaller,
100+
)
101+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
102+
result = openapi.unmarshal_response(request, response)
103+
```
104+
105+
You can also explicitly import `V3ResponseUnmarshaller`, which is a shortcut to the latest OpenAPI v3 version.
106+
107+
## Extra Media Type Deserializers
108+
109+
The library comes with a set of built-in media type deserializers for formats such as `application/json`, `application/xml`, `application/x-www-form-urlencoded`, and `multipart/form-data`.
110+
111+
You can also define your own deserializers. To do this, pass a dictionary of custom media type deserializers with the supported MIME types as keys to the `unmarshal_response` function:
112+
113+
```python hl_lines="11"
114+
def protobuf_deserializer(message):
115+
feature = route_guide_pb2.Feature()
116+
feature.ParseFromString(message)
117+
return feature
118+
119+
extra_media_type_deserializers = {
120+
'application/protobuf': protobuf_deserializer,
121+
}
122+
123+
config = Config(
124+
extra_media_type_deserializers=extra_media_type_deserializers,
125+
)
126+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
127+
128+
result = openapi.unmarshal_response(request, response)
129+
```
130+
131+
## Extra Format Validators
132+
133+
OpenAPI defines a `format` keyword that hints at how a value should be interpreted. For example, a `string` with the format `date` should conform to the RFC 3339 date format.
134+
135+
OpenAPI comes with a set of built-in format validators, but it's also possible to add custom ones.
136+
137+
Here's how you can add support for a `usdate` format that handles dates in the form MM/DD/YYYY:
138+
139+
``` python hl_lines="11"
140+
import re
141+
142+
def validate_usdate(value):
143+
return bool(re.match(r"^\d{1,2}/\d{1,2}/\d{4}$", value))
144+
145+
extra_format_validators = {
146+
'usdate': validate_usdate,
147+
}
148+
149+
config = Config(
150+
extra_format_validators=extra_format_validators,
151+
)
152+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
153+
154+
openapi.validate_response(request, response)
155+
```
156+
157+
## Extra Format Unmarshallers
158+
159+
Based on the `format` keyword, openapi-core can also unmarshal values to specific formats.
160+
161+
The library comes with a set of built-in format unmarshallers, but it's also possible to add custom ones.
162+
163+
Here's an example with the `usdate` format that converts a value to a date object:
164+
165+
``` python hl_lines="11"
166+
from datetime import datetime
167+
168+
def unmarshal_usdate(value):
169+
return datetime.strptime(value, "%m/%d/%Y").date()
170+
171+
extra_format_unmarshallers = {
172+
'usdate': unmarshal_usdate,
173+
}
174+
175+
config = Config(
176+
extra_format_unmarshallers=extra_format_unmarshallers,
177+
)
178+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
179+
180+
result = openapi.unmarshal_response(request, response)
181+
```

docs/contributing.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ hide:
55

66
# Contributing
77

8-
Firstly, thank you all for taking the time to contribute.
8+
Firstly, thank you for taking the time to contribute.
99

1010
The following section describes how you can contribute to the openapi-core project on GitHub.
1111

1212
## Reporting bugs
1313

1414
### Before you report
1515

16-
- Check whether your issue does not already exist in the [Issue tracker](https://github.com/python-openapi/openapi-core/issues).
17-
- Make sure it is not a support request or question better suited for [Discussion board](https://github.com/python-openapi/openapi-core/discussions).
16+
- Check whether your issue already exists in the [Issue tracker](https://github.com/python-openapi/openapi-core/issues).
17+
- Make sure it is not a support request or question better suited for the [Discussion board](https://github.com/python-openapi/openapi-core/discussions).
1818

1919
### How to submit a report
2020

21-
- Include clear title.
22-
- Describe your runtime environment with exact versions you use.
23-
- Describe the exact steps which reproduce the problem, including minimal code snippets.
24-
- Describe the behavior you observed after following the steps, pasting console outputs.
25-
- Describe expected behavior to see and why, including links to documentations.
21+
- Include a clear title.
22+
- Describe your runtime environment with the exact versions you use.
23+
- Describe the exact steps to reproduce the problem, including minimal code snippets.
24+
- Describe the behavior you observed after following the steps, including console outputs.
25+
- Describe the expected behavior and why, including links to documentation.
2626

2727
## Code contribution
2828

@@ -50,9 +50,9 @@ poetry shell
5050

5151
### Static checks
5252

53-
The project uses static checks using fantastic [pre-commit](https://pre-commit.com/). Every change is checked on CI and if it does not pass the tests it cannot be accepted. If you want to check locally then run following command to install pre-commit.
53+
The project uses static checks with the fantastic [pre-commit](https://pre-commit.com/). Every change is checked on CI, and if it does not pass the tests, it cannot be accepted. If you want to check locally, run the following command to install pre-commit.
5454

55-
To turn on pre-commit checks for commit operations in git, enter:
55+
To enable pre-commit checks for commit operations in git, enter:
5656

5757
```console
5858
pre-commit install
@@ -70,4 +70,4 @@ To run all checks on all files, enter:
7070
pre-commit run --all-files
7171
```
7272

73-
Pre-commit check results are also attached to your PR through integration with Github Action.
73+
Pre-commit check results are also attached to your PR through integration with GitHub Actions.

docs/customizations/extra_format_unmarshallers.md

-26
This file was deleted.

docs/customizations/extra_format_validators.md

-26
This file was deleted.

0 commit comments

Comments
 (0)