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
|
from __future__ import annotations
import datetime
import json
from decimal import Decimal
from functools import partial
from pathlib import Path
from typing import Any
import pydantic as pydantic_v2
import pytest
from pydantic import v1 as pydantic_v1
from pydantic.v1.color import Color as ColorV1
from pydantic_extra_types.color import Color as ColorV2
from litestar.exceptions import SerializationException
from litestar.plugins.pydantic import PydanticInitPlugin, _model_dump, _model_dump_json
from litestar.serialization import (
decode_json,
decode_msgpack,
default_serializer,
encode_json,
encode_msgpack,
get_serializer,
)
from . import PydanticVersion
TODAY = datetime.date.today()
YESTERDAY_DATE = TODAY - datetime.timedelta(days=1)
FUTURE_DATE = TODAY + datetime.timedelta(days=1)
YESTERDAY_DATETIME = datetime.datetime(2023, 6, 14, 12, 0, 0)
FUTURE_DATETIME = datetime.datetime(2030, 6, 16, 12, 0, 0)
AWARE_DATETIME = datetime.datetime(2023, 6, 15, 12, 0, 0, tzinfo=datetime.timezone.utc)
NAIVE_DATETIME = datetime.datetime(2023, 6, 15, 12, 0, 0).replace(tzinfo=None)
class CustomStr(str):
pass
class CustomInt(int):
pass
class CustomFloat(float):
pass
class CustomList(list):
pass
class CustomSet(set):
pass
class CustomFrozenSet(frozenset):
pass
class CustomTuple(tuple):
pass
class ModelV1(pydantic_v1.BaseModel):
class Config:
arbitrary_types_allowed = True
custom_str: CustomStr = CustomStr()
custom_int: CustomInt = CustomInt()
custom_float: CustomFloat = CustomFloat()
custom_list: CustomList = CustomList()
custom_set: CustomSet = CustomSet()
custom_frozenset: CustomFrozenSet = CustomFrozenSet()
custom_tuple: CustomTuple = CustomTuple()
conset: pydantic_v1.conset(int, min_items=1) # type: ignore[valid-type]
confrozenset: pydantic_v1.confrozenset(int, min_items=1) # type: ignore[valid-type]
conlist: pydantic_v1.conlist(int, min_items=1) # type: ignore[valid-type]
path: Path
email_str: pydantic_v1.EmailStr
name_email: pydantic_v1.NameEmail
color: ColorV1
bytesize: pydantic_v1.ByteSize
secret_str: pydantic_v1.SecretStr
secret_bytes: pydantic_v1.SecretBytes
payment_card_number: pydantic_v1.PaymentCardNumber
constr: pydantic_v1.constr(min_length=1) # type: ignore[valid-type]
conbytes: pydantic_v1.conbytes(min_length=1) # type: ignore[valid-type]
condate: pydantic_v1.condate(ge=TODAY) # type: ignore[valid-type]
condecimal: pydantic_v1.condecimal(ge=Decimal("1")) # type: ignore[valid-type]
confloat: pydantic_v1.confloat(ge=0) # type: ignore[valid-type]
conint: pydantic_v1.conint(ge=0) # type: ignore[valid-type]
url: pydantic_v1.AnyUrl
http_url: pydantic_v1.HttpUrl
class ModelV2(pydantic_v2.BaseModel):
model_config = {"arbitrary_types_allowed": True}
conset: pydantic_v2.conset(int, min_length=1) # type: ignore[valid-type]
confrozenset: pydantic_v2.confrozenset(int, min_length=1) # type: ignore[valid-type]
conlist: pydantic_v2.conlist(int, min_length=1) # type: ignore[valid-type]
path: Path
email_str: pydantic_v2.EmailStr
name_email: pydantic_v2.NameEmail
color: ColorV2
bytesize: pydantic_v2.ByteSize
secret_str: pydantic_v2.SecretStr
secret_bytes: pydantic_v2.SecretBytes
payment_card_number: pydantic_v2.PaymentCardNumber
constr: pydantic_v2.constr(min_length=1) # type: ignore[valid-type]
conbytes: pydantic_v2.conbytes(min_length=1) # type: ignore[valid-type]
condate: pydantic_v2.condate(ge=TODAY) # type: ignore[valid-type]
condecimal: pydantic_v2.condecimal(ge=Decimal("1")) # type: ignore[valid-type]
confloat: pydantic_v2.confloat(ge=0) # type: ignore[valid-type]
conint: pydantic_v2.conint(ge=0) # type: ignore[valid-type]
url: pydantic_v2.AnyUrl
http_url: pydantic_v2.HttpUrl
paste_date: pydantic_v2.PastDate
future_date: pydantic_v2.FutureDate
paste_datetime: pydantic_v2.PastDatetime
future_datetime: pydantic_v2.FutureDatetime
aware_datetime: pydantic_v2.AwareDatetime
naive_datetime: pydantic_v2.NaiveDatetime
serializer = partial(default_serializer, type_encoders=PydanticInitPlugin.encoders())
@pytest.fixture()
def model_type(pydantic_version: PydanticVersion) -> type[ModelV1 | ModelV2]:
return ModelV1 if pydantic_version == "v1" else ModelV2
@pytest.fixture()
def model(pydantic_version: PydanticVersion) -> ModelV1 | ModelV2:
if pydantic_version == "v1":
return ModelV1(
path=Path("example"),
email_str=pydantic_v1.parse_obj_as(pydantic_v1.EmailStr, "info@example.org"),
name_email=pydantic_v1.NameEmail("info", "info@example.org"),
color=ColorV1("rgb(255, 255, 255)"),
bytesize=pydantic_v1.ByteSize(100),
secret_str=pydantic_v1.SecretStr("hello"),
secret_bytes=pydantic_v1.SecretBytes(b"hello"),
payment_card_number=pydantic_v1.PaymentCardNumber("4000000000000002"),
constr="hello",
conbytes=b"hello",
condate=TODAY,
condecimal=Decimal("3.14"),
confloat=1.0,
conset={1},
confrozenset=frozenset([1]),
conint=1,
conlist=[1],
url="some://example.org/", # type: ignore[arg-type]
http_url="http://example.org/", # type: ignore[arg-type]
)
return ModelV2(
path=Path("example"),
email_str=pydantic_v2.parse_obj_as(pydantic_v2.EmailStr, "info@example.org"), # pyright: ignore[reportArgumentType]
name_email=pydantic_v2.NameEmail("info", "info@example.org"),
color=ColorV2("rgb(255, 255, 255)"),
bytesize=pydantic_v2.ByteSize(100),
secret_str=pydantic_v2.SecretStr("hello"),
secret_bytes=pydantic_v2.SecretBytes(b"hello"),
payment_card_number=pydantic_v2.PaymentCardNumber("4000000000000002"),
constr="hello",
conbytes=b"hello",
condate=TODAY,
condecimal=Decimal("3.14"),
confloat=1.0,
conset={1},
confrozenset=frozenset([1]),
conint=1,
conlist=[1],
url="some://example.org/", # type: ignore[arg-type]
http_url="http://example.org/", # type: ignore[arg-type]
paste_date=YESTERDAY_DATE,
future_date=FUTURE_DATE,
paste_datetime=YESTERDAY_DATETIME,
future_datetime=FUTURE_DATETIME,
aware_datetime=AWARE_DATETIME,
naive_datetime=NAIVE_DATETIME,
)
@pytest.mark.parametrize(
"attribute_name, expected",
[
("path", "example"),
("email_str", "info@example.org"),
("name_email", "info <info@example.org>"),
("color", "white"),
("bytesize", 100),
("secret_str", "**********"),
("secret_bytes", "**********"),
("payment_card_number", "4000000000000002"),
("constr", "hello"),
("conbytes", b"hello"),
("condate", TODAY.isoformat()),
("condecimal", 3.14),
("conset", {1}),
("confrozenset", frozenset([1])),
("conint", 1),
("url", "some://example.org/"),
("http_url", "http://example.org/"),
("paste_date", YESTERDAY_DATE.isoformat()),
("future_date", FUTURE_DATE.isoformat()),
("paste_datetime", YESTERDAY_DATETIME.isoformat()),
("future_datetime", FUTURE_DATETIME.isoformat()),
("aware_datetime", AWARE_DATETIME.isoformat()),
("naive_datetime", NAIVE_DATETIME.isoformat()),
],
)
def test_default_serializer(model: ModelV1 | ModelV2, attribute_name: str, expected: Any) -> None:
# Skip Pydantic v2-specific date fields when testing with ModelV1
v2_only_fields = {
"paste_date",
"future_date",
"paste_datetime",
"future_datetime",
"aware_datetime",
"naive_datetime",
}
if isinstance(model, ModelV1) and attribute_name in v2_only_fields:
pytest.skip(f"Field '{attribute_name}' only exists in Pydantic v2")
assert serializer(getattr(model, attribute_name)) == expected
def test_serialization_of_model_instance(model: ModelV1 | ModelV2) -> None:
assert serializer(getattr(model, "conbytes")) == b"hello"
assert serializer(model) == _model_dump(model)
@pytest.mark.parametrize("prefer_alias", [False, True])
def test_pydantic_json_compatibility(
model: ModelV1 | ModelV2, prefer_alias: bool, pydantic_version: PydanticVersion
) -> None:
raw = _model_dump_json(model, by_alias=prefer_alias)
encoded_json = encode_json(model, serializer=get_serializer(PydanticInitPlugin.encoders(prefer_alias=prefer_alias)))
raw_result = json.loads(raw)
encoded_result = json.loads(encoded_json)
if pydantic_version == "v1":
# pydantic v1 dumps decimals into floats as json, we therefore regard this as an error
assert raw_result.get("condecimal") == float(encoded_result.get("condecimal"))
del raw_result["condecimal"]
del encoded_result["condecimal"]
assert raw_result == encoded_result
@pytest.mark.parametrize("encoder", [encode_json, encode_msgpack])
def test_encoder_raises_serialization_exception(model: ModelV1 | ModelV2, encoder: Any) -> None:
with pytest.raises(SerializationException):
encoder(object())
@pytest.mark.parametrize("decoder", [decode_json, decode_msgpack])
def test_decode_json_raises_serialization_exception(model: ModelV1 | ModelV2, decoder: Any) -> None:
with pytest.raises(SerializationException):
decoder(b"str")
@pytest.mark.parametrize("prefer_alias", [False, True])
def test_decode_json_typed(model: ModelV1 | ModelV2, prefer_alias: bool, model_type: type[ModelV1 | ModelV2]) -> None:
dumped_model = _model_dump_json(model, by_alias=prefer_alias)
decoded_model = decode_json(value=dumped_model, target_type=model_type, type_decoders=PydanticInitPlugin.decoders())
assert _model_dump_json(decoded_model, by_alias=prefer_alias) == dumped_model # type: ignore[arg-type]
@pytest.mark.parametrize("prefer_alias", [False, True])
def test_decode_msgpack_typed(
model: ModelV1 | ModelV2, model_type: type[ModelV1 | ModelV2], prefer_alias: bool
) -> None:
model_json = _model_dump_json(model, by_alias=prefer_alias)
assert (
decode_msgpack(
encode_msgpack(model, serializer=get_serializer(PydanticInitPlugin.encoders(prefer_alias=prefer_alias))),
model_type,
type_decoders=PydanticInitPlugin.decoders(),
).json() # type: ignore[attr-defined]
== model_json
)
|