@@ -159,21 +159,28 @@ impl GeneralFieldsSerializer {
159
159
if matches ! ( extra. sort_keys, SortKeysMode :: Unsorted ) {
160
160
for result in main_iter {
161
161
let ( key, value) = result?;
162
+ let key_str = key_str ( & key) ?;
162
163
if let Some ( is_required) =
163
- self . process_field_entry_python ( & key, & value, & output_dict, include, exclude, & extra) ?
164
+ self . process_field_entry_python ( key_str , & key, & value, & output_dict, include, exclude, & extra) ?
164
165
{
165
166
if is_required {
166
167
used_req_fields += 1 ;
167
168
}
168
169
}
169
170
}
170
171
} else {
171
- let mut items = main_iter. collect :: < PyResult < Vec < _ > > > ( ) ?;
172
- items. sort_by_cached_key ( |( key, _) | key_str ( key) . unwrap_or_default ( ) . to_string ( ) ) ;
173
-
174
- for ( key, value) in items {
172
+ let mut items = main_iter
173
+ . map ( |r| -> PyResult < _ > {
174
+ let ( k, v) = r?;
175
+ let k_str = key_str ( & k) ?. to_owned ( ) ;
176
+ Ok ( ( k_str, k, v) )
177
+ } )
178
+ . collect :: < PyResult < Vec < _ > > > ( ) ?;
179
+ items. sort_by ( |( a, _, _) , ( b, _, _) | a. cmp ( b) ) ;
180
+
181
+ for ( key_str, key, value) in items {
175
182
if let Some ( is_required) =
176
- self . process_field_entry_python ( & key, & value, & output_dict, include, exclude, & extra) ?
183
+ self . process_field_entry_python ( & key_str , & key, & value, & output_dict, include, exclude, & extra) ?
177
184
{
178
185
if is_required {
179
186
used_req_fields += 1 ;
@@ -218,8 +225,10 @@ impl GeneralFieldsSerializer {
218
225
Ok ( sorted_dict)
219
226
}
220
227
228
+ #[ allow( clippy:: too_many_arguments) ]
221
229
fn process_field_entry_python < ' py > (
222
230
& self ,
231
+ key_str : & str ,
223
232
key : & Bound < ' py , PyAny > ,
224
233
value : & Bound < ' py , PyAny > ,
225
234
output_dict : & Bound < ' py , PyDict > ,
@@ -231,7 +240,6 @@ impl GeneralFieldsSerializer {
231
240
// - Some(true) -> Field was required and processed
232
241
// - Some(false) -> Field was processed but not required
233
242
// - None -> Field was filtered out or skipped
234
- let key_str = key_str ( key) ?;
235
243
let op_field = self . fields . get ( key_str) ;
236
244
237
245
if extra. exclude_none && value. is_none ( ) {
@@ -301,11 +309,11 @@ impl GeneralFieldsSerializer {
301
309
None => infer_to_python ( value, next_include. as_ref ( ) , next_exclude. as_ref ( ) , & field_extra) ?,
302
310
}
303
311
} ;
304
- output_dict. set_item ( key , processed_value) ?;
312
+ output_dict. set_item ( key_str , processed_value) ?;
305
313
return Ok ( None ) ;
306
314
} else if field_extra. check == SerCheck :: Strict {
307
315
return Err ( PydanticSerializationUnexpectedValue :: new (
308
- Some ( format ! ( "Unexpected field `{key }`" ) ) ,
316
+ Some ( format ! ( "Unexpected field `{key_str }`" ) ) ,
309
317
field_extra. model_type_name ( ) . map ( |bound| bound. to_string ( ) ) ,
310
318
None ,
311
319
)
@@ -332,20 +340,30 @@ impl GeneralFieldsSerializer {
332
340
if matches ! ( extra. sort_keys, SortKeysMode :: Unsorted ) {
333
341
for result in main_iter {
334
342
let ( key, value) = result. map_err ( py_err_se_err) ?;
335
- self . process_field_entry :: < S > ( & key, & value, & mut map, include, exclude, & extra) ?;
343
+ let key_str = key_str ( & key) . map_err ( py_err_se_err) ?;
344
+ self . process_field_entry :: < S > ( key_str, & key, & value, & mut map, include, exclude, & extra) ?;
336
345
}
337
346
} else {
338
- let mut items = main_iter. collect :: < PyResult < Vec < _ > > > ( ) . map_err ( py_err_se_err) ?;
339
- items. sort_by_cached_key ( |( key, _) | key_str ( key) . unwrap_or_default ( ) . to_string ( ) ) ;
340
- for ( key, value) in items {
341
- self . process_field_entry :: < S > ( & key, & value, & mut map, include, exclude, & extra) ?;
347
+ let mut items = main_iter
348
+ . map ( |r| -> PyResult < _ > {
349
+ let ( k, v) = r?;
350
+ let k_str = key_str ( & k) ?. to_owned ( ) ;
351
+ Ok ( ( k_str, k, v) )
352
+ } )
353
+ . collect :: < PyResult < Vec < _ > > > ( )
354
+ . map_err ( py_err_se_err) ?;
355
+ items. sort_by ( |( a, _, _) , ( b, _, _) | a. cmp ( b) ) ;
356
+ for ( key_str, key, value) in items {
357
+ self . process_field_entry :: < S > ( & key_str, & key, & value, & mut map, include, exclude, & extra) ?;
342
358
}
343
359
}
344
360
Ok ( map)
345
361
}
346
362
363
+ #[ allow( clippy:: too_many_arguments) ]
347
364
fn process_field_entry < ' py , S : serde:: ser:: Serializer > (
348
365
& self ,
366
+ key_str : & str ,
349
367
key : & Bound < ' py , PyAny > ,
350
368
value : & Bound < ' py , PyAny > ,
351
369
map : & mut S :: SerializeMap ,
@@ -356,15 +374,14 @@ impl GeneralFieldsSerializer {
356
374
if extra. exclude_none && value. is_none ( ) {
357
375
return Ok ( ( ) ) ;
358
376
}
359
- let field_key_str = key_str ( key) . map_err ( py_err_se_err) ?;
360
377
let field_extra = Extra {
361
- field_name : Some ( field_key_str ) ,
378
+ field_name : Some ( key_str ) ,
362
379
..* extra
363
380
} ;
364
381
365
382
let filter = self . filter . key_filter ( key, include, exclude) . map_err ( py_err_se_err) ?;
366
383
if let Some ( ( next_include, next_exclude) ) = filter {
367
- if let Some ( field) = self . fields . get ( field_key_str ) {
384
+ if let Some ( field) = self . fields . get ( key_str ) {
368
385
if let Some ( ref serializer) = field. serializer {
369
386
if !exclude_default ( value, & field_extra, serializer) . map_err ( py_err_se_err) ? {
370
387
if matches ! ( extra. sort_keys, SortKeysMode :: Recursive ) && value. downcast :: < PyDict > ( ) . is_ok ( ) {
@@ -376,7 +393,7 @@ impl GeneralFieldsSerializer {
376
393
next_exclude. as_ref ( ) ,
377
394
& field_extra,
378
395
) ;
379
- let output_key = field. get_key_json ( field_key_str , & field_extra) ;
396
+ let output_key = field. get_key_json ( key_str , & field_extra) ;
380
397
map. serialize_entry ( & output_key, & s) ?;
381
398
} else {
382
399
let s = PydanticSerializer :: new (
@@ -386,7 +403,7 @@ impl GeneralFieldsSerializer {
386
403
next_exclude. as_ref ( ) ,
387
404
& field_extra,
388
405
) ;
389
- let output_key = field. get_key_json ( field_key_str , & field_extra) ;
406
+ let output_key = field. get_key_json ( key_str , & field_extra) ;
390
407
map. serialize_entry ( & output_key, & s) ?;
391
408
}
392
409
}
0 commit comments