Skip to content

Conversation

oguzkocer
Copy link
Contributor

This PR enhances the WpContextual derive macro to automatically generate SparseField trait implementations, eliminating manual code duplication and adding proper serde rename attribute support.

Summary

  1. Automated SparseField generation: The WpContextual macro now automatically generates SparseField trait implementations that extract and use #[serde(rename = "...")] attributes for correct API field name mapping.

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

  3. Explicit method naming: Renamed SparseField::as_str() to as_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.

@oguzkocer oguzkocer added this to the 0.2 milestone Sep 4, 2025
@oguzkocer oguzkocer force-pushed the feature/wp-derive-params-field-traits branch from 43f0320 to 4720275 Compare September 10, 2025 18:48
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.
@oguzkocer oguzkocer force-pushed the move/sparse-field-trait-implementations-to-appropriate-modules branch from 8f5911b to f1738d7 Compare September 10, 2025 18:50
@oguzkocer oguzkocer marked this pull request as ready for review September 10, 2025 18:50
@oguzkocer
Copy link
Contributor Author

Admin merging it as CI confirmed the changes before the rebase.

@oguzkocer oguzkocer merged commit cc9fa67 into trunk Sep 10, 2025
1 of 2 checks passed
@oguzkocer oguzkocer deleted the move/sparse-field-trait-implementations-to-appropriate-modules branch September 10, 2025 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants