Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions src/serializers/type_serializers/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::tools::SchemaDict;
use crate::PydanticSerializationUnexpectedValue;

use super::{
infer_json_key, infer_serialize, infer_to_python, py_err_se_err, BuildSerializer, CombinedSerializer, Extra,
SerCheck, TypeSerializer,
infer_json_key, infer_serialize, infer_to_python, BuildSerializer, CombinedSerializer, Extra, SerCheck,
TypeSerializer,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -78,13 +78,14 @@ impl TypeSerializer for UnionSerializer {
// try the serializers in left to right order with error_on fallback=true
let mut new_extra = extra.clone();
new_extra.check = SerCheck::Strict;
let mut errors: Vec<PyErr> = Vec::new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using SmallVec similar to in the validation case (so we don't allocate for small unions). Same in each of the below.


for comb_serializer in &self.choices {
match comb_serializer.to_python(value, include, exclude, &new_extra) {
Ok(v) => return Ok(v),
Err(err) => match err.is_instance_of::<PydanticSerializationUnexpectedValue>(value.py()) {
true => (),
false => return Err(err),
false => errors.push(err),
},
}
}
Expand All @@ -95,25 +96,31 @@ impl TypeSerializer for UnionSerializer {
Ok(v) => return Ok(v),
Err(err) => match err.is_instance_of::<PydanticSerializationUnexpectedValue>(value.py()) {
true => (),
false => return Err(err),
false => errors.push(err),
},
}
}
}

for err in &errors {
extra.warnings.custom_warning(err.to_string());
}

extra.warnings.on_fallback_py(self.get_name(), value, extra)?;
infer_to_python(value, include, exclude, extra)
}

fn json_key<'a>(&self, key: &'a Bound<'_, PyAny>, extra: &Extra) -> PyResult<Cow<'a, str>> {
let mut new_extra = extra.clone();
new_extra.check = SerCheck::Strict;
let mut errors: Vec<PyErr> = Vec::new();

for comb_serializer in &self.choices {
match comb_serializer.json_key(key, &new_extra) {
Ok(v) => return Ok(v),
Err(err) => match err.is_instance_of::<PydanticSerializationUnexpectedValue>(key.py()) {
true => (),
false => return Err(err),
false => errors.push(err),
},
}
}
Expand All @@ -124,12 +131,16 @@ impl TypeSerializer for UnionSerializer {
Ok(v) => return Ok(v),
Err(err) => match err.is_instance_of::<PydanticSerializationUnexpectedValue>(key.py()) {
true => (),
false => return Err(err),
false => errors.push(err),
},
}
}
}

for err in &errors {
extra.warnings.custom_warning(err.to_string());
}

extra.warnings.on_fallback_py(self.get_name(), key, extra)?;
infer_json_key(key, extra)
}
Expand All @@ -145,12 +156,14 @@ impl TypeSerializer for UnionSerializer {
let py = value.py();
let mut new_extra = extra.clone();
new_extra.check = SerCheck::Strict;
let mut errors: Vec<PyErr> = Vec::new();

for comb_serializer in &self.choices {
match comb_serializer.to_python(value, include, exclude, &new_extra) {
Ok(v) => return infer_serialize(v.bind(py), serializer, None, None, extra),
Err(err) => match err.is_instance_of::<PydanticSerializationUnexpectedValue>(py) {
Err(err) => match err.is_instance_of::<PydanticSerializationUnexpectedValue>(value.py()) {
true => (),
false => return Err(py_err_se_err(err)),
false => errors.push(err),
},
}
}
Expand All @@ -159,14 +172,18 @@ impl TypeSerializer for UnionSerializer {
for comb_serializer in &self.choices {
match comb_serializer.to_python(value, include, exclude, &new_extra) {
Ok(v) => return infer_serialize(v.bind(py), serializer, None, None, extra),
Err(err) => match err.is_instance_of::<PydanticSerializationUnexpectedValue>(py) {
Err(err) => match err.is_instance_of::<PydanticSerializationUnexpectedValue>(value.py()) {
true => (),
false => return Err(py_err_se_err(err)),
false => errors.push(err),
},
}
}
}

for err in &errors {
extra.warnings.custom_warning(err.to_string());
}

extra.warnings.on_fallback_ser::<S>(self.get_name(), value, extra)?;
infer_serialize(value, serializer, include, exclude, extra)
}
Expand Down
24 changes: 24 additions & 0 deletions tests/serializers/test_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,27 @@ def test_union_serializer_picks_exact_type_over_subclass_json(
)
assert s.to_python(input_value, mode='json') == expected_value
assert s.to_json(input_value) == json.dumps(expected_value).encode()


def test_custom_serializer() -> None:
s = SchemaSerializer(
core_schema.union_schema(
[
core_schema.dict_schema(
keys_schema=core_schema.any_schema(),
values_schema=core_schema.any_schema(),
serialization=core_schema.plain_serializer_function_ser_schema(lambda x: x['id']),
),
core_schema.list_schema(
items_schema=core_schema.dict_schema(
keys_schema=core_schema.any_schema(),
values_schema=core_schema.any_schema(),
serialization=core_schema.plain_serializer_function_ser_schema(lambda x: x['id']),
)
),
]
)
)
print(s)
assert s.to_python([{'id': 1}, {'id': 2}]) == [1, 2]
assert s.to_python({'id': 1}) == 1