Skip to content

REST API: Ensure empty meta is returned as an object in view context#11682

Open
liaisontw wants to merge 1 commit intoWordPress:trunkfrom
liaisontw:fix/54484-rest-api-return-empty-meta-object
Open

REST API: Ensure empty meta is returned as an object in view context#11682
liaisontw wants to merge 1 commit intoWordPress:trunkfrom
liaisontw:fix/54484-rest-api-return-empty-meta-object

Conversation

@liaisontw
Copy link
Copy Markdown

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.

@github-actions
Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props liaison.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@liaisontw liaisontw force-pushed the fix/54484-rest-api-return-empty-meta-object branch from bbf8c41 to a59c400 Compare April 30, 2026 05:09
@github-actions
Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@liaisontw liaisontw force-pushed the fix/54484-rest-api-return-empty-meta-object branch from a59c400 to 121f3e7 Compare April 30, 2026 05:25
@liaisontw liaisontw force-pushed the fix/54484-rest-api-return-empty-meta-object branch from 121f3e7 to 585978c Compare April 30, 2026 15:33
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.

1 participant