Fix custom field serializer inside a flattened field#756
Open
gaoflow wants to merge 1 commit into
Open
Conversation
A flattened dataclass field is rendered inline in the enclosing class's serialize function, but only the top-level fields' custom serializers were registered in the generated globals. Referencing a flattened child's field serializer therefore raised `SerdeError: name '<field>_serializer' is not defined`. Recurse into flattened (and optional-flattened) dataclass fields when collecting field serializers and skip_if predicates for the scope. Fixes yukinarit#453
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #756 +/- ##
==========================================
+ Coverage 90.83% 90.98% +0.15%
==========================================
Files 13 13
Lines 2040 2352 +312
Branches 370 462 +92
==========================================
+ Hits 1853 2140 +287
- Misses 124 140 +16
- Partials 63 72 +9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #453.
A custom field serializer on a field of a flattened dataclass crashed serialization:
Cause
A flattened dataclass field is rendered inline in the enclosing class's generated serialize function, so it emits references like
value_serializer(...). But the loop that registers custom field serializers (andskip_ifpredicates) in the generated globals only iterated the top-level fields — the flattened child's serializer was never put in scope.Fix
Recurse into flattened (and optional-flattened) dataclass fields when collecting field serializers /
skip_ifpredicates, so they are available in the enclosing scope. Handles nested flattens too.Tests
Added
test_flatten_custom_field_serializer(basic + nested flatten; fails without the patch). Full suite green (5169 passed). Deserialization was already unaffected (a flattened child is deserialized via its own scoped function).Thanks @AustinScola for the report and minimal reproducer.