-
Notifications
You must be signed in to change notification settings - Fork 3
Automate SparseField
implementations with serde rename support
#872
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
Merged
oguzkocer
merged 5 commits into
trunk
from
move/sparse-field-trait-implementations-to-appropriate-modules
Sep 10, 2025
Merged
Automate SparseField
implementations with serde rename support
#872
oguzkocer
merged 5 commits into
trunk
from
move/sparse-field-trait-implementations-to-appropriate-modules
Sep 10, 2025
Conversation
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
jkmassel
approved these changes
Sep 10, 2025
43f0320
to
4720275
Compare
Base automatically changed from
feature/wp-derive-params-field-traits
to
trunk
September 10, 2025 18:48
Relocate SparseField implementations from request::endpoint modules to their corresponding type modules for better organization and module cohesion. Changes: - Move impl SparseField for SparsePostFieldWith*Context from posts_endpoint to posts module - Move impl SparseField for SparseMediaFieldWith*Context from media_endpoint to media module - Move impl SparseField for SparseTemplateFieldWith*Context from templates_endpoint to templates module - Move impl SparseField for SparseSearchResultFieldWith*Context from search_endpoint to search_results module - Move impl SparseField for SparseCommentFieldWith*Context from comments_endpoint to comments module - Add SparseField import to each type module - Clean up unused SparseField imports from endpoint modules - Preserve all field name mappings and custom serialization logic
Add unit tests to validate all custom field name mappings in SparseField trait implementations across all WordPress REST API entity types. Changes: - Add tests for Posts: PostType → "type" mapping across all contexts - Add tests for Media: PostId → "post", PostType → "type" mappings - Add tests for Templates: TemplateType → "type", PostId → "wp_id" mappings - Add tests for Search Results: ObjectType → "type", ObjectSubtype → "subtype" mappings - Add tests for Comments: CommentType → "type" mapping across all contexts - Include regular field mappings (Id → "id") to verify default behavior - Test all applicable contexts (edit, embed, view) for each entity type - Use rstest parameterized tests for clean, declarative test cases - Follow naming pattern test_as_mapped_field_name_* for future method rename All 35 tests pass, confirming current manual SparseField implementations work correctly. These tests will serve as regression protection when transitioning to macro-generated implementations.
Phase 1: Remove redundant manual SparseField implementations by enhancing the WpContextual macro to automatically generate SparseField trait implementations that extract and use serde rename attributes. Changes: - Enhanced WpContextual macro to extract serde rename attributes and generate appropriate SparseField implementations - Removed manual SparseField implementations from comments, media, posts, search_results, and templates modules - Eliminated redundant default_sparse_field_implementation_from_field_name macro and all its usage - Cleaned up imports and moved SparseField imports to test modules where needed The macro now automatically handles field name mapping using serde rename attributes, eliminating the need for manual implementations and reducing code duplication while maintaining full test compatibility.
Rename `as_str()` to `as_mapped_field_name()` to clearly indicate that this method returns the API field name that may differ from the Rust field name due to serde rename attributes. Changes: - Update SparseField trait method signature from `as_str()` to `as_mapped_field_name()` - Update all implementations and test assertions to use the new method name - Update WpContextual macro to generate the renamed method - Update request builder to use the new method for `_fields` query parameters - Improve library documentation to clarify serde rename support - Remove unused `as_field_name()` method generation from WpContextual macro This makes the API more self-documenting and clearly distinguishes between raw field names and their mapped API equivalents.
8f5911b
to
f1738d7
Compare
Admin merging it as CI confirmed the changes before the rebase. |
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.
This PR enhances the
WpContextual
derive macro to automatically generateSparseField
trait implementations, eliminating manual code duplication and adding proper serde rename attribute support.Summary
Automated
SparseField
generation: TheWpContextual
macro now automatically generatesSparseField
trait implementations that extract and use#[serde(rename = "...")]
attributes for correct API field name mapping.Comprehensive unit tests: Added unit tests across all contextual types to verify that field mappings correctly handle both standard field names and serde-renamed fields.
Explicit method naming: Renamed
SparseField::as_str()
toas_mapped_field_name()
to clearly indicate the method returns API field names that may differ from Rust field names due to serde renames.This reduces code duplication by 240+ lines while improving maintainability and ensuring consistent field name mapping throughout the codebase.