Repository navigation
Editor: Validate the entity in the entity fields reads and REST route - #16067
mukeshpanchal27 wants to merge 4 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
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. |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the entity-fields validation and route-collision changes.
No open issues 🎉
What changed in this PR
This PR aligns the new entity fields API with the entity actions API’s validation and route-collision safeguards for WordPress 7.2.
Changes:
- Validate entity kinds and names before registry reads, returning empty lists with an incorrect-usage notice for invalid input.
- Reject empty REST arguments and preserve conflicting post type or taxonomy routes.
- Add regression tests for invalid registry inputs, empty REST arguments, and taxonomy route collisions.
| File | Description |
|---|---|
tests/phpunit/tests/rest-api/rest-entity-fields-controller.php |
Tests empty arguments and preservation of a conflicting taxonomy route. |
tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php |
Tests invalid entity inputs in both registry read methods. |
src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php |
Adds minimum argument lengths and a route-collision guard. |
src/wp-includes/class-wp-entity-fields-registry.php |
Validates entity inputs before reads and documents empty results. |
Review effort: Balanced
| * Empty when the entity is invalid. | ||
| */ | ||
| public function get_registered( $kind, $name ) { | ||
| if ( ! $this->validate_entity( __METHOD__, $kind, $name ) ) { |
There was a problem hiding this comment.
These checks are good, we should have them. Thanks.
| 'kind' => array( | ||
| 'description' => __( 'Entity kind.' ), | ||
| 'type' => 'string', | ||
| 'minLength' => 1, |
There was a problem hiding this comment.
This is good, thanks for bringing them in for entity fields.
| * | ||
| * @return bool Whether a post type or taxonomy has its items on the route. | ||
| */ | ||
| protected function is_route_of_entity_items() { |
There was a problem hiding this comment.
This is not necessary and actually counter-productive. We want the endpoint to be registered. It should also be removed in actions as it's an old artifact from when the endpoint as named just actions and we had a client-side fallback. Right now, both fields and actions are registered server-side and need these endpoints to be registered.
Given how time-sensitive this is (we want it in 7.2 RC cycle), I've pushed directly the fixes.
The entity fields API lacked checks that the entity actions API has: - `WP_Entity_Fields_Registry::get_registered()` and `::get_registered_field_modules()` did not validate the entity kind and name, so an array caused a fatal `TypeError`. They now return an empty list with a notice. - The `kind` and `name` arguments of `/wp/v2/entity-fields` had no `minLength`, so empty values returned a 200 response. They now return a 400 error. - The `/wp/v2/entity-fields` route was registered even when a post type or taxonomy uses `entity-fields` as its REST base. It now leaves the route to the post type or taxonomy and reports it, like the actions route. Follow-up to [64528]. See #66229. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ents. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dpoint. The other core endpoints with a fixed base do not check whether a post type or taxonomy already serves its items on their route, so the entity actions endpoint no longer does either. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…point. The other core endpoints with a fixed base do not check whether a post type or taxonomy already serves its items on their route, so the entity fields endpoint no longer does either. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7e58509 to
26266c2
Compare
|
@mukeshpanchal27 I've rebased this onto |
The entity fields API is new in 7.2. It is missing two input checks that the entity actions API already has. This PR adds them, matching the actions code. It also removes the route-collision check from the entity actions endpoint, so that both endpoints behave like the other core endpoints.
1. Reading with an invalid entity causes a fatal error
WP_Entity_Fields_Registry::get_registered()and::get_registered_field_modules()do not validate$kindand$name. Withwp_get_registered_entity_fields( array(), 'page' ), PHP 8 throwsTypeError: Cannot access offset of type array on array.The actions registry calls
validate_entity()in both read methods and returnsarray()with a_doing_it_wrong()notice. The fields registry already has the samevalidate_entity()method, so both read methods now call it.2. Empty
kindandnamereturn 200GET /wp/v2/entity-fields?kind=&name=returns200with an empty field list, because the arguments have nominLength. The same request to/wp/v2/entity-actionsreturns400. This PR adds'minLength' => 1to both arguments.3. Remove the route-collision check from the entity actions endpoint
WP_REST_Entity_Actions_Controller::is_route_of_entity_items()skipped registering/wp/v2/entity-actionswhen a post type or taxonomy withshow_in_restusesentity-actionsas its REST base. An earlier version of this PR added the same check to the fields endpoint.No other core endpoint with a fixed base, such as
/wp/v2/settings,/wp/v2/search,/wp/v2/block-typesor/wp/v2/global-styles, has this check. The collision needs a plugin to choose that REST base explicitly. So this PR removes the check, and its test, from the actions endpoint instead of copying it to the fields endpoint. Both endpoints now behave like the rest of core.This removes one translatable string (the actions
_doing_it_wrong()message) and adds none.Tests
test_reading_with_an_invalid_entity_returns_empty_lists()covers an array kind, an array name, an empty kind, and an integer name.test_get_items_requires_kind_and_name()now also covers an emptykindand an emptyname.test_the_route_of_a_taxonomy_with_the_same_base_is_kept()is removed from the actions controller tests, along with the check.wp-api-generated.js) is updated for the newminLengtharguments.Trac ticket: https://core.trac.wordpress.org/ticket/66229
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Finding the issues while reviewing 7.2 changes, drafting the fix and tests, and running the tests. I reviewed the final change.
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.
🤖 Generated with Claude Code