Repository navigation
Expand file tree
/
Copy pathconverter.py
More file actions
118 lines (95 loc) · 5.57 KB
/
Copy pathconverter.py
File metadata and controls
118 lines (95 loc) · 5.57 KB
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""Pydantic-backed :class:`DataConverter` for the Durable Task Python SDK.
This is the heart of the example: a small, self-contained converter that plugs
a third-party serialization/validation library (`pydantic
<https://docs.pydantic.dev/>`_) into the SDK's pluggable serialization seam.
How the seam works
------------------
Both the worker and the client accept a ``data_converter`` argument. The SDK
routes **every** payload boundary -- orchestrator/activity/entity inputs and
outputs, external events, and custom status -- through that single converter,
so one object controls how Python values become JSON on the wire and how they
are reconstructed on the way back.
A converter implements three methods:
* ``serialize(value)`` -- Python value -> JSON string (or ``None``).
* ``deserialize(data, t)`` -- JSON string -> Python value, optionally
reconstructed as ``t`` (the type the SDK learned from a function's
annotation, a ``return_type=`` argument, or a typed client accessor).
* ``coerce(value, t)`` -- already-parsed value -> reconstructed as ``t``
(used where the SDK already holds a parsed value, e.g. entity state).
It may also override one hook:
* ``can_reconstruct(t)`` -- tells the SDK's inbound type-discovery that an
*input* annotated with type ``t`` should be handed to ``deserialize`` /
``coerce`` (rather than passed through as raw JSON). The default recognizes
dataclasses and ``from_json()``-capable types; override it to add your own
(here, pydantic models).
This converter recognizes :class:`pydantic.BaseModel` subclasses and uses
pydantic for them (gaining validation, aliasing, custom field types, etc.).
For everything else -- ``str``, ``int``, ``list``, dataclasses, ... -- it
delegates to the SDK's default :class:`JsonDataConverter`, so plugging it in
costs nothing for non-pydantic payloads. This "handle my types, delegate the
rest" shape is the recommended pattern for a real custom converter.
"""
from __future__ import annotations
from typing import Any
from pydantic import BaseModel
from durabletask.serialization import DataConverter, JsonDataConverter
def _is_model_type(target_type: Any) -> bool:
"""Return True when ``target_type`` is a pydantic model class."""
return isinstance(target_type, type) and issubclass(target_type, BaseModel)
class PydanticDataConverter(DataConverter):
"""A :class:`DataConverter` that serializes pydantic models with pydantic.
Pydantic models are serialized via ``model_dump_json()`` and reconstructed
(with full validation) via ``model_validate_json()`` / ``model_validate()``
whenever the SDK supplies the model type. Every other value falls through to
the default :class:`JsonDataConverter`.
> [!NOTE]
> For simplicity this converter only intercepts when a pydantic model is the
> *top-level* ``target_type``. A model nested **inside another model** (like
> ``Order.items: list[OrderItem]`` below) round-trips fine because pydantic
> handles that recursion itself. But a model nested in a *top-level generic*
> the SDK is asked to rebuild directly -- e.g. ``return_type=list[OrderItem]``
> or an input annotated ``dict[str, OrderItem]`` -- is **not** intercepted
> here; it falls to the default codec, which cannot construct a pydantic
> model from a positional ``dict`` and so leaves the elements as raw dicts. A
> production converter would recurse into such generics (for example via
> ``pydantic.TypeAdapter``); this example keeps the surface minimal.
"""
def __init__(self) -> None:
# Delegate non-pydantic payloads to the SDK's built-in JSON codec.
self._fallback = JsonDataConverter()
def serialize(self, value: Any) -> str | None:
if value is None:
return None
if isinstance(value, BaseModel):
# ``model_dump_json`` honors pydantic field serializers, aliases,
# and custom types (e.g. ``datetime`` -> ISO 8601) automatically.
return value.model_dump_json()
return self._fallback.serialize(value)
def deserialize(self, data: str | None, target_type: type | None = None) -> Any:
if data is None or data == "":
return None
if _is_model_type(target_type):
# ``model_validate_json`` parses *and validates* the payload,
# raising ``pydantic.ValidationError`` on malformed data.
return target_type.model_validate_json(data) # type: ignore[union-attr]
return self._fallback.deserialize(data, target_type)
def coerce(self, value: Any, target_type: type | None = None) -> Any:
if value is None:
return None
if _is_model_type(target_type):
return target_type.model_validate(value) # type: ignore[union-attr]
return self._fallback.coerce(value, target_type)
def can_reconstruct(self, target_type: Any) -> bool:
# Teach the SDK's inbound type-discovery that pydantic models are
# reconstructable, so an orchestrator/activity input annotated with a
# model type is rebuilt (and validated) by this converter instead of
# arriving as a plain ``dict``. For everything else, defer to the same
# ``JsonDataConverter`` fallback this converter uses for serialization,
# so its reconstruction claims match what it actually handles
# (dataclasses, ``from_json`` types, ``Optional`` / ``list`` wrappers;
# builtins excluded).
if _is_model_type(target_type):
return True
return self._fallback.can_reconstruct(target_type)