@@ -8,9 +8,7 @@ use std::borrow::Cow;
8
8
use crate :: build_tools:: py_schema_err;
9
9
use crate :: common:: union:: { Discriminator , SMALL_UNION_THRESHOLD } ;
10
10
use crate :: definitions:: DefinitionsBuilder ;
11
- use crate :: serializers:: type_serializers:: py_err_se_err;
12
11
use crate :: tools:: { truncate_safe_repr, SchemaDict } ;
13
- use crate :: PydanticSerializationUnexpectedValue ;
14
12
15
13
use super :: {
16
14
infer_json_key, infer_serialize, infer_to_python, BuildSerializer , CombinedSerializer , Extra , SerCheck ,
@@ -77,7 +75,6 @@ fn to_python(
77
75
exclude : Option < & Bound < ' _ , PyAny > > ,
78
76
extra : & Extra ,
79
77
choices : & [ CombinedSerializer ] ,
80
- name : & str ,
81
78
retry_with_lax_check : bool ,
82
79
) -> PyResult < PyObject > {
83
80
// try the serializers in left to right order with error_on fallback=true
@@ -88,22 +85,15 @@ fn to_python(
88
85
for comb_serializer in choices {
89
86
match comb_serializer. to_python ( value, include, exclude, & new_extra) {
90
87
Ok ( v) => return Ok ( v) ,
91
- Err ( err) => match err. is_instance_of :: < PydanticSerializationUnexpectedValue > ( value. py ( ) ) {
92
- true => ( ) ,
93
- false => errors. push ( err) ,
94
- } ,
88
+ Err ( err) => errors. push ( err) ,
95
89
}
96
90
}
97
91
98
92
if retry_with_lax_check {
99
93
new_extra. check = SerCheck :: Lax ;
100
94
for comb_serializer in choices {
101
- match comb_serializer. to_python ( value, include, exclude, & new_extra) {
102
- Ok ( v) => return Ok ( v) ,
103
- Err ( err) => match err. is_instance_of :: < PydanticSerializationUnexpectedValue > ( value. py ( ) ) {
104
- true => ( ) ,
105
- false => errors. push ( err) ,
106
- } ,
95
+ if let Ok ( v) = comb_serializer. to_python ( value, include, exclude, & new_extra) {
96
+ return Ok ( v) ;
107
97
}
108
98
}
109
99
}
@@ -112,15 +102,13 @@ fn to_python(
112
102
extra. warnings . custom_warning ( err. to_string ( ) ) ;
113
103
}
114
104
115
- extra. warnings . on_fallback_py ( name, value, extra) ?;
116
105
infer_to_python ( value, include, exclude, extra)
117
106
}
118
107
119
108
fn json_key < ' a > (
120
109
key : & ' a Bound < ' _ , PyAny > ,
121
110
extra : & Extra ,
122
111
choices : & [ CombinedSerializer ] ,
123
- name : & str ,
124
112
retry_with_lax_check : bool ,
125
113
) -> PyResult < Cow < ' a , str > > {
126
114
let mut new_extra = extra. clone ( ) ;
@@ -130,22 +118,15 @@ fn json_key<'a>(
130
118
for comb_serializer in choices {
131
119
match comb_serializer. json_key ( key, & new_extra) {
132
120
Ok ( v) => return Ok ( v) ,
133
- Err ( err) => match err. is_instance_of :: < PydanticSerializationUnexpectedValue > ( key. py ( ) ) {
134
- true => ( ) ,
135
- false => errors. push ( err) ,
136
- } ,
121
+ Err ( err) => errors. push ( err) ,
137
122
}
138
123
}
139
124
140
125
if retry_with_lax_check {
141
126
new_extra. check = SerCheck :: Lax ;
142
127
for comb_serializer in choices {
143
- match comb_serializer. json_key ( key, & new_extra) {
144
- Ok ( v) => return Ok ( v) ,
145
- Err ( err) => match err. is_instance_of :: < PydanticSerializationUnexpectedValue > ( key. py ( ) ) {
146
- true => ( ) ,
147
- false => errors. push ( err) ,
148
- } ,
128
+ if let Ok ( v) = comb_serializer. json_key ( key, & new_extra) {
129
+ return Ok ( v) ;
149
130
}
150
131
}
151
132
}
@@ -154,7 +135,6 @@ fn json_key<'a>(
154
135
extra. warnings . custom_warning ( err. to_string ( ) ) ;
155
136
}
156
137
157
- extra. warnings . on_fallback_py ( name, key, extra) ?;
158
138
infer_json_key ( key, extra)
159
139
}
160
140
@@ -166,7 +146,6 @@ fn serde_serialize<S: serde::ser::Serializer>(
166
146
exclude : Option < & Bound < ' _ , PyAny > > ,
167
147
extra : & Extra ,
168
148
choices : & [ CombinedSerializer ] ,
169
- name : & str ,
170
149
retry_with_lax_check : bool ,
171
150
) -> Result < S :: Ok , S :: Error > {
172
151
let py = value. py ( ) ;
@@ -177,22 +156,15 @@ fn serde_serialize<S: serde::ser::Serializer>(
177
156
for comb_serializer in choices {
178
157
match comb_serializer. to_python ( value, include, exclude, & new_extra) {
179
158
Ok ( v) => return infer_serialize ( v. bind ( py) , serializer, None , None , extra) ,
180
- Err ( err) => match err. is_instance_of :: < PydanticSerializationUnexpectedValue > ( py) {
181
- true => ( ) ,
182
- false => errors. push ( err) ,
183
- } ,
159
+ Err ( err) => errors. push ( err) ,
184
160
}
185
161
}
186
162
187
163
if retry_with_lax_check {
188
164
new_extra. check = SerCheck :: Lax ;
189
165
for comb_serializer in choices {
190
- match comb_serializer. to_python ( value, include, exclude, & new_extra) {
191
- Ok ( v) => return infer_serialize ( v. bind ( py) , serializer, None , None , extra) ,
192
- Err ( err) => match err. is_instance_of :: < PydanticSerializationUnexpectedValue > ( py) {
193
- true => ( ) ,
194
- false => errors. push ( err) ,
195
- } ,
166
+ if let Ok ( v) = comb_serializer. to_python ( value, include, exclude, & new_extra) {
167
+ return infer_serialize ( v. bind ( py) , serializer, None , None , extra) ;
196
168
}
197
169
}
198
170
}
@@ -201,7 +173,6 @@ fn serde_serialize<S: serde::ser::Serializer>(
201
173
extra. warnings . custom_warning ( err. to_string ( ) ) ;
202
174
}
203
175
204
- extra. warnings . on_fallback_ser :: < S > ( name, value, extra) ?;
205
176
infer_serialize ( value, serializer, include, exclude, extra)
206
177
}
207
178
@@ -219,13 +190,12 @@ impl TypeSerializer for UnionSerializer {
219
190
exclude,
220
191
extra,
221
192
& self . choices ,
222
- self . get_name ( ) ,
223
193
self . retry_with_lax_check ( ) ,
224
194
)
225
195
}
226
196
227
197
fn json_key < ' a > ( & self , key : & ' a Bound < ' _ , PyAny > , extra : & Extra ) -> PyResult < Cow < ' a , str > > {
228
- json_key ( key, extra, & self . choices , self . get_name ( ) , self . retry_with_lax_check ( ) )
198
+ json_key ( key, extra, & self . choices , self . retry_with_lax_check ( ) )
229
199
}
230
200
231
201
fn serde_serialize < S : serde:: ser:: Serializer > (
@@ -243,7 +213,6 @@ impl TypeSerializer for UnionSerializer {
243
213
exclude,
244
214
extra,
245
215
& self . choices ,
246
- self . get_name ( ) ,
247
216
self . retry_with_lax_check ( ) ,
248
217
)
249
218
}
@@ -313,8 +282,6 @@ impl TypeSerializer for TaggedUnionSerializer {
313
282
exclude : Option < & Bound < ' _ , PyAny > > ,
314
283
extra : & Extra ,
315
284
) -> PyResult < PyObject > {
316
- let py = value. py ( ) ;
317
-
318
285
let mut new_extra = extra. clone ( ) ;
319
286
new_extra. check = SerCheck :: Strict ;
320
287
@@ -325,15 +292,14 @@ impl TypeSerializer for TaggedUnionSerializer {
325
292
326
293
match serializer. to_python ( value, include, exclude, & new_extra) {
327
294
Ok ( v) => return Ok ( v) ,
328
- Err ( err ) => match err . is_instance_of :: < PydanticSerializationUnexpectedValue > ( py ) {
329
- true => {
330
- if self . retry_with_lax_check ( ) {
331
- new_extra . check = SerCheck :: Lax ;
332
- return serializer . to_python ( value , include , exclude , & new_extra ) ;
295
+ Err ( _ ) => {
296
+ if self . retry_with_lax_check ( ) {
297
+ new_extra . check = SerCheck :: Lax ;
298
+ if let Ok ( v ) = serializer . to_python ( value , include , exclude , & new_extra ) {
299
+ return Ok ( v ) ;
333
300
}
334
301
}
335
- false => return Err ( err) ,
336
- } ,
302
+ }
337
303
}
338
304
}
339
305
}
@@ -344,13 +310,11 @@ impl TypeSerializer for TaggedUnionSerializer {
344
310
exclude,
345
311
extra,
346
312
& self . choices ,
347
- self . get_name ( ) ,
348
313
self . retry_with_lax_check ( ) ,
349
314
)
350
315
}
351
316
352
317
fn json_key < ' a > ( & self , key : & ' a Bound < ' _ , PyAny > , extra : & Extra ) -> PyResult < Cow < ' a , str > > {
353
- let py = key. py ( ) ;
354
318
let mut new_extra = extra. clone ( ) ;
355
319
new_extra. check = SerCheck :: Strict ;
356
320
@@ -361,20 +325,19 @@ impl TypeSerializer for TaggedUnionSerializer {
361
325
362
326
match serializer. json_key ( key, & new_extra) {
363
327
Ok ( v) => return Ok ( v) ,
364
- Err ( err ) => match err . is_instance_of :: < PydanticSerializationUnexpectedValue > ( py ) {
365
- true => {
366
- if self . retry_with_lax_check ( ) {
367
- new_extra . check = SerCheck :: Lax ;
368
- return serializer . json_key ( key , & new_extra ) ;
328
+ Err ( _ ) => {
329
+ if self . retry_with_lax_check ( ) {
330
+ new_extra . check = SerCheck :: Lax ;
331
+ if let Ok ( v ) = serializer . json_key ( key , & new_extra ) {
332
+ return Ok ( v ) ;
369
333
}
370
334
}
371
- false => return Err ( err) ,
372
- } ,
335
+ }
373
336
}
374
337
}
375
338
}
376
339
377
- json_key ( key, extra, & self . choices , self . get_name ( ) , self . retry_with_lax_check ( ) )
340
+ json_key ( key, extra, & self . choices , self . retry_with_lax_check ( ) )
378
341
}
379
342
380
343
fn serde_serialize < S : serde:: ser:: Serializer > (
@@ -396,18 +359,14 @@ impl TypeSerializer for TaggedUnionSerializer {
396
359
397
360
match selected_serializer. to_python ( value, include, exclude, & new_extra) {
398
361
Ok ( v) => return infer_serialize ( v. bind ( py) , serializer, None , None , extra) ,
399
- Err ( err) => match err. is_instance_of :: < PydanticSerializationUnexpectedValue > ( py) {
400
- true => {
401
- if self . retry_with_lax_check ( ) {
402
- new_extra. check = SerCheck :: Lax ;
403
- match selected_serializer. to_python ( value, include, exclude, & new_extra) {
404
- Ok ( v) => return infer_serialize ( v. bind ( py) , serializer, None , None , extra) ,
405
- Err ( err) => return Err ( py_err_se_err ( err) ) ,
406
- }
362
+ Err ( _) => {
363
+ if self . retry_with_lax_check ( ) {
364
+ new_extra. check = SerCheck :: Lax ;
365
+ if let Ok ( v) = selected_serializer. to_python ( value, include, exclude, & new_extra) {
366
+ return infer_serialize ( v. bind ( py) , serializer, None , None , extra) ;
407
367
}
408
368
}
409
- false => return Err ( py_err_se_err ( err) ) ,
410
- } ,
369
+ }
411
370
}
412
371
}
413
372
}
@@ -419,7 +378,6 @@ impl TypeSerializer for TaggedUnionSerializer {
419
378
exclude,
420
379
extra,
421
380
& self . choices ,
422
- self . get_name ( ) ,
423
381
self . retry_with_lax_check ( ) ,
424
382
)
425
383
}
0 commit comments