Skip to content

Commit 944bf91

Browse files
authored
Assume wrap validators produce the same serialization schema (#573)
1 parent bfc74d7 commit 944bf91

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/serializers/type_serializers/function.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ impl BuildSerializer for FunctionAfterSerializerBuilder {
5050
definitions: &mut DefinitionsBuilder<CombinedSerializer>,
5151
) -> PyResult<CombinedSerializer> {
5252
let py = schema.py();
53-
// while `before` schemas have an obvious type, for
54-
// `after` schemas it's less, clear but the default will be the same type, and the user/lib can always
55-
// override the serializer
53+
// While `before` function schemas do not modify the output type (and therefore affect the
54+
// serialization), for `after` schemas, there's no way to directly infer what schema should
55+
// be used for serialization. For convenience, the default is to assume the wrapped schema
56+
// should be used; the user/lib can override the serializer if necessary.
5657
let schema = schema.get_as_req(intern!(py, "schema"))?;
5758
CombinedSerializer::build(schema, config, definitions)
5859
}
@@ -279,7 +280,13 @@ impl BuildSerializer for FunctionWrapSerializerBuilder {
279280
config: Option<&PyDict>,
280281
definitions: &mut DefinitionsBuilder<CombinedSerializer>,
281282
) -> PyResult<CombinedSerializer> {
282-
super::any::AnySerializer::build(schema, config, definitions)
283+
let py = schema.py();
284+
// While `before` function schemas do not modify the output type (and therefore affect the
285+
// serialization), for `wrap` schemas (like `after`), there's no way to directly infer what
286+
// schema should be used for serialization. For convenience, the default is to assume the
287+
// wrapped schema should be used; the user/lib can override the serializer if necessary.
288+
let schema = schema.get_as_req(intern!(py, "schema"))?;
289+
CombinedSerializer::build(schema, config, definitions)
283290
}
284291
}
285292

tests/serializers/test_functions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,21 @@ def test_pydantic_serialization_unexpected_value():
612612
v = PydanticSerializationUnexpectedValue()
613613
assert str(v) == 'Unexpected Value'
614614
assert repr(v) == 'PydanticSerializationUnexpectedValue(Unexpected Value)'
615+
616+
617+
def test_function_after_preserves_wrapped_serialization():
618+
def f(value, _info):
619+
return value
620+
621+
s = SchemaSerializer(core_schema.general_after_validator_function(f, core_schema.int_schema()))
622+
with pytest.warns(UserWarning, match='Expected `int` but got `str` - serialized value may not be as expected'):
623+
assert s.to_python('abc') == 'abc'
624+
625+
626+
def test_function_wrap_preserves_wrapped_serialization():
627+
def f(value, handler, _info):
628+
return handler(value)
629+
630+
s = SchemaSerializer(core_schema.general_wrap_validator_function(f, core_schema.int_schema()))
631+
with pytest.warns(UserWarning, match='Expected `int` but got `str` - serialized value may not be as expected'):
632+
assert s.to_python('abc') == 'abc'

0 commit comments

Comments
 (0)