File: unmarshalling.md

package info (click to toggle)
python-openapi-core 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,104 kB
  • sloc: python: 19,979; makefile: 44
file content (93 lines) | stat: -rw-r--r-- 3,012 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
---
hide:
  - navigation
---

# Unmarshalling

Unmarshalling is the process of converting a primitive schema type value into a higher-level object based on a `format` keyword. All request/response data that can be described by a schema in the OpenAPI specification can be unmarshalled.

Unmarshallers first validate data against the provided schema (See [Validation](validation.md)).

Openapi-core comes with a set of built-in format unmarshallers:

- `date` - converts a string into a date object,
- `date-time` - converts a string into a datetime object,
- `binary` - converts a string into a byte object,
- `uuid` - converts a string into a UUID object,
- `byte` - decodes a Base64-encoded string.

You can also define your own format unmarshallers (See [Extra Format Unmarshallers](configuration.md#extra-format-unmarshallers)).

## Request unmarshalling

Use the `unmarshal_request` method to validate and unmarshal request data against a given spec. By default, the OpenAPI spec version is detected:

```python
# raises an error if the request is invalid
result = openapi.unmarshal_request(request)
```

The request object should implement the OpenAPI Request protocol (See [Integrations](integrations/index.md)).

!!! note

    The Webhooks feature is part of OpenAPI v3.1 only.

Use the same method to validate and unmarshal webhook request data against a given spec.

```python
# raises an error if the request is invalid
result = openapi.unmarshal_request(webhook_request)
```

The webhook request object should implement the OpenAPI WebhookRequest protocol (See [Integrations](integrations/index.md)).

Retrieve validated and unmarshalled request data:

```python
# get parameters
path_params = result.parameters.path
query_params = result.parameters.query
cookies_params = result.parameters.cookies
headers_params = result.parameters.headers
# get body
body = result.body
# get security data
security = result.security
```

You can also define your own request unmarshaller (See [Request Unmarshaller](configuration.md#request-unmarshaller)).

## Response unmarshalling

Use the `unmarshal_response` method to validate and unmarshal response data against a given spec. By default, the OpenAPI spec version is detected:

```python
# raises an error if the response is invalid
result = openapi.unmarshal_response(request, response)
```

The response object should implement the OpenAPI Response protocol (See [Integrations](integrations/index.md)).

!!! note

    The Webhooks feature is part of OpenAPI v3.1 only.

Use the same method to validate and unmarshal response data from a webhook request against a given spec.

```python
# raises an error if the request is invalid
result = openapi.unmarshal_response(webhook_request, response)
```

Retrieve validated and unmarshalled response data:

```python
# get headers
headers = result.headers
# get data
data = result.data
```

You can also define your own response unmarshaller (See [Response Unmarshaller](configuration.md#response-unmarshaller)).