REST API: Ensure empty meta is returned as an object in view context#11682
REST API: Ensure empty meta is returned as an object in view context#11682liaisontw wants to merge 1 commit intoWordPress:trunkfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
bbf8c41 to
a59c400
Compare
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
a59c400 to
121f3e7
Compare
121f3e7 to
585978c
Compare
This PR addresses an inconsistency where the REST API returns an empty array [] for the meta field, despite the JSON schema defining it as an object. This behavior causes deserialization errors in strictly-typed API clients (e.g., Java, Swift).
Summary of Changes
WP_REST_Posts_Controller: Conditionally casts empty meta values to an empty object {} when the request context is view and the schema defines the field as an object. This preserves the existing array structure for edit context to ensure backward compatibility for update requests.
WP_REST_Blocks_Controller: Refactored filter_response_by_context to safely handle both array and object structures for the wp_pattern_sync_status property. The new implementation uses a more concise approach by casting to an array for value extraction while maintaining the original data structure integrity.
WP_REST_Meta_Fields:
Updated get_value() to support returning an empty stdClass object.
Updated check_meta_is_array() to ensure that incoming objects (from JSON {}) are correctly handled and validated as arrays during update operations, maintaining bi-directional compatibility.
Trac ticket: https://core.trac.wordpress.org/ticket/54484
Use of AI Tools
AI assistance: Yes
Tool(s): Gemini
Model(s): Gemini 3 Flash
Used for: Refactoring the logic in WP_REST_Blocks_Controller to reduce code complexity. The final implementation, logic boundaries (specifically regarding type strictness in validators), and compatibility checks were manually reviewed and adjusted by me to ensure adherence to WordPress Core standards.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.