|
1 | 1 | from typing import TYPE_CHECKING
|
2 | 2 | from typing import Any
|
3 | 3 | from typing import Callable
|
| 4 | +from typing import Iterable |
4 | 5 | from typing import List
|
| 6 | +from typing import Mapping |
| 7 | +from typing import Optional |
| 8 | +from typing import Type |
| 9 | +from typing import Union |
5 | 10 |
|
6 | 11 | from jsonschema_path import SchemaPath
|
7 | 12 |
|
8 | 13 | from openapi_core.casting.schemas.datatypes import CasterCallable
|
9 | 14 | from openapi_core.casting.schemas.exceptions import CastError
|
| 15 | +from openapi_core.schema.schemas import get_properties |
| 16 | +from openapi_core.validation.schemas.validators import SchemaValidator |
10 | 17 |
|
11 | 18 | if TYPE_CHECKING:
|
12 | 19 | from openapi_core.casting.schemas.factories import SchemaCastersFactory
|
13 | 20 |
|
14 | 21 |
|
15 |
| -class BaseSchemaCaster: |
16 |
| - def __init__(self, schema: SchemaPath): |
| 22 | +class PrimitiveCaster: |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + schema: SchemaPath, |
| 26 | + schema_validator: SchemaValidator, |
| 27 | + schema_caster: "SchemaCaster", |
| 28 | + ): |
17 | 29 | self.schema = schema
|
| 30 | + self.schema_validator = schema_validator |
| 31 | + self.schema_caster = schema_caster |
18 | 32 |
|
19 | 33 | def __call__(self, value: Any) -> Any:
|
20 | 34 | if value is None:
|
21 | 35 | return value
|
22 | 36 |
|
23 | 37 | return self.cast(value)
|
24 | 38 |
|
| 39 | + def evolve(self, schema: SchemaPath) -> "SchemaCaster": |
| 40 | + cls = self.__class__ |
| 41 | + |
| 42 | + return cls( |
| 43 | + schema, |
| 44 | + self.schema_validator.evolve(schema), |
| 45 | + self.schema_caster.evolve(schema), |
| 46 | + ) |
| 47 | + |
25 | 48 | def cast(self, value: Any) -> Any:
|
26 | 49 | raise NotImplementedError
|
27 | 50 |
|
28 | 51 |
|
29 |
| -class CallableSchemaCaster(BaseSchemaCaster): |
30 |
| - def __init__(self, schema: SchemaPath, caster_callable: CasterCallable): |
31 |
| - super().__init__(schema) |
32 |
| - self.caster_callable = caster_callable |
| 52 | +class DummyCaster(PrimitiveCaster): |
| 53 | + def cast(self, value: Any) -> Any: |
| 54 | + return value |
| 55 | + |
| 56 | + |
| 57 | +class PrimitiveTypeCaster(PrimitiveCaster): |
| 58 | + primitive_type: CasterCallable = NotImplemented |
33 | 59 |
|
34 | 60 | def cast(self, value: Any) -> Any:
|
| 61 | + if isinstance(value, self.primitive_type): |
| 62 | + return value |
| 63 | + |
| 64 | + if not isinstance(value, (str, bytes)): |
| 65 | + raise CastError(value, self.schema["type"]) |
| 66 | + |
35 | 67 | try:
|
36 |
| - return self.caster_callable(value) |
| 68 | + return self.primitive_type(value) |
37 | 69 | except (ValueError, TypeError):
|
38 | 70 | raise CastError(value, self.schema["type"])
|
39 | 71 |
|
40 | 72 |
|
41 |
| -class DummyCaster(BaseSchemaCaster): |
42 |
| - def cast(self, value: Any) -> Any: |
43 |
| - return value |
| 73 | +class IntegerCaster(PrimitiveTypeCaster): |
| 74 | + primitive_type = int |
44 | 75 |
|
45 | 76 |
|
46 |
| -class ComplexCaster(BaseSchemaCaster): |
47 |
| - def __init__( |
48 |
| - self, schema: SchemaPath, casters_factory: "SchemaCastersFactory" |
49 |
| - ): |
50 |
| - super().__init__(schema) |
51 |
| - self.casters_factory = casters_factory |
| 77 | +class NumberCaster(PrimitiveTypeCaster): |
| 78 | + primitive_type = float |
| 79 | + |
| 80 | + |
| 81 | +class BooleanCaster(PrimitiveTypeCaster): |
| 82 | + primitive_type = bool |
52 | 83 |
|
53 | 84 |
|
54 |
| -class ArrayCaster(ComplexCaster): |
| 85 | +class ArrayCaster(PrimitiveCaster): |
55 | 86 | @property
|
56 |
| - def items_caster(self) -> BaseSchemaCaster: |
57 |
| - return self.casters_factory.create(self.schema / "items") |
| 87 | + def items_caster(self) -> "SchemaUnmarshaller": |
| 88 | + # sometimes we don't have any schema i.e. free-form objects |
| 89 | + items_schema = self.schema.get("items", SchemaPath.from_dict({})) |
| 90 | + return self.schema_caster.evolve(items_schema) |
58 | 91 |
|
59 | 92 | def cast(self, value: Any) -> List[Any]:
|
60 | 93 | # str and bytes are not arrays according to the OpenAPI spec
|
61 |
| - if isinstance(value, (str, bytes)): |
| 94 | + if isinstance(value, (str, bytes)) or not isinstance(value, Iterable): |
62 | 95 | raise CastError(value, self.schema["type"])
|
63 | 96 |
|
64 | 97 | try:
|
65 |
| - return list(map(self.items_caster, value)) |
| 98 | + return list(map(self.items_caster.cast, value)) |
66 | 99 | except (ValueError, TypeError):
|
67 | 100 | raise CastError(value, self.schema["type"])
|
| 101 | + |
| 102 | + |
| 103 | +class ObjectCaster(PrimitiveCaster): |
| 104 | + def cast(self, value: Any, schema_only: bool = False) -> Any: |
| 105 | + if not isinstance(value, dict): |
| 106 | + raise CastError(value, self.schema["type"]) |
| 107 | + |
| 108 | + one_of_schema = self.schema_validator.get_one_of_schema(value) |
| 109 | + if one_of_schema is not None: |
| 110 | + one_of_properties = self.evolve(one_of_schema).cast( |
| 111 | + value, schema_only=True |
| 112 | + ) |
| 113 | + value.update(one_of_properties) |
| 114 | + |
| 115 | + any_of_schemas = self.schema_validator.iter_any_of_schemas(value) |
| 116 | + for any_of_schema in any_of_schemas: |
| 117 | + any_of_properties = self.evolve(any_of_schema).cast( |
| 118 | + value, schema_only=True |
| 119 | + ) |
| 120 | + value.update(any_of_properties) |
| 121 | + |
| 122 | + all_of_schemas = self.schema_validator.iter_all_of_schemas(value) |
| 123 | + for all_of_schema in all_of_schemas: |
| 124 | + all_of_properties = self.evolve(all_of_schema).cast( |
| 125 | + value, schema_only=True |
| 126 | + ) |
| 127 | + value.update(all_of_properties) |
| 128 | + |
| 129 | + for prop_name, prop_schema in get_properties(self.schema).items(): |
| 130 | + try: |
| 131 | + prop_value = value[prop_name] |
| 132 | + except KeyError: |
| 133 | + if "default" not in prop_schema: |
| 134 | + continue |
| 135 | + prop_value = prop_schema["default"] |
| 136 | + |
| 137 | + value[prop_name] = self.schema_caster.evolve(prop_schema).cast( |
| 138 | + prop_value |
| 139 | + ) |
| 140 | + |
| 141 | + if schema_only: |
| 142 | + return value |
| 143 | + |
| 144 | + additional_properties = self.schema.getkey( |
| 145 | + "additionalProperties", True |
| 146 | + ) |
| 147 | + if additional_properties is not False: |
| 148 | + # free-form object |
| 149 | + if additional_properties is True: |
| 150 | + additional_prop_schema = SchemaPath.from_dict( |
| 151 | + {"nullable": True} |
| 152 | + ) |
| 153 | + # defined schema |
| 154 | + else: |
| 155 | + additional_prop_schema = self.schema / "additionalProperties" |
| 156 | + additional_prop_caster = self.schema_caster.evolve( |
| 157 | + additional_prop_schema |
| 158 | + ) |
| 159 | + for prop_name, prop_value in value.items(): |
| 160 | + if prop_name in value: |
| 161 | + continue |
| 162 | + value[prop_name] = additional_prop_caster.cast(prop_value) |
| 163 | + |
| 164 | + return value |
| 165 | + |
| 166 | + |
| 167 | +class TypesCaster: |
| 168 | + casters: Mapping[str, Type[PrimitiveCaster]] = {} |
| 169 | + multi: Optional[Type[PrimitiveCaster]] = None |
| 170 | + |
| 171 | + def __init__( |
| 172 | + self, |
| 173 | + casters: Mapping[str, Type[PrimitiveCaster]], |
| 174 | + default: Type[PrimitiveCaster], |
| 175 | + multi: Optional[Type[PrimitiveCaster]] = None, |
| 176 | + ): |
| 177 | + self.casters = casters |
| 178 | + self.default = default |
| 179 | + self.multi = multi |
| 180 | + |
| 181 | + def get_types(self) -> List[str]: |
| 182 | + return list(self.casters.keys()) |
| 183 | + |
| 184 | + def get_caster( |
| 185 | + self, |
| 186 | + schema_type: Optional[Union[Iterable[str], str]], |
| 187 | + ) -> Type["PrimitiveCaster"]: |
| 188 | + if schema_type is None: |
| 189 | + return self.default |
| 190 | + if isinstance(schema_type, Iterable) and not isinstance( |
| 191 | + schema_type, str |
| 192 | + ): |
| 193 | + if self.multi is None: |
| 194 | + raise TypeError("caster does not accept multiple types") |
| 195 | + return self.multi |
| 196 | + |
| 197 | + return self.casters[schema_type] |
| 198 | + |
| 199 | + |
| 200 | +class SchemaCaster: |
| 201 | + def __init__( |
| 202 | + self, |
| 203 | + schema: SchemaPath, |
| 204 | + schema_validator: SchemaValidator, |
| 205 | + types_caster: TypesCaster, |
| 206 | + ): |
| 207 | + self.schema = schema |
| 208 | + self.schema_validator = schema_validator |
| 209 | + |
| 210 | + self.types_caster = types_caster |
| 211 | + |
| 212 | + def cast(self, value: Any) -> Any: |
| 213 | + # skip casting for nullable in OpenAPI 3.0 |
| 214 | + if value is None and self.schema.getkey("nullable", False): |
| 215 | + return value |
| 216 | + |
| 217 | + schema_type = self.schema.getkey("type") |
| 218 | + type_caster = self.get_type_caster(schema_type) |
| 219 | + return type_caster(value) |
| 220 | + |
| 221 | + def get_type_caster( |
| 222 | + self, |
| 223 | + schema_type: Optional[Union[Iterable[str], str]], |
| 224 | + ) -> PrimitiveCaster: |
| 225 | + caster_cls = self.types_caster.get_caster(schema_type) |
| 226 | + return caster_cls( |
| 227 | + self.schema, |
| 228 | + self.schema_validator, |
| 229 | + self, |
| 230 | + ) |
| 231 | + |
| 232 | + def evolve(self, schema: SchemaPath) -> "SchemaCaster": |
| 233 | + cls = self.__class__ |
| 234 | + |
| 235 | + return cls( |
| 236 | + schema, |
| 237 | + self.schema_validator.evolve(schema), |
| 238 | + self.types_caster, |
| 239 | + ) |
0 commit comments