-
Notifications
You must be signed in to change notification settings - Fork 309
respect field_serializer
when using serialize_as_any=True
#1835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
// FIXME: should `exclude_if` be applied to extra fields too? | ||
if serialization_exclude_if(field.serialization_exclude_if.as_ref(), value)? { | ||
return Ok(None); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a possible bug which maybe needs changing in 2.13 rather than backporting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you talking about the FIXME comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense to support it, regarding backporting I have the same opinion as in #1835 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let output_key = field.get_key_json(key_str, &field_extra); | ||
map.serialize_entry(&output_key, &s)?; | ||
} else if self.mode == FieldsMode::TypedDictAllow { | ||
// FIXME: why is `extra_serializer` not used here when `serialize_as_any` is not set? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably a bug we should repro and fix in 2.12?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a MRE:
class Model(BaseModel, extra='allow'):
a: int
__pydantic_extra__: dict[str, int]
m = Model(a=1, extra=1)
m.a = 'not_an_int'
m.model_dump_json() # PydanticSerializationUnexpectedValue warning raised
m.a = 1
m.extra = 'not_an_int'
m.model_dump_json() # No warning raised
I created pydantic/pydantic#12385. We could backport to 2.12, but this may introduce warnings that will break CI, which is unideal for a patch release (and this wasn't reported by any user yet).
CodSpeed Performance ReportMerging #1835 will not alter performanceComparing Summary
|
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
Change Summary
Fixes pydantic/pydantic#12379
Related issue number
This adjusts logic in
GeneralFieldsSerializer
so that the type inference application ofSerializeAsAny
is applied after field serializers are applied. This is more consistent withSerializeAsAny
annotation on a field that also has afield_serializer
; thefield_serializer
would still be applied in that case too.Checklist
pydantic-core
(except for expected changes)