REST API: Preserve unknown, respect null in server-side block rendering.

- Skips validation where there is no attribute definition, but keeps the attribute value. Previously, the attribute would be omitted from the attributes passed to `render_callback`. Notably, this resolves an issue where `render_callback` cannot receive a block's `align` and `customClassName` attribute values, since these are defined as a client-side filter.
- Validates `null` as a proper value in its own right. Previously, a client implementation of a block could track `{“attribute":null}` as an explicitly empty value, and the server would wrongly initiate defaulting behavior. The new behavior will now only populate a default value if the attribute is not defined at all, including when unset in its being invalid per the attribute schema. 

Props aduth, noisysocks, youknowriad, danielbachhuber.

Merges [43918] to trunk.

See #45145 for the patch, #45098 for the original ticket.

git-svn-id: https://develop.svn.wordpress.org/trunk@44269 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonathan Desrosiers
2018-12-17 17:59:44 +00:00
parent abc29f4cef
commit 2ae5bb324d
5 changed files with 59 additions and 19 deletions

View File

@@ -168,7 +168,8 @@ class WP_Test_Block_Type extends WP_UnitTestCase {
'wrongType' => 5,
'wrongTypeDefaulted' => 5,
/* missingDefaulted */
'undefined' => 'omit',
'undefined' => 'include',
'intendedNull' => null,
);
$block_type = new WP_Block_Type(
@@ -189,6 +190,10 @@ class WP_Test_Block_Type extends WP_UnitTestCase {
'type' => 'string',
'default' => 'define',
),
'intendedNull' => array(
'type' => array( 'string', 'null' ),
'default' => 'wrong',
),
),
)
);
@@ -198,14 +203,29 @@ class WP_Test_Block_Type extends WP_UnitTestCase {
$this->assertEquals(
array(
'correct' => 'include',
'wrongType' => null,
/* wrongType */
'wrongTypeDefaulted' => 'defaulted',
'missingDefaulted' => 'define',
'undefined' => 'include',
'intendedNull' => null,
),
$prepared_attributes
);
}
/**
* @ticket 45145
*/
function test_prepare_attributes_none_defined() {
$attributes = array( 'exists' => 'keep' );
$block_type = new WP_Block_Type( 'core/dummy', array() );
$prepared_attributes = $block_type->prepare_attributes_for_render( $attributes );
$this->assertEquals( $attributes, $prepared_attributes );
}
/**
* @ticket 45097
*/

View File

@@ -319,7 +319,9 @@ class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( self::$block_name );
$defaults = array();
foreach ( $block_type->attributes as $key => $attribute ) {
$defaults[ $key ] = isset( $attribute['default'] ) ? $attribute['default'] : null;
if ( isset( $attribute['default'] ) ) {
$defaults[ $key ] = $attribute['default'];
}
}
$request = new WP_REST_Request( 'GET', self::$rest_api_route . self::$block_name );