1313Pydantic v1 is not supported.
1414"""
1515
16+ from dataclasses import dataclass
1617from typing import Any , Optional , Type
1718
1819from pydantic import TypeAdapter
19- from pydantic_core import to_json , SchemaSerializer
20+ from pydantic_core import SchemaSerializer , to_json
2021from pydantic_core .core_schema import any_schema
2122
2223import temporalio .api .common .v1
3233# implements __get_pydantic_core_schema__ so that pydantic unwraps proxied types.
3334
3435
36+ @dataclass
37+ class ToJsonOptions :
38+ """Options for converting to JSON with pydantic."""
39+
40+ exclude_unset : bool = False
41+
42+
3543class PydanticJSONPlainPayloadConverter (EncodingPayloadConverter ):
3644 """Pydantic JSON payload converter.
3745
@@ -45,9 +53,10 @@ class PydanticJSONPlainPayloadConverter(EncodingPayloadConverter):
4553 See https://docs.pydantic.dev/latest/api/standard_library_types/
4654 """
4755
48- def __init__ (self , exclude_unset = False ):
56+ def __init__ (self , to_json_options : Optional [ToJsonOptions ]):
57+ """Create a new payload converter."""
4958 self ._schema_serializer = SchemaSerializer (any_schema ())
50- self ._exclude_unset = exclude_unset
59+ self ._to_json_options = to_json_options
5160
5261 @property
5362 def encoding (self ) -> str :
@@ -62,7 +71,13 @@ def to_payload(self, value: Any) -> Optional[temporalio.api.common.v1.Payload]:
6271 See
6372 https://docs.pydantic.dev/latest/api/pydantic_core/#pydantic_core.to_json.
6473 """
65- data = self ._schema_serializer .to_json (value , exclude_unset = self ._exclude_unset ) if self ._exclude_unset else to_json (value )
74+ data = (
75+ self ._schema_serializer .to_json (
76+ value , exclude_unset = self ._to_json_options .exclude_unset
77+ )
78+ if self ._to_json_options
79+ else to_json (value )
80+ )
6681 return temporalio .api .common .v1 .Payload (
6782 metadata = {"encoding" : self .encoding .encode ()}, data = data
6883 )
@@ -91,9 +106,9 @@ class PydanticPayloadConverter(CompositePayloadConverter):
91106 :py:class:`PydanticJSONPlainPayloadConverter`.
92107 """
93108
94- def __init__ (self , exclude_unset = False ) -> None :
109+ def __init__ (self , to_json_options : Optional [ ToJsonOptions ] ) -> None :
95110 """Initialize object"""
96- json_payload_converter = PydanticJSONPlainPayloadConverter (exclude_unset = exclude_unset )
111+ json_payload_converter = PydanticJSONPlainPayloadConverter (to_json_options )
97112 super ().__init__ (
98113 * (
99114 c
0 commit comments