<!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>[46249] trunk: REST API: Pass "null" as the post date property to reset post to initial "floating" date value.</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 { white-space: pre-line; 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/46249">46249</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/46249","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>kadamwhite</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-09-23 17:24:58 +0000 (Mon, 23 Sep 2019)</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: Pass "null" as the post date property to reset post to initial "floating" date value.

Props TimothyBlynJacobs, adamsilverstein, jnylen0, mnelson4.
Fixes <a href="https://core.trac.wordpress.org/ticket/44975">#44975</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiclasswprestrequestphp">trunk/src/wp-includes/rest-api/class-wp-rest-request.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestpostscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-posts-controller.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="#trunktestsqunitfixtureswpapigeneratedjs">trunk/tests/qunit/fixtures/wp-api-generated.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesrestapiclasswprestrequestphp"></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/class-wp-rest-request.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/class-wp-rest-request.php  2019-09-23 16:50:17 UTC (rev 46248)
+++ trunk/src/wp-includes/rest-api/class-wp-rest-request.php    2019-09-23 17:24:58 UTC (rev 46249)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -398,6 +398,30 @@
</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">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Checks if a parameter exists in the request.
+        *
+        * This allows distinguishing between an omitted parameter,
+        * and a parameter specifically set to null.
+        *
+        * @since 5.3.0
+        *
+        * @param string $key Parameter name.
+        *
+        * @return bool True if a param exists for the given key.
+        */
+       public function has_param( $key ) {
+               $order = $this->get_parameter_order();
+
+               foreach ( $order as $type ) {
+                       if ( array_key_exists( $key, $this->params[ $type ] ) ) {
+                               return true;
+                       }
+               }
+
+               return false;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Sets a parameter on the request.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.4.0
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp"></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-posts-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-posts-controller.php       2019-09-23 16:50:17 UTC (rev 46248)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2019-09-23 17:24:58 UTC (rev 46249)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1036,6 +1036,16 @@
</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"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Sending a null date or date_gmt value resets date and date_gmt to their
+               // default values (`0000-00-00 00:00:00`).
+               if (
+                       ( ! empty( $schema['properties']['date_gmt'] ) && $request->has_param( 'date_gmt' ) && null === $request['date_gmt'] ) ||
+                       ( ! empty( $schema['properties']['date'] ) && $request->has_param( 'date' ) && null === $request['date'] )
+               ) {
+                       $prepared_post->post_date_gmt = null;
+                       $prepared_post->post_date     = null;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Post slug.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $schema['properties']['slug'] ) && isset( $request['slug'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $prepared_post->post_name = $request['slug'];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1891,13 +1901,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'properties' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'date'         => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'description' => __( "The date the object was published, in the site's timezone." ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'type'        => 'string',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 'type'        => array( 'string', 'null' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'format'      => 'date-time',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'view', 'edit', 'embed' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><span class="cx" style="display: block; padding: 0 10px">                                'date_gmt'     => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'description' => __( 'The date the object was published, as GMT.' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'type'        => 'string',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 'type'        => array( 'string', 'null' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'format'      => 'date-time',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'view', 'edit' ),
</span><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        2019-09-23 16:50:17 UTC (rev 46248)
+++ trunk/src/wp-includes/rest-api.php  2019-09-23 17:24:58 UTC (rev 46249)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1209,6 +1209,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @return true|WP_Error
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function rest_validate_value_from_schema( $value, $args, $param = '' ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( is_array( $args['type'] ) ) {
+               foreach ( $args['type'] as $type ) {
+                       $type_args         = $args;
+                       $type_args['type'] = $type;
+
+                       if ( true === rest_validate_value_from_schema( $value, $type_args, $param ) ) {
+                               return true;
+                       }
+               }
+
+               /* translators: 1: Parameter, 2: List of types. */
+               return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s' ), $param, implode( ',', $args['type'] ) ) );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( 'array' === $args['type'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! is_null( $value ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $value = wp_parse_list( $value );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1261,6 +1275,15 @@
</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"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( 'null' === $args['type'] ) {
+               if ( null !== $value ) {
+                       /* translators: 1: Parameter, 2: Type name. */
+                       return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, 'null' ) );
+               }
+
+               return true;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( ! empty( $args['enum'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! in_array( $value, $args['enum'], true ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        /* translators: 1: Parameter, 2: List of valid values. */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1365,6 +1388,27 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @return true|WP_Error
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function rest_sanitize_value_from_schema( $value, $args ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( is_array( $args['type'] ) ) {
+               // Determine which type the value was validated against, and use that type when performing sanitization
+               $validated_type = '';
+
+               foreach ( $args['type'] as $type ) {
+                       $type_args         = $args;
+                       $type_args['type'] = $type;
+
+                       if ( ! is_wp_error( rest_validate_value_from_schema( $value, $type_args ) ) ) {
+                               $validated_type = $type;
+                               break;
+                       }
+               }
+
+               if ( ! $validated_type ) {
+                       return null;
+               }
+
+               $args['type'] = $validated_type;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( 'array' === $args['type'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( empty( $args['items'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return (array) $value;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1407,6 +1451,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return $value;
</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">+        if ( 'null' === $args['type'] ) {
+               return null;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( 'integer' === $args['type'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return (int) $value;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestpostscontrollerphp"></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-posts-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-posts-controller.php      2019-09-23 16:50:17 UTC (rev 46248)
+++ trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php        2019-09-23 17:24:58 UTC (rev 46249)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2625,6 +2625,66 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $params['excerpt'], $post->post_excerpt );
</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">+        /**
+        * Verify that updating a post with a `null` date or date_gmt results in a reset post, where all
+        * date values are equal (date, date_gmt, date_modified and date_modofied_gmt) in the API response.
+        * In the database, the post_date_gmt field is reset to the default `0000-00-00 00:00:00`.
+        *
+        * @ticket 44975
+        */
+       public function test_rest_update_post_with_empty_date() {
+               // Create a new test post.
+               $post_id = $this->factory->post->create();
+               wp_set_current_user( self::$editor_id );
+
+               // Set the post date to the future.
+               $future_date = '2919-07-29T18:00:00';
+               $request     = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
+               $request->add_header( 'content-type', 'application/json' );
+               $params = $this->set_post_data(
+                       array(
+                               'date_gmt' => $future_date,
+                               'date'     => $future_date,
+                               'title'    => 'update',
+                               'status'   => 'draft',
+                       )
+               );
+               $request->set_body( wp_json_encode( $params ) );
+               $response = rest_get_server()->dispatch( $request );
+               $this->check_update_post_response( $response );
+               $new_data = $response->get_data();
+
+               // Verify the post is set to the future date.
+               $this->assertEquals( $new_data['date_gmt'], $future_date );
+               $this->assertEquals( $new_data['date'], $future_date );
+               $this->assertNotEquals( $new_data['date_gmt'], $new_data['modified_gmt'] );
+               $this->assertNotEquals( $new_data['date'], $new_data['modified'] );
+
+               // Update post with a blank field (date or date_gmt).
+               $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
+               $request->add_header( 'content-type', 'application/json' );
+               $params = $this->set_post_data(
+                       array(
+                               'date_gmt' => null,
+                               'title'    => 'test',
+                               'status'   => 'draft',
+                       )
+               );
+               $request->set_body( wp_json_encode( $params ) );
+               $response = rest_get_server()->dispatch( $request );
+
+               // Verify the date field values are reset in the API response.
+               $this->check_update_post_response( $response );
+               $new_data = $response->get_data();
+               $this->assertEquals( $new_data['date_gmt'], $new_data['date'] );
+               $this->assertNotEquals( $new_data['date_gmt'], $future_date );
+
+               $post = get_post( $post_id, 'ARRAY_A' );
+               $this->assertEquals( $post['post_date_gmt'], '0000-00-00 00:00:00' );
+               $this->assertNotEquals( $new_data['date_gmt'], $future_date );
+               $this->assertNotEquals( $new_data['date'], $future_date );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_rest_update_post_raw() {
</span><span class="cx" style="display: block; padding: 0 10px">                wp_set_current_user( self::$editor_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</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   2019-09-23 16:50:17 UTC (rev 46248)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php     2019-09-23 17:24:58 UTC (rev 46249)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -292,4 +292,59 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 1.10, rest_sanitize_value_from_schema( 1.10, $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) );
</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_nullable_date() {
+               $schema = array(
+                       'type'   => array( 'string', 'null' ),
+                       'format' => 'date-time',
+               );
+
+               $this->assertNull( rest_sanitize_value_from_schema( null, $schema ) );
+               $this->assertEquals( '2019-09-19T18:00:00', rest_sanitize_value_from_schema( '2019-09-19T18:00:00', $schema ) );
+               $this->assertNull( rest_sanitize_value_from_schema( 'lalala', $schema ) );
+       }
+
+       public function test_object_or_string() {
+               $schema = array(
+                       'type'       => array( 'object', 'string' ),
+                       'properties' => array(
+                               'raw' => array(
+                                       'type' => 'string',
+                               ),
+                       ),
+               );
+
+               $this->assertEquals( 'My Value', rest_sanitize_value_from_schema( 'My Value', $schema ) );
+               $this->assertEquals( array( 'raw' => 'My Value' ), rest_sanitize_value_from_schema( array( 'raw' => 'My Value' ), $schema ) );
+               $this->assertNull( rest_sanitize_value_from_schema( array( 'raw' => 1 ), $schema ) );
+       }
+
+       public function test_object_or_bool() {
+               $schema = array(
+                       'type'       => array( 'object', 'boolean' ),
+                       'properties' => array(
+                               'raw' => array(
+                                       'type' => 'boolean',
+                               ),
+                       ),
+               );
+
+               $this->assertTrue( rest_sanitize_value_from_schema( true, $schema ) );
+               $this->assertTrue( rest_sanitize_value_from_schema( '1', $schema ) );
+               $this->assertTrue( rest_sanitize_value_from_schema( 1, $schema ) );
+
+               $this->assertFalse( rest_sanitize_value_from_schema( false, $schema ) );
+               $this->assertFalse( rest_sanitize_value_from_schema( '0', $schema ) );
+               $this->assertFalse( rest_sanitize_value_from_schema( 0, $schema ) );
+
+               $this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => true ), $schema ) );
+               $this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => '1' ), $schema ) );
+               $this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => 1 ), $schema ) );
+
+               $this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => false ), $schema ) );
+               $this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => '0' ), $schema ) );
+               $this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => 0 ), $schema ) );
+
+               $this->assertNull( rest_sanitize_value_from_schema( array( 'raw' => 'something non boolean' ), $schema ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</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     2019-09-23 16:50:17 UTC (rev 46248)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php       2019-09-23 17:24:58 UTC (rev 46249)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -296,4 +296,36 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( rest_validate_value_from_schema( 1, $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( rest_validate_value_from_schema( array(), $schema ) );
</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_null() {
+               $this->assertTrue( rest_validate_value_from_schema( null, array( 'type' => 'null' ) ) );
+               $this->assertWPError( rest_validate_value_from_schema( '', array( 'type' => 'null' ) ) );
+               $this->assertWPError( rest_validate_value_from_schema( 'null', array( 'type' => 'null' ) ) );
+       }
+
+       public function test_nullable_date() {
+               $schema = array(
+                       'type'   => array( 'string', 'null' ),
+                       'format' => 'date-time',
+               );
+
+               $this->assertTrue( rest_validate_value_from_schema( null, $schema ) );
+               $this->assertTrue( rest_validate_value_from_schema( '2019-09-19T18:00:00', $schema ) );
+               $this->assertWPError( rest_validate_value_from_schema( 'some random string', $schema ) );
+       }
+
+       public function test_object_or_string() {
+               $schema = array(
+                       'type'       => array( 'object', 'string' ),
+                       'properties' => array(
+                               'raw' => array(
+                                       'type' => 'string',
+                               ),
+                       ),
+               );
+
+               $this->assertTrue( rest_validate_value_from_schema( 'My Value', $schema ) );
+               $this->assertTrue( rest_validate_value_from_schema( array( 'raw' => 'My Value' ), $schema ) );
+               $this->assertWPError( rest_validate_value_from_schema( array( 'raw' => array( 'a list' ) ), $schema ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsqunitfixtureswpapigeneratedjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/qunit/fixtures/wp-api-generated.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/fixtures/wp-api-generated.js    2019-09-23 16:50:17 UTC (rev 46248)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js      2019-09-23 17:24:58 UTC (rev 46249)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -373,12 +373,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         "date": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, in the site's timezone.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "date_gmt": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, as GMT.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "slug": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -553,12 +559,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         "date": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, in the site's timezone.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "date_gmt": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, as GMT.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "slug": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -893,12 +905,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         "date": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, in the site's timezone.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "date_gmt": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, as GMT.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "slug": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1238,12 +1256,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         "date": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, in the site's timezone.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "date_gmt": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, as GMT.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "slug": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1390,12 +1414,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         "date": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, in the site's timezone.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "date_gmt": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, as GMT.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "slug": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1702,12 +1732,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         "date": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, in the site's timezone.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "date_gmt": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, as GMT.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "slug": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2015,12 +2051,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         "date": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, in the site's timezone.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "date_gmt": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, as GMT.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "slug": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2152,12 +2194,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         "date": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, in the site's timezone.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "date_gmt": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, as GMT.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "slug": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2399,12 +2447,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         "date": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, in the site's timezone.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "date_gmt": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, as GMT.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "slug": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2503,12 +2557,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         "date": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, in the site's timezone.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "date_gmt": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, as GMT.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "slug": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2612,12 +2672,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         "date": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, in the site's timezone.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "date_gmt": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span><span class="cx" style="display: block; padding: 0 10px">                             "description": "The date the object was published, as GMT.",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                            "type": "string"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                            "type": [
+                                "string",
+                                "null"
+                            ]
</ins><span class="cx" style="display: block; padding: 0 10px">                         },
</span><span class="cx" style="display: block; padding: 0 10px">                         "slug": {
</span><span class="cx" style="display: block; padding: 0 10px">                             "required": false,
</span></span></pre>
</div>
</div>

</body>
</html>