File: unmarshallers.py

package info (click to toggle)
python-openapi-core 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,008 kB
  • sloc: python: 18,868; makefile: 47
file content (436 lines) | stat: -rw-r--r-- 15,751 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
from typing import Optional

from jsonschema_path import SchemaPath
from openapi_spec_validator.validation.types import SpecValidatorType

from openapi_core.casting.schemas.factories import SchemaCastersFactory
from openapi_core.deserializing.media_types import (
    media_type_deserializers_factory,
)
from openapi_core.deserializing.media_types.datatypes import (
    MediaTypeDeserializersDict,
)
from openapi_core.deserializing.media_types.factories import (
    MediaTypeDeserializersFactory,
)
from openapi_core.deserializing.styles import style_deserializers_factory
from openapi_core.deserializing.styles.factories import (
    StyleDeserializersFactory,
)
from openapi_core.protocols import BaseRequest
from openapi_core.protocols import Request
from openapi_core.protocols import WebhookRequest
from openapi_core.security import security_provider_factory
from openapi_core.security.factories import SecurityProviderFactory
from openapi_core.templating.paths.exceptions import PathError
from openapi_core.templating.paths.types import PathFinderType
from openapi_core.unmarshalling.request.datatypes import RequestUnmarshalResult
from openapi_core.unmarshalling.schemas import (
    oas30_write_schema_unmarshallers_factory,
)
from openapi_core.unmarshalling.schemas import (
    oas31_schema_unmarshallers_factory,
)
from openapi_core.unmarshalling.schemas.datatypes import (
    FormatUnmarshallersDict,
)
from openapi_core.unmarshalling.schemas.factories import (
    SchemaUnmarshallersFactory,
)
from openapi_core.unmarshalling.unmarshallers import BaseUnmarshaller
from openapi_core.util import chainiters
from openapi_core.validation.request.exceptions import MissingRequestBody
from openapi_core.validation.request.exceptions import ParametersError
from openapi_core.validation.request.exceptions import (
    RequestBodyValidationError,
)
from openapi_core.validation.request.exceptions import SecurityValidationError
from openapi_core.validation.request.validators import APICallRequestValidator
from openapi_core.validation.request.validators import BaseRequestValidator
from openapi_core.validation.request.validators import V30RequestBodyValidator
from openapi_core.validation.request.validators import (
    V30RequestParametersValidator,
)
from openapi_core.validation.request.validators import (
    V30RequestSecurityValidator,
)
from openapi_core.validation.request.validators import V30RequestValidator
from openapi_core.validation.request.validators import V31RequestBodyValidator
from openapi_core.validation.request.validators import (
    V31RequestParametersValidator,
)
from openapi_core.validation.request.validators import (
    V31RequestSecurityValidator,
)
from openapi_core.validation.request.validators import V31RequestValidator
from openapi_core.validation.request.validators import (
    V31WebhookRequestBodyValidator,
)
from openapi_core.validation.request.validators import (
    V31WebhookRequestParametersValidator,
)
from openapi_core.validation.request.validators import (
    V31WebhookRequestSecurityValidator,
)
from openapi_core.validation.request.validators import (
    V31WebhookRequestValidator,
)
from openapi_core.validation.request.validators import WebhookRequestValidator
from openapi_core.validation.schemas.datatypes import FormatValidatorsDict
from openapi_core.validation.schemas.factories import SchemaValidatorsFactory


class BaseRequestUnmarshaller(BaseRequestValidator, BaseUnmarshaller):
    def __init__(
        self,
        spec: SchemaPath,
        base_url: Optional[str] = None,
        style_deserializers_factory: StyleDeserializersFactory = style_deserializers_factory,
        media_type_deserializers_factory: MediaTypeDeserializersFactory = media_type_deserializers_factory,
        schema_casters_factory: Optional[SchemaCastersFactory] = None,
        schema_validators_factory: Optional[SchemaValidatorsFactory] = None,
        path_finder_cls: Optional[PathFinderType] = None,
        spec_validator_cls: Optional[SpecValidatorType] = None,
        format_validators: Optional[FormatValidatorsDict] = None,
        extra_format_validators: Optional[FormatValidatorsDict] = None,
        extra_media_type_deserializers: Optional[
            MediaTypeDeserializersDict
        ] = None,
        security_provider_factory: SecurityProviderFactory = security_provider_factory,
        schema_unmarshallers_factory: Optional[
            SchemaUnmarshallersFactory
        ] = None,
        format_unmarshallers: Optional[FormatUnmarshallersDict] = None,
        extra_format_unmarshallers: Optional[FormatUnmarshallersDict] = None,
    ):
        BaseUnmarshaller.__init__(
            self,
            spec,
            base_url=base_url,
            style_deserializers_factory=style_deserializers_factory,
            media_type_deserializers_factory=media_type_deserializers_factory,
            schema_casters_factory=schema_casters_factory,
            schema_validators_factory=schema_validators_factory,
            path_finder_cls=path_finder_cls,
            spec_validator_cls=spec_validator_cls,
            format_validators=format_validators,
            extra_format_validators=extra_format_validators,
            extra_media_type_deserializers=extra_media_type_deserializers,
            schema_unmarshallers_factory=schema_unmarshallers_factory,
            format_unmarshallers=format_unmarshallers,
            extra_format_unmarshallers=extra_format_unmarshallers,
        )
        BaseRequestValidator.__init__(
            self,
            spec,
            base_url=base_url,
            style_deserializers_factory=style_deserializers_factory,
            media_type_deserializers_factory=media_type_deserializers_factory,
            schema_casters_factory=schema_casters_factory,
            schema_validators_factory=schema_validators_factory,
            path_finder_cls=path_finder_cls,
            spec_validator_cls=spec_validator_cls,
            format_validators=format_validators,
            extra_format_validators=extra_format_validators,
            extra_media_type_deserializers=extra_media_type_deserializers,
            security_provider_factory=security_provider_factory,
        )

    def _unmarshal(
        self, request: BaseRequest, operation: SchemaPath, path: SchemaPath
    ) -> RequestUnmarshalResult:
        try:
            security = self._get_security(request.parameters, operation)
        except SecurityValidationError as exc:
            return RequestUnmarshalResult(errors=[exc])

        try:
            params = self._get_parameters(request.parameters, operation, path)
        except ParametersError as exc:
            params = exc.parameters
            params_errors = exc.errors
        else:
            params_errors = []

        try:
            body = self._get_body(
                request.body, request.content_type, operation
            )
        except MissingRequestBody:
            body = None
            body_errors = []
        except RequestBodyValidationError as exc:
            body = None
            body_errors = [exc]
        else:
            body_errors = []

        errors = list(chainiters(params_errors, body_errors))
        return RequestUnmarshalResult(
            errors=errors,
            body=body,
            parameters=params,
            security=security,
        )

    def _unmarshal_body(
        self, request: BaseRequest, operation: SchemaPath, path: SchemaPath
    ) -> RequestUnmarshalResult:
        try:
            body = self._get_body(
                request.body, request.content_type, operation
            )
        except MissingRequestBody:
            body = None
            errors = []
        except RequestBodyValidationError as exc:
            body = None
            errors = [exc]
        else:
            errors = []

        return RequestUnmarshalResult(
            errors=errors,
            body=body,
        )

    def _unmarshal_parameters(
        self, request: BaseRequest, operation: SchemaPath, path: SchemaPath
    ) -> RequestUnmarshalResult:
        try:
            params = self._get_parameters(request.parameters, path, operation)
        except ParametersError as exc:
            params = exc.parameters
            params_errors = exc.errors
        else:
            params_errors = []

        return RequestUnmarshalResult(
            errors=params_errors,
            parameters=params,
        )

    def _unmarshal_security(
        self, request: BaseRequest, operation: SchemaPath, path: SchemaPath
    ) -> RequestUnmarshalResult:
        try:
            security = self._get_security(request.parameters, operation)
        except SecurityValidationError as exc:
            return RequestUnmarshalResult(errors=[exc])

        return RequestUnmarshalResult(
            errors=[],
            security=security,
        )


class BaseAPICallRequestUnmarshaller(BaseRequestUnmarshaller):
    pass


class BaseWebhookRequestUnmarshaller(BaseRequestUnmarshaller):
    pass


class APICallRequestUnmarshaller(
    APICallRequestValidator, BaseAPICallRequestUnmarshaller
):
    def unmarshal(self, request: Request) -> RequestUnmarshalResult:
        try:
            path, operation, _, path_result, _ = self._find_path(request)
        # don't process if operation errors
        except PathError as exc:
            return RequestUnmarshalResult(errors=[exc])

        request.parameters.path = (
            request.parameters.path or path_result.variables
        )

        return self._unmarshal(request, operation, path)


class APICallRequestBodyUnmarshaller(
    APICallRequestValidator, BaseAPICallRequestUnmarshaller
):
    def unmarshal(self, request: Request) -> RequestUnmarshalResult:
        try:
            path, operation, _, path_result, _ = self._find_path(request)
        # don't process if operation errors
        except PathError as exc:
            return RequestUnmarshalResult(errors=[exc])

        request.parameters.path = (
            request.parameters.path or path_result.variables
        )

        return self._unmarshal_body(request, operation, path)


class APICallRequestParametersUnmarshaller(
    APICallRequestValidator, BaseAPICallRequestUnmarshaller
):
    def unmarshal(self, request: Request) -> RequestUnmarshalResult:
        try:
            path, operation, _, path_result, _ = self._find_path(request)
        # don't process if operation errors
        except PathError as exc:
            return RequestUnmarshalResult(errors=[exc])

        request.parameters.path = (
            request.parameters.path or path_result.variables
        )

        return self._unmarshal_parameters(request, operation, path)


class APICallRequestSecurityUnmarshaller(
    APICallRequestValidator, BaseAPICallRequestUnmarshaller
):
    def unmarshal(self, request: Request) -> RequestUnmarshalResult:
        try:
            path, operation, _, path_result, _ = self._find_path(request)
        # don't process if operation errors
        except PathError as exc:
            return RequestUnmarshalResult(errors=[exc])

        request.parameters.path = (
            request.parameters.path or path_result.variables
        )

        return self._unmarshal_security(request, operation, path)


class WebhookRequestUnmarshaller(
    WebhookRequestValidator, BaseWebhookRequestUnmarshaller
):
    def unmarshal(self, request: WebhookRequest) -> RequestUnmarshalResult:
        try:
            path, operation, _, path_result, _ = self._find_path(request)
        # don't process if operation errors
        except PathError as exc:
            return RequestUnmarshalResult(errors=[exc])

        request.parameters.path = (
            request.parameters.path or path_result.variables
        )

        return self._unmarshal(request, operation, path)


class WebhookRequestBodyUnmarshaller(
    WebhookRequestValidator, BaseWebhookRequestUnmarshaller
):
    def unmarshal(self, request: WebhookRequest) -> RequestUnmarshalResult:
        try:
            path, operation, _, path_result, _ = self._find_path(request)
        # don't process if operation errors
        except PathError as exc:
            return RequestUnmarshalResult(errors=[exc])

        request.parameters.path = (
            request.parameters.path or path_result.variables
        )

        return self._unmarshal_body(request, operation, path)


class WebhookRequestParametersUnmarshaller(
    WebhookRequestValidator, BaseWebhookRequestUnmarshaller
):
    def unmarshal(self, request: WebhookRequest) -> RequestUnmarshalResult:
        try:
            path, operation, _, path_result, _ = self._find_path(request)
        # don't process if operation errors
        except PathError as exc:
            return RequestUnmarshalResult(errors=[exc])

        request.parameters.path = (
            request.parameters.path or path_result.variables
        )

        return self._unmarshal_parameters(request, operation, path)


class WebhookRequestSecuritysUnmarshaller(
    WebhookRequestValidator, BaseWebhookRequestUnmarshaller
):
    def unmarshal(self, request: WebhookRequest) -> RequestUnmarshalResult:
        try:
            path, operation, _, path_result, _ = self._find_path(request)
        # don't process if operation errors
        except PathError as exc:
            return RequestUnmarshalResult(errors=[exc])

        request.parameters.path = (
            request.parameters.path or path_result.variables
        )

        return self._unmarshal_security(request, operation, path)


class V30RequestBodyUnmarshaller(
    V30RequestBodyValidator, APICallRequestBodyUnmarshaller
):
    schema_unmarshallers_factory = oas30_write_schema_unmarshallers_factory


class V30RequestParametersUnmarshaller(
    V30RequestParametersValidator, APICallRequestParametersUnmarshaller
):
    schema_unmarshallers_factory = oas30_write_schema_unmarshallers_factory


class V30RequestSecurityUnmarshaller(
    V30RequestSecurityValidator, APICallRequestSecurityUnmarshaller
):
    schema_unmarshallers_factory = oas30_write_schema_unmarshallers_factory


class V30RequestUnmarshaller(V30RequestValidator, APICallRequestUnmarshaller):
    schema_unmarshallers_factory = oas30_write_schema_unmarshallers_factory


class V31RequestBodyUnmarshaller(
    V31RequestBodyValidator, APICallRequestBodyUnmarshaller
):
    schema_unmarshallers_factory = oas31_schema_unmarshallers_factory


class V31RequestParametersUnmarshaller(
    V31RequestParametersValidator, APICallRequestParametersUnmarshaller
):
    schema_unmarshallers_factory = oas31_schema_unmarshallers_factory


class V31RequestSecurityUnmarshaller(
    V31RequestSecurityValidator, APICallRequestSecurityUnmarshaller
):
    schema_unmarshallers_factory = oas31_schema_unmarshallers_factory


class V31RequestUnmarshaller(V31RequestValidator, APICallRequestUnmarshaller):
    schema_unmarshallers_factory = oas31_schema_unmarshallers_factory


class V31WebhookRequestBodyUnmarshaller(
    V31WebhookRequestBodyValidator, WebhookRequestBodyUnmarshaller
):
    schema_unmarshallers_factory = oas31_schema_unmarshallers_factory


class V31WebhookRequestParametersUnmarshaller(
    V31WebhookRequestParametersValidator, WebhookRequestParametersUnmarshaller
):
    schema_unmarshallers_factory = oas31_schema_unmarshallers_factory


class V31WebhookRequestSecurityUnmarshaller(
    V31WebhookRequestSecurityValidator, WebhookRequestSecuritysUnmarshaller
):
    schema_unmarshallers_factory = oas31_schema_unmarshallers_factory


class V31WebhookRequestUnmarshaller(
    V31WebhookRequestValidator, WebhookRequestUnmarshaller
):
    schema_unmarshallers_factory = oas31_schema_unmarshallers_factory