<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[42000] trunk: REST API: Don?\226?\128?\153t remove unregistered properties from objects in schema.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/42000">42000</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/42000","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>joehoyle</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-10-24 21:04:50 +0000 (Tue, 24 Oct 2017)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>REST API: Don?\226?\128?\153t remove unregistered properties from objects in schema.
In <a href="https://core.trac.wordpress.org/changeset/41727">r41727</a> the ability to sanitise and validate objects from JSON schema was added, with a whitelist approach. It was decided we should pass through all non-registered properties to reflect the behaviour of the root object in register_rest_route. To prevent arbitrary extra data via setting objects, we force additionalProperties to false in the settings endpoint.
See <a href="https://core.trac.wordpress.org/ticket/38583">#38583</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestcontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestsettingscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemasanitizationphp">trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemavalidationphp">trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestsettingscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-settings-controller.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesrestapiendpointsclasswprestcontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php 2017-10-24 20:29:00 UTC (rev 41999)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php 2017-10-24 21:04:50 UTC (rev 42000)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -545,7 +545,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $endpoint_args[ $field_id ]['required'] = true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( array( 'type', 'format', 'enum', 'items', 'properties' ) as $schema_prop ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( array( 'type', 'format', 'enum', 'items', 'properties', 'additionalProperties' ) as $schema_prop ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $params[ $schema_prop ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestsettingscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php 2017-10-24 20:29:00 UTC (rev 41999)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php 2017-10-24 21:04:50 UTC (rev 42000)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -248,6 +248,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $rest_args['schema'] = $this->set_additional_properties_to_false( $rest_args['schema'] );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $rest_options[ $rest_args['name'] ] = $rest_args;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -301,4 +303,32 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> return rest_parse_request_arg( $value, $request, $param );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Recursively add additionalProperties = false to all objects in a schema.
+ *
+ * This is need to restrict properties of objects in settings values to only
+ * registered items, as the REST API will allow additional properties by
+ * default.
+ *
+ * @since 4.9.0
+ *
+ * @param array $schema The schema array.
+ * @return array
+ */
+ protected function set_additional_properties_to_false( $schema ) {
+ switch ( $schema['type'] ) {
+ case 'object':
+ foreach ( $schema['properties'] as $key => $child_schema ) {
+ $schema['properties'][ $key ] = $this->set_additional_properties_to_false( $child_schema );
+ }
+ $schema['additionalProperties'] = false;
+ break;
+ case 'array':
+ $schema['items'] = $this->set_additional_properties_to_false( $schema['items'] );
+ break;
+ }
+
+ return $schema;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api.php 2017-10-24 20:29:00 UTC (rev 41999)
+++ trunk/src/wp-includes/rest-api.php 2017-10-24 21:04:50 UTC (rev 42000)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1106,14 +1106,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $value as $property => $v ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! isset( $args['properties'][ $property ] ) ) {
- continue;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $args['properties'][ $property ] ) ) {
+ $is_valid = rest_validate_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' );
+ if ( is_wp_error( $is_valid ) ) {
+ return $is_valid;
+ }
+ } elseif ( isset( $args['additionalProperties'] ) && false === $args['additionalProperties'] ) {
+ return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not a valid property of Object.' ), $property ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $is_valid = rest_validate_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' );
-
- if ( is_wp_error( $is_valid ) ) {
- return $is_valid;
- }
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1246,11 +1246,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $value as $property => $v ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! isset( $args['properties'][ $property ] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $args['properties'][ $property ] ) ) {
+ $value[ $property ] = rest_sanitize_value_from_schema( $v, $args['properties'][ $property ] );
+ } elseif ( isset( $args['additionalProperties'] ) && false === $args['additionalProperties'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> unset( $value[ $property ] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- continue;
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $value[ $property ] = rest_sanitize_value_from_schema( $v, $args['properties'][ $property ] );
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $value;
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestschemasanitizationphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php 2017-10-24 20:29:00 UTC (rev 41999)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php 2017-10-24 21:04:50 UTC (rev 42000)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -157,8 +157,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( array( 'a' => 1 ), rest_sanitize_value_from_schema( array( 'a' => 1 ), $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( array( 'a' => 1 ), rest_sanitize_value_from_schema( array( 'a' => '1' ), $schema ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( array( 'a' => 1, 'b' => 1 ), rest_sanitize_value_from_schema( array( 'a' => '1', 'b' => 1 ), $schema ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_type_object_strips_additional_properties() {
+ $schema = array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'a' => array(
+ 'type' => 'number',
+ ),
+ ),
+ 'additionalProperties' => false,
+ );
+ $this->assertEquals( array( 'a' => 1 ), rest_sanitize_value_from_schema( array( 'a' => 1 ), $schema ) );
+ $this->assertEquals( array( 'a' => 1 ), rest_sanitize_value_from_schema( array( 'a' => '1' ), $schema ) );
+ $this->assertEquals( array( 'a' => 1 ), rest_sanitize_value_from_schema( array( 'a' => '1', 'b' => 1 ), $schema ) );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_type_object_nested() {
</span><span class="cx" style="display: block; padding: 0 10px"> $schema = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'object',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -195,7 +211,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'a' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'b' => 1,
</span><span class="cx" style="display: block; padding: 0 10px"> 'c' => 3,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'd' => '1',
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'b' => 1,
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> rest_sanitize_value_from_schema(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestschemavalidationphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php 2017-10-24 20:29:00 UTC (rev 41999)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php 2017-10-24 21:04:50 UTC (rev 42000)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -186,14 +186,29 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'object',
</span><span class="cx" style="display: block; padding: 0 10px"> 'properties' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'a' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'type' => 'number'
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'type' => 'number',
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( rest_validate_value_from_schema( array( 'a' => 1 ), $schema ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertTrue( rest_validate_value_from_schema( array( 'a' => 1, 'b' => 2 ), $schema ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertWPError( rest_validate_value_from_schema( array( 'a' => 'invalid' ), $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_type_object_additional_properties_false() {
+ $schema = array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'a' => array(
+ 'type' => 'number',
+ ),
+ ),
+ 'additionalProperties' => false,
+ );
+ $this->assertTrue( rest_validate_value_from_schema( array( 'a' => 1 ), $schema ) );
+ $this->assertWPError( rest_validate_value_from_schema( array( 'a' => 1, 'b' => 2 ), $schema ) );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_type_object_nested() {
</span><span class="cx" style="display: block; padding: 0 10px"> $schema = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'object',
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestsettingscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-settings-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-settings-controller.php 2017-10-24 20:29:00 UTC (rev 41999)
+++ trunk/tests/phpunit/tests/rest-api/rest-settings-controller.php 2017-10-24 21:04:50 UTC (rev 42000)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -190,6 +190,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'object',
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // We have to re-register the route, as the args changes based off registered settings.
+ $this->server->override_by_default = true;
+ $this->endpoint->register_routes();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Object is cast to correct types.
</span><span class="cx" style="display: block; padding: 0 10px"> update_option( 'mycustomsetting', array( 'a' => '1' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -209,7 +213,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = $this->server->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( array( 'a' => 1 ), $data['mycustomsetting'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( null, $data['mycustomsetting'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> unregister_setting( 'somegroup', 'mycustomsetting' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -372,6 +376,37 @@
</span><span class="cx" style="display: block; padding: 0 10px"> unregister_setting( 'somegroup', 'mycustomsetting' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_update_item_with_nested_object() {
+ register_setting( 'somegroup', 'mycustomsetting', array(
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'a' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'b' => array(
+ 'type' => 'number',
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ 'type' => 'object',
+ ) );
+
+ // We have to re-register the route, as the args changes based off registered settings.
+ $this->server->override_by_default = true;
+ $this->endpoint->register_routes();
+ wp_set_current_user( self::$administrator );
+
+ $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
+ $request->set_param( 'mycustomsetting', array( 'a' => array( 'b' => 1, 'c' => 1 ) ) );
+ $response = $this->server->dispatch( $request );
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_update_item_with_object() {
</span><span class="cx" style="display: block; padding: 0 10px"> register_setting( 'somegroup', 'mycustomsetting', array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'show_in_rest' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -407,6 +442,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( array(), $data['mycustomsetting'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( array(), get_option( 'mycustomsetting' ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Provide more keys.
+ $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
+ $request->set_param( 'mycustomsetting', array( 'a' => 1, 'b' => 2 ) );
+ $response = $this->server->dispatch( $request );
+
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Setting an invalid object.
</span><span class="cx" style="display: block; padding: 0 10px"> $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
</span><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'mycustomsetting', array( 'a' => 'invalid' ) );
</span></span></pre>
</div>
</div>
</body>
</html>