|
1 | 1 | import warnings
|
2 | 2 | from typing import Any
|
| 3 | +from typing import Mapping |
3 | 4 | from typing import Optional
|
| 5 | +from typing import cast |
4 | 6 | from xml.etree.ElementTree import ParseError
|
5 | 7 |
|
| 8 | +from jsonschema_path import SchemaPath |
| 9 | + |
6 | 10 | from openapi_core.deserializing.media_types.datatypes import (
|
7 | 11 | DeserializerCallable,
|
8 | 12 | )
|
| 13 | +from openapi_core.deserializing.media_types.datatypes import ( |
| 14 | + MediaTypeDeserializersDict, |
| 15 | +) |
9 | 16 | from openapi_core.deserializing.media_types.exceptions import (
|
10 | 17 | MediaTypeDeserializeError,
|
11 | 18 | )
|
| 19 | +from openapi_core.deserializing.styles.factories import ( |
| 20 | + StyleDeserializersFactory, |
| 21 | +) |
| 22 | +from openapi_core.schema.encodings import get_content_type |
| 23 | +from openapi_core.schema.parameters import get_style_and_explode |
| 24 | +from openapi_core.schema.protocols import SuportsGetAll |
| 25 | +from openapi_core.schema.protocols import SuportsGetList |
| 26 | +from openapi_core.schema.schemas import get_properties |
| 27 | + |
| 28 | + |
| 29 | +class MediaTypesDeserializer: |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + media_type_deserializers: Optional[MediaTypeDeserializersDict] = None, |
| 33 | + extra_media_type_deserializers: Optional[ |
| 34 | + MediaTypeDeserializersDict |
| 35 | + ] = None, |
| 36 | + ): |
| 37 | + if media_type_deserializers is None: |
| 38 | + media_type_deserializers = {} |
| 39 | + self.media_type_deserializers = media_type_deserializers |
| 40 | + if extra_media_type_deserializers is None: |
| 41 | + extra_media_type_deserializers = {} |
| 42 | + self.extra_media_type_deserializers = extra_media_type_deserializers |
| 43 | + |
| 44 | + def deserialize(self, mimetype: str, value: Any, **parameters: str) -> Any: |
| 45 | + deserializer_callable = self.get_deserializer_callable(mimetype) |
| 46 | + |
| 47 | + try: |
| 48 | + return deserializer_callable(value, **parameters) |
| 49 | + except (ParseError, ValueError, TypeError, AttributeError): |
| 50 | + raise MediaTypeDeserializeError(mimetype, value) |
| 51 | + |
| 52 | + def get_deserializer_callable( |
| 53 | + self, |
| 54 | + mimetype: str, |
| 55 | + ) -> DeserializerCallable: |
| 56 | + if mimetype in self.extra_media_type_deserializers: |
| 57 | + return self.extra_media_type_deserializers[mimetype] |
| 58 | + return self.media_type_deserializers[mimetype] |
12 | 59 |
|
13 | 60 |
|
14 |
| -class CallableMediaTypeDeserializer: |
| 61 | +class MediaTypeDeserializer: |
15 | 62 | def __init__(
|
16 | 63 | self,
|
| 64 | + style_deserializers_factory: StyleDeserializersFactory, |
| 65 | + media_types_deserializer: MediaTypesDeserializer, |
17 | 66 | mimetype: str,
|
18 |
| - deserializer_callable: Optional[DeserializerCallable] = None, |
| 67 | + schema: Optional[SchemaPath] = None, |
| 68 | + encoding: Optional[SchemaPath] = None, |
19 | 69 | **parameters: str,
|
20 | 70 | ):
|
| 71 | + self.style_deserializers_factory = style_deserializers_factory |
| 72 | + self.media_types_deserializer = media_types_deserializer |
21 | 73 | self.mimetype = mimetype
|
22 |
| - self.deserializer_callable = deserializer_callable |
| 74 | + self.schema = schema |
| 75 | + self.encoding = encoding |
23 | 76 | self.parameters = parameters
|
24 | 77 |
|
25 | 78 | def deserialize(self, value: Any) -> Any:
|
26 |
| - if self.deserializer_callable is None: |
27 |
| - warnings.warn(f"Unsupported {self.mimetype} mimetype") |
28 |
| - return value |
| 79 | + deserialized = self.media_types_deserializer.deserialize( |
| 80 | + self.mimetype, value, **self.parameters |
| 81 | + ) |
29 | 82 |
|
30 |
| - try: |
31 |
| - return self.deserializer_callable(value, **self.parameters) |
32 |
| - except (ParseError, ValueError, TypeError, AttributeError): |
33 |
| - raise MediaTypeDeserializeError(self.mimetype, value) |
| 83 | + if ( |
| 84 | + self.mimetype != "application/x-www-form-urlencoded" |
| 85 | + and not self.mimetype.startswith("multipart") |
| 86 | + ): |
| 87 | + return deserialized |
| 88 | + |
| 89 | + # decode multipart request bodies |
| 90 | + return self.decode(deserialized) |
| 91 | + |
| 92 | + def evolve( |
| 93 | + self, mimetype: str, schema: Optional[SchemaPath] |
| 94 | + ) -> "MediaTypeDeserializer": |
| 95 | + cls = self.__class__ |
| 96 | + |
| 97 | + return cls( |
| 98 | + self.style_deserializers_factory, |
| 99 | + self.media_types_deserializer, |
| 100 | + mimetype, |
| 101 | + schema=schema, |
| 102 | + ) |
| 103 | + |
| 104 | + def decode(self, location: Mapping[str, Any]) -> Mapping[str, Any]: |
| 105 | + # schema is required for multipart |
| 106 | + assert self.schema is not None |
| 107 | + schema_props = self.schema.get("properties") |
| 108 | + properties = {} |
| 109 | + for prop_name, prop_schema in get_properties(self.schema).items(): |
| 110 | + try: |
| 111 | + properties[prop_name] = self.decode_property( |
| 112 | + prop_name, prop_schema, location |
| 113 | + ) |
| 114 | + except KeyError: |
| 115 | + if "default" not in prop_schema: |
| 116 | + continue |
| 117 | + properties[prop_name] = prop_schema["default"] |
| 118 | + |
| 119 | + return properties |
| 120 | + |
| 121 | + def decode_property( |
| 122 | + self, |
| 123 | + prop_name: str, |
| 124 | + prop_schema: SchemaPath, |
| 125 | + location: Mapping[str, Any], |
| 126 | + ) -> Any: |
| 127 | + if self.encoding is None or prop_name not in self.encoding: |
| 128 | + return self.decode_property_content_type( |
| 129 | + prop_name, prop_schema, location |
| 130 | + ) |
| 131 | + |
| 132 | + prep_encoding = self.encoding / prop_name |
| 133 | + if ( |
| 134 | + "style" not in prep_encoding |
| 135 | + and "explode" not in prep_encoding |
| 136 | + and "allowReserved" not in prep_encoding |
| 137 | + ): |
| 138 | + return self.decode_property_content_type( |
| 139 | + prop_name, prop_schema, location, prep_encoding |
| 140 | + ) |
| 141 | + |
| 142 | + return self.decode_property_style( |
| 143 | + prop_name, prop_schema, location, prep_encoding |
| 144 | + ) |
| 145 | + |
| 146 | + def decode_property_style( |
| 147 | + self, |
| 148 | + prop_name: str, |
| 149 | + prop_schema: SchemaPath, |
| 150 | + location: Mapping[str, Any], |
| 151 | + prep_encoding: SchemaPath, |
| 152 | + ) -> Any: |
| 153 | + prop_style, prop_explode = get_style_and_explode( |
| 154 | + prep_encoding, default_location="query" |
| 155 | + ) |
| 156 | + prop_deserializer = self.style_deserializers_factory.create( |
| 157 | + prop_style, prop_explode, prop_schema, name=prop_name |
| 158 | + ) |
| 159 | + return prop_deserializer.deserialize(location) |
| 160 | + |
| 161 | + def decode_property_content_type( |
| 162 | + self, |
| 163 | + prop_name: str, |
| 164 | + prop_schema: SchemaPath, |
| 165 | + location: Mapping[str, Any], |
| 166 | + prep_encoding: Optional[SchemaPath] = None, |
| 167 | + ) -> Any: |
| 168 | + prop_content_type = get_content_type(prop_schema, prep_encoding) |
| 169 | + prop_deserializer = self.evolve( |
| 170 | + prop_content_type, |
| 171 | + prop_schema, |
| 172 | + ) |
| 173 | + prop_schema_type = prop_schema.getkey("type", "") |
| 174 | + if prop_schema_type == "array": |
| 175 | + if isinstance(location, SuportsGetAll): |
| 176 | + value = location.getall(prop_name) |
| 177 | + if isinstance(location, SuportsGetList): |
| 178 | + value = location.getlist(prop_name) |
| 179 | + return list(map(prop_deserializer.deserialize, value)) |
| 180 | + else: |
| 181 | + value = location[prop_name] |
| 182 | + return prop_deserializer.deserialize(value) |
0 commit comments