Skip to content

REST API: Use wp_prepare_json_value_for_client() for view configs and entity fields - #14552

Closed
jorgefilipecosta wants to merge 1 commit into
WordPress:trunkfrom
jorgefilipecosta:remove/rest-cast-empty-objects-helper
Closed

jorgefilipecosta wants to merge 1 commit into
WordPress:trunkfrom
jorgefilipecosta:remove/rest-cast-empty-objects-helper

Conversation

@jorgefilipecosta

Copy link
Copy Markdown
Member

Follow up to https://core.trac.wordpress.org/changeset/64503 and https://core.trac.wordpress.org/changeset/64592.

This PR removes the private _rest_cast_empty_objects_from_schema() helper and uses the public wp_prepare_json_value_for_client() instead, so there is a single function that prepares empty objects according to a schema:

  • The view config and entity fields endpoints call wp_prepare_json_value_for_client().
  • WP_REST_View_Config_Controller::cast_empty_objects() (since 7.1) is kept and delegates to it.
  • The private helper was added in this release cycle, so it is removed without deprecation.

wp_prepare_json_value_for_client() also handles cases the private helper missed: it recurses into the matching oneOf/anyOf branch (e.g. a form field children: [ [] ] is now [ {} ]), patternProperties, stdClass and JsonSerializable values, and keeps [] when the schema also allows an array. No view config or entity fields schema allows both an object and an array, so the existing responses are unchanged.

The change was drafted with AI assistance and reviewed by me.

Trac ticket: https://core.trac.wordpress.org/ticket/66267

Testing

Verify unit tests are passing:

npm run test:php -- --group entity-fields,view-config,json-schema

…nd entity fields.

Remove the private `_rest_cast_empty_objects_from_schema()` helper, which
`wp_prepare_json_value_for_client()` supersedes. The public function also
recurses into `oneOf`/`anyOf` branches, `patternProperties`, objects and
`JsonSerializable` values, and keeps an empty array as `[]` when the schema
also allows an array.

Follow-up to [64503], [64592].

See #66267.
@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 jorgefilipecosta.

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

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

wporg-sync pushed a commit that referenced this pull request Oct 20, 2026
…nd entity fields.

Removes the private `_rest_cast_empty_objects_from_schema()` helper, so a single function prepares empty objects according to a schema. The view config and entity fields endpoints now call `wp_prepare_json_value_for_client()`, and `WP_REST_View_Config_Controller::cast_empty_objects()` delegates to it. The private helper was added in this release cycle, so it is removed without deprecation.
`wp_prepare_json_value_for_client()` also recurses into the matching `oneOf`/`anyOf` branch and into `patternProperties`, and keeps `[]` when the schema also allows an array. No view config or entity fields schema allows both an object and an array, so the existing responses are unchanged.

Developed in #14552.

Follow-up to [64503], [64592].

Props jorgefilipecosta, oandregal.
See #66267.

git-svn-id: https://develop.svn.wordpress.org/trunk@64602 602fd350-edb4-49c9-b593-d223f7449a82
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Oct 20, 2026
…nd entity fields.

Removes the private `_rest_cast_empty_objects_from_schema()` helper, so a single function prepares empty objects according to a schema. The view config and entity fields endpoints now call `wp_prepare_json_value_for_client()`, and `WP_REST_View_Config_Controller::cast_empty_objects()` delegates to it. The private helper was added in this release cycle, so it is removed without deprecation.
`wp_prepare_json_value_for_client()` also recurses into the matching `oneOf`/`anyOf` branch and into `patternProperties`, and keeps `[]` when the schema also allows an array. No view config or entity fields schema allows both an object and an array, so the existing responses are unchanged.

Developed in WordPress/wordpress-develop#14552.

Follow-up to [64503], [64592].

Props jorgefilipecosta, oandregal.
See #66267.
Built from https://develop.svn.wordpress.org/trunk@64602


git-svn-id: http://core.svn.wordpress.org/trunk@63752 1a063a9b-81f0-0310-95a4-ce76da25c4cd
@jorgefilipecosta

Copy link
Copy Markdown
Member Author

Committed at 3673e3a.

jorgefilipecosta added a commit to WordPress/gutenberg that referenced this pull request Oct 20, 2026
…nfigs and entity fields

Mirrors WordPress/wordpress-develop#14552: replaces
`gutenberg_rest_cast_empty_objects_from_schema()` with a copy of core's
`wp_prepare_json_value_for_client()` (and its private
`_wp_json_schema_matches_type()` helper), and ports core's tests.
jorgefilipecosta added a commit to WordPress/gutenberg that referenced this pull request Oct 20, 2026
…nfigs and entity fields

Mirrors WordPress/wordpress-develop#14552: replaces
`gutenberg_rest_cast_empty_objects_from_schema()` with a copy of core's
`wp_prepare_json_value_for_client()` (and its private
`_wp_json_schema_matches_type()` helper), and ports core's tests.
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.

2 participants