Repository navigation
REST API: Allow resetting meta keys with nothing stored to null - #14845
dannflorea wants to merge 2 commits into
Conversation
Only validate the current value of a single meta key before a null reset when a value is actually stored, so unset boolean, integer and enum keys no longer fail with rest_invalid_stored_value. See #66377.
|
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. |
| $current = get_metadata_raw( $this->get_meta_type(), $object_id, $meta_key, true ); | ||
|
|
||
| if ( is_wp_error( rest_validate_value_from_schema( $current, $args['schema'] ) ) ) { | ||
| if ( null !== $current && is_wp_error( rest_validate_value_from_schema( $current, $args['schema'] ) ) ) { |
There was a problem hiding this comment.
In the case of null, get_metadata() falls back to get_metadata_default(). So this means that the fallback value doesn't need to be obtained here, since the null case is checked. So this seems right to me.
Tests: Add type hints suggested in review. Co-authored-by: Weston Ruter <westonruter@gmail.com>
|
Thanks for the review, @westonruter! I've applied all the type suggestions. And thanks for confirming the get_metadata_raw() reasoning: since the null case is handled, the default fallback isn't needed there. |
Trac ticket: https://core.trac.wordpress.org/ticket/66377
What
When a single meta key has nothing stored,
WP_REST_Meta_Fields::update_value()validated the empty string returned byget_metadata()before anullreset. Forboolean,integer,numberandenumkeys that fails, so the request was rejected with a 500rest_invalid_stored_valueeven though nothing was stored.This reads the current value with
get_metadata_raw(), which returnsnullwhen no row exists, and only validates when there is a stored value. A stored value that fails the schema, including a stored empty string, is still protected.This also fixes echoing back comment meta: the core
_wp_note_statuskey (enum, no default) is returned asnullwhen unset, and sending it back previously failed with a 500.Tests
test_set_value_to_null_with_nothing_stored:nullfor unset boolean, integer, number and enum keys returns 200 and creates no row.test_set_value_to_null_with_invalid_empty_string_stored: a stored empty string on a boolean key is still protected (500, row kept).test_update_comment_with_meta_echoed_back_from_response: updating a comment with the meta from its own GET response returns 200.Testing instructions
register_post_meta( 'post', 'repro_flag', array( 'type' => 'boolean', 'single' => true, 'show_in_rest' => true ) );POST /wp/v2/posts/<id>with{"meta":{"repro_flag":null}}.rest_invalid_stored_value. After: 200.Verified with WordPress Playground (trunk nightly, PHP 8.3). I did not run PHPUnit locally; relying on CI.