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
|
from functools import partial
import pytest
from jsonschema_path import SchemaPath
from openapi_schema_validator import OAS30WriteValidator
from openapi_core.unmarshalling.schemas import oas30_types_unmarshaller
from openapi_core.unmarshalling.schemas.exceptions import (
FormatterNotFoundError,
)
from openapi_core.unmarshalling.schemas.factories import (
SchemaUnmarshallersFactory,
)
from openapi_core.validation.schemas import (
oas30_write_schema_validators_factory,
)
from openapi_core.validation.schemas.exceptions import InvalidSchemaValue
from openapi_core.validation.schemas.factories import SchemaValidatorsFactory
@pytest.fixture
def schema_unmarshaller_factory():
def create_unmarshaller(
validators_factory,
schema,
format_validators=None,
extra_format_validators=None,
extra_format_unmarshallers=None,
):
return SchemaUnmarshallersFactory(
validators_factory,
oas30_types_unmarshaller,
).create(
schema,
format_validators=format_validators,
extra_format_validators=extra_format_validators,
extra_format_unmarshallers=extra_format_unmarshallers,
)
return create_unmarshaller
@pytest.fixture
def unmarshaller_factory(schema_unmarshaller_factory):
return partial(
schema_unmarshaller_factory,
oas30_write_schema_validators_factory,
)
class TestOAS30SchemaUnmarshallerFactoryCreate:
def test_string_format_unknown(self, unmarshaller_factory):
unknown_format = "unknown"
schema = {
"type": "string",
"format": unknown_format,
}
spec = SchemaPath.from_dict(schema)
with pytest.raises(FormatterNotFoundError):
unmarshaller_factory(spec)
def test_string_format_invalid_value(self, unmarshaller_factory):
custom_format = "custom"
schema = {
"type": "string",
"format": custom_format,
}
spec = SchemaPath.from_dict(schema)
with pytest.raises(
FormatterNotFoundError,
match="Formatter not found for custom format",
):
unmarshaller_factory(spec)
class TestOAS30SchemaUnmarshallerUnmarshal:
def test_schema_extra_format_unmarshaller_format_invalid(
self, schema_unmarshaller_factory, unmarshaller_factory
):
def custom_format_unmarshaller(value):
raise ValueError
custom_format = "custom"
schema = {
"type": "string",
"format": "custom",
}
spec = SchemaPath.from_dict(schema)
value = "x"
schema_validators_factory = SchemaValidatorsFactory(
OAS30WriteValidator
)
extra_format_unmarshallers = {
custom_format: custom_format_unmarshaller,
}
unmarshaller = schema_unmarshaller_factory(
schema_validators_factory,
spec,
extra_format_unmarshallers=extra_format_unmarshallers,
)
result = unmarshaller.unmarshal(value)
assert result == value
def test_schema_extra_format_unmarshaller_format_custom(
self, schema_unmarshaller_factory
):
formatted = "x-custom"
def custom_format_unmarshaller(value):
return formatted
custom_format = "custom"
schema = {
"type": "string",
"format": custom_format,
}
spec = SchemaPath.from_dict(schema)
value = "x"
schema_validators_factory = SchemaValidatorsFactory(
OAS30WriteValidator
)
extra_format_unmarshallers = {
custom_format: custom_format_unmarshaller,
}
unmarshaller = schema_unmarshaller_factory(
schema_validators_factory,
spec,
extra_format_unmarshallers=extra_format_unmarshallers,
)
result = unmarshaller.unmarshal(value)
assert result == formatted
def test_schema_extra_format_validator_format_invalid(
self, schema_unmarshaller_factory, unmarshaller_factory
):
def custom_format_validator(value):
return False
custom_format = "custom"
schema = {
"type": "string",
"format": custom_format,
}
spec = SchemaPath.from_dict(schema)
value = "x"
schema_validators_factory = SchemaValidatorsFactory(
OAS30WriteValidator
)
extra_format_validators = {
custom_format: custom_format_validator,
}
unmarshaller = schema_unmarshaller_factory(
schema_validators_factory,
spec,
extra_format_validators=extra_format_validators,
)
with pytest.raises(InvalidSchemaValue):
unmarshaller.unmarshal(value)
def test_schema_extra_format_validator_format_custom(
self, schema_unmarshaller_factory
):
def custom_format_validator(value):
return True
custom_format = "custom"
schema = {
"type": "string",
"format": custom_format,
}
spec = SchemaPath.from_dict(schema)
value = "x"
schema_validators_factory = SchemaValidatorsFactory(
OAS30WriteValidator
)
extra_format_validators = {
custom_format: custom_format_validator,
}
unmarshaller = schema_unmarshaller_factory(
schema_validators_factory,
spec,
extra_format_validators=extra_format_validators,
)
result = unmarshaller.unmarshal(value)
assert result == value
@pytest.mark.xfail(
reason=(
"Not registered format raises FormatterNotFoundError"
"See https://github.com/python-openapi/openapi-core/issues/515"
),
strict=True,
)
def test_schema_format_validator_format_invalid(
self, schema_unmarshaller_factory, unmarshaller_factory
):
custom_format = "date"
schema = {
"type": "string",
"format": custom_format,
}
spec = SchemaPath.from_dict(schema)
value = "x"
schema_validators_factory = SchemaValidatorsFactory(
OAS30WriteValidator
)
format_validators = {}
unmarshaller = schema_unmarshaller_factory(
schema_validators_factory,
spec,
format_validators=format_validators,
)
result = unmarshaller.unmarshal(value)
assert result == value
def test_schema_format_validator_format_custom(
self, schema_unmarshaller_factory, unmarshaller_factory
):
def custom_format_validator(value):
return True
custom_format = "date"
schema = {
"type": "string",
"format": custom_format,
}
spec = SchemaPath.from_dict(schema)
value = "x"
schema_validators_factory = SchemaValidatorsFactory(
OAS30WriteValidator
)
format_validators = {
custom_format: custom_format_validator,
}
unmarshaller = schema_unmarshaller_factory(
schema_validators_factory,
spec,
format_validators=format_validators,
)
result = unmarshaller.unmarshal(value)
assert result == value
|