File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -626,3 +626,26 @@ def test_union_serializer_picks_exact_type_over_subclass_json(
626
626
)
627
627
assert s .to_python (input_value , mode = 'json' ) == expected_value
628
628
assert s .to_json (input_value ) == json .dumps (expected_value ).encode ()
629
+
630
+
631
+ def test_custom_serializer () -> None :
632
+ s = SchemaSerializer (
633
+ core_schema .union_schema (
634
+ [
635
+ core_schema .dict_schema (
636
+ keys_schema = core_schema .any_schema (),
637
+ values_schema = core_schema .any_schema (),
638
+ serialization = core_schema .plain_serializer_function_ser_schema (lambda x : x ['id' ]),
639
+ ),
640
+ core_schema .list_schema (
641
+ items_schema = core_schema .dict_schema (
642
+ keys_schema = core_schema .any_schema (),
643
+ values_schema = core_schema .any_schema (),
644
+ serialization = core_schema .plain_serializer_function_ser_schema (lambda x : x ['id' ]),
645
+ )
646
+ ),
647
+ ]
648
+ )
649
+ )
650
+ assert s .to_python ([{'id' : 1 }, {'id' : 2 }]) == [1 , 2 ]
651
+ assert s .to_python ({'id' : 1 }) == 1
You can’t perform that action at this time.
0 commit comments