@@ -8,6 +8,7 @@ use std::borrow::Cow;
88use  crate :: build_tools:: py_schema_err; 
99use  crate :: common:: union:: { Discriminator ,  SMALL_UNION_THRESHOLD } ; 
1010use  crate :: definitions:: DefinitionsBuilder ; 
11+ use  crate :: serializers:: PydanticSerializationUnexpectedValue ; 
1112use  crate :: tools:: { truncate_safe_repr,  SchemaDict } ; 
1213
1314use  super :: { 
@@ -79,15 +80,15 @@ fn union_serialize<S, R>(
7980    extra :  & Extra , 
8081    choices :  & [ CombinedSerializer ] , 
8182    retry_with_lax_check :  bool , 
82- )  -> R  { 
83+ )  -> PyResult < R >  { 
8384    // try the serializers in left to right order with error_on fallback=true 
8485    let  mut  new_extra = extra. clone ( ) ; 
8586    new_extra. check  = SerCheck :: Strict ; 
8687    let  mut  errors:  SmallVec < [ PyErr ;  SMALL_UNION_THRESHOLD ] >  = SmallVec :: new ( ) ; 
8788
8889    for  comb_serializer in  choices { 
8990        match  selector ( comb_serializer,  & new_extra)  { 
90-             Ok ( v)  => return  finalizer ( Some ( v) ) , 
91+             Ok ( v)  => return  Ok ( finalizer ( Some ( v) ) ) , 
9192            Err ( err)  => errors. push ( err) , 
9293        } 
9394    } 
@@ -97,7 +98,7 @@ fn union_serialize<S, R>(
9798        new_extra. check  = SerCheck :: Lax ; 
9899        for  comb_serializer in  choices { 
99100            if  let  Ok ( v)  = selector ( comb_serializer,  & new_extra)  { 
100-                 return  finalizer ( Some ( v) ) ; 
101+                 return  Ok ( finalizer ( Some ( v) ) ) ; 
101102            } 
102103        } 
103104    } 
@@ -110,13 +111,12 @@ fn union_serialize<S, R>(
110111    } 
111112    // Otherwise, if we've encountered errors, return them to the parent union, which should take 
112113    // care of the formatting for us 
113-     // TODO: change up return type to support this 
114-     // else if !errors.is_empty() { 
115-     //     let message = errors.iter().map(ToString::to_string).collect::<Vec<_>>().join("\n"); 
116-     //     return Err(PydanticSerializationUnexpectedValue::new_err(Some(message))); 
117-     // } 
114+     else  if  !errors. is_empty ( )  { 
115+         let  message = errors. iter ( ) . map ( ToString :: to_string) . collect :: < Vec < _ > > ( ) . join ( "\n " ) ; 
116+         return  Err ( PydanticSerializationUnexpectedValue :: new_err ( Some ( message) ) ) ; 
117+     } 
118118
119-     finalizer ( None ) 
119+     Ok ( finalizer ( None ) ) 
120120} 
121121
122122impl  TypeSerializer  for  UnionSerializer  { 
@@ -134,6 +134,7 @@ impl TypeSerializer for UnionSerializer {
134134            & self . choices , 
135135            self . retry_with_lax_check ( ) , 
136136        ) 
137+         . unwrap ( ) 
137138    } 
138139
139140    fn  json_key < ' a > ( & self ,  key :  & ' a  Bound < ' _ ,  PyAny > ,  extra :  & Extra )  -> PyResult < Cow < ' a ,  str > >  { 
@@ -144,6 +145,7 @@ impl TypeSerializer for UnionSerializer {
144145            & self . choices , 
145146            self . retry_with_lax_check ( ) , 
146147        ) 
148+         . unwrap ( ) 
147149    } 
148150
149151    fn  serde_serialize < S :  serde:: ser:: Serializer > ( 
@@ -169,6 +171,7 @@ impl TypeSerializer for UnionSerializer {
169171            & self . choices , 
170172            self . retry_with_lax_check ( ) , 
171173        ) 
174+         . unwrap ( ) 
172175    } 
173176
174177    fn  get_name ( & self )  -> & str  { 
@@ -265,6 +268,7 @@ impl TypeSerializer for TaggedUnionSerializer {
265268            & self . choices , 
266269            self . retry_with_lax_check ( ) , 
267270        ) 
271+         . unwrap ( ) 
268272    } 
269273
270274    fn  json_key < ' a > ( & self ,  key :  & ' a  Bound < ' _ ,  PyAny > ,  extra :  & Extra )  -> PyResult < Cow < ' a ,  str > >  { 
@@ -297,6 +301,7 @@ impl TypeSerializer for TaggedUnionSerializer {
297301            & self . choices , 
298302            self . retry_with_lax_check ( ) , 
299303        ) 
304+         . unwrap ( ) 
300305    } 
301306
302307    fn  serde_serialize < S :  serde:: ser:: Serializer > ( 
@@ -345,6 +350,7 @@ impl TypeSerializer for TaggedUnionSerializer {
345350            & self . choices , 
346351            self . retry_with_lax_check ( ) , 
347352        ) 
353+         . unwrap ( ) 
348354    } 
349355
350356    fn  get_name ( & self )  -> & str  { 
0 commit comments