File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -153,13 +153,17 @@ class Model(BaseModel):
153153 func : core_schema .NoInfoValidatorFunction | core_schema .WithInfoValidatorFunction
154154
155155 def __get_pydantic_core_schema__ (self , source_type : Any , handler : _GetCoreSchemaHandler ) -> core_schema .CoreSchema :
156+ schema = handler (source_type )
157+ serialization = core_schema .wrap_serializer_function_ser_schema (function = lambda v , h : h (v ), schema = schema )
156158 info_arg = _inspect_validator (self .func , 'plain' )
157159 if info_arg :
158160 func = cast (core_schema .WithInfoValidatorFunction , self .func )
159- return core_schema .with_info_plain_validator_function (func , field_name = handler .field_name )
161+ return core_schema .with_info_plain_validator_function (
162+ func , field_name = handler .field_name , serialization = serialization
163+ )
160164 else :
161165 func = cast (core_schema .NoInfoValidatorFunction , self .func )
162- return core_schema .no_info_plain_validator_function (func )
166+ return core_schema .no_info_plain_validator_function (func , serialization = serialization )
163167
164168
165169@dataclasses .dataclass (frozen = True , ** _internal_dataclass .slots_true )
Original file line number Diff line number Diff line change 2020 ConfigDict ,
2121 Field ,
2222 GetCoreSchemaHandler ,
23+ PlainSerializer ,
2324 PydanticDeprecatedSince20 ,
2425 PydanticUserError ,
2526 TypeAdapter ,
@@ -2806,3 +2807,19 @@ def value_b_validator(cls, value):
28062807 'ctx' : {'error' : IsInstance (AssertionError )},
28072808 },
28082809 ]
2810+
2811+
2812+ def test_plain_validator_plain_serializer () -> None :
2813+ """https://github.com/pydantic/pydantic/issues/8512"""
2814+ ser_type = str
2815+ serializer = PlainSerializer (lambda x : ser_type (int (x )), return_type = ser_type )
2816+ validator = PlainValidator (lambda x : bool (int (x )))
2817+
2818+ class Blah (BaseModel ):
2819+ foo : Annotated [bool , validator , serializer ]
2820+ bar : Annotated [bool , serializer , validator ]
2821+
2822+ blah = Blah (foo = '0' , bar = '1' )
2823+ data = blah .model_dump ()
2824+ assert isinstance (data ['foo' ], ser_type )
2825+ assert isinstance (data ['bar' ], ser_type )
You can’t perform that action at this time.
0 commit comments