|
| 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 | +``` |
0 commit comments