Skip to content

Conversation

davidhewitt
Copy link
Contributor

Change Summary

Fixes pydantic/pydantic#12379

Related issue number

This adjusts logic in GeneralFieldsSerializer so that the type inference application of SerializeAsAny is applied after field serializers are applied. This is more consistent with SerializeAsAny annotation on a field that also has a field_serializer; the field_serializer would still be applied in that case too.

Checklist

  • Unit tests for the changes exist
  • Documentation reflects the changes where applicable
  • Pydantic tests pass with this pydantic-core (except for expected changes)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@davidhewitt davidhewitt requested a review from Viicos October 13, 2025 09:44
Comment on lines +332 to +335
// FIXME: should `exclude_if` be applied to extra fields too?
if serialization_exclude_if(field.serialization_exclude_if.as_ref(), value)? {
return Ok(None);
}
Copy link
Contributor Author

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?

Copy link
Member

@Viicos Viicos Oct 13, 2025

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Member

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).

Copy link
Contributor Author

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?
Copy link
Contributor Author

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?

Copy link
Member

@Viicos Viicos Oct 13, 2025

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).

Copy link

codspeed-hq bot commented Oct 13, 2025

CodSpeed Performance Report

Merging #1835 will not alter performance

Comparing dh/ser-as-any-field-serializers (55786b4) with main (0425954)

Summary

✅ 163 untouched

Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
@davidhewitt davidhewitt merged commit eb48cad into main Oct 13, 2025
32 checks passed
@davidhewitt davidhewitt deleted the dh/ser-as-any-field-serializers branch October 13, 2025 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2.12.0: field_serializer not called by model_dump(mode='json', serialize_as_any=True) in v2.12

2 participants