Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 0 additions & 74 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3235,80 +3235,6 @@ function rest_default_additional_properties_to_false( $schema ) {
return $schema;
}

/**
* Recursively casts empty arrays to objects where the schema types them as objects.
*
* PHP cannot distinguish an empty associative array from an empty list, so
* `json_encode()` always serializes `array()` as a JSON array (`[]`). A REST
* schema, however, may type a value as an object, which must encode as `{}`.
* This walks the value against its schema and casts any empty, object-typed
* array to an object. Non-empty associative arrays already encode as objects,
* so they are left as arrays and only recursed into to fix any nested empty
* objects.
*
* Union schemas (`oneOf`/`anyOf`) are handled only for the empty-array case:
* an empty value is cast to an object when any branch allows an object. Such
* values are not recursed into.
*
* @since 7.2.0
* @access private
*
* @param mixed $value The value to normalize.
* @param array $schema The schema node describing the value.
* @return mixed The normalized value, with empty object-typed arrays cast to objects.
*/
function _rest_cast_empty_objects_from_schema( $value, $schema ) {
if ( ! is_array( $value ) || ! is_array( $schema ) ) {
return $value;
}

if ( isset( $schema['oneOf'] ) || isset( $schema['anyOf'] ) ) {
$branches = $schema['oneOf'] ?? $schema['anyOf'];
if ( array() === $value ) {
foreach ( $branches as $branch ) {
if ( is_array( $branch ) && in_array( 'object', (array) ( $branch['type'] ?? array() ), true ) ) {
return (object) array();
}
}
}
return $value;
}

$types = (array) ( $schema['type'] ?? array() );

if ( in_array( 'array', $types, true ) && isset( $schema['items'] ) ) {
foreach ( $value as $index => $item ) {
$value[ $index ] = _rest_cast_empty_objects_from_schema( $item, $schema['items'] );
}
return $value;
}

if ( in_array( 'object', $types, true ) ) {
if ( isset( $schema['properties'] ) ) {
foreach ( $schema['properties'] as $property => $property_schema ) {
if ( array_key_exists( $property, $value ) ) {
$value[ $property ] = _rest_cast_empty_objects_from_schema( $value[ $property ], $property_schema );
}
}
}
if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
foreach ( $value as $key => $item ) {
if ( isset( $schema['properties'][ $key ] ) ) {
continue;
}
$value[ $key ] = _rest_cast_empty_objects_from_schema( $item, $schema['additionalProperties'] );
}
}

// Empty object-typed arrays must serialize as {} to match the schema.
if ( array() === $value ) {
return (object) array();
}
}

return $value;
}

/**
* Gets the REST API route for a post.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function get_items( $request ) {
$field_schema = $this->get_field_schema();
$fields = array();
foreach ( wp_get_registered_entity_fields( $kind, $name ) as $field ) {
$fields[] = _rest_cast_empty_objects_from_schema( $field, $field_schema );
$fields[] = wp_prepare_json_value_for_client( $field, $field_schema );
}

$script_modules = array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ public function get_items( $request ) {
'kind' => $kind,
'name' => $name,
'version' => WP_View_Config_Data::LATEST_VERSION,
'default_view' => _rest_cast_empty_objects_from_schema( $config['default_view'], $schema['properties']['default_view'] ),
'default_layouts' => _rest_cast_empty_objects_from_schema( $config['default_layouts'], $schema['properties']['default_layouts'] ),
'view_list' => _rest_cast_empty_objects_from_schema( $config['view_list'], $schema['properties']['view_list'] ),
'form' => _rest_cast_empty_objects_from_schema( $config['form'], $schema['properties']['form'] ),
'default_view' => wp_prepare_json_value_for_client( $config['default_view'], $schema['properties']['default_view'] ),
'default_layouts' => wp_prepare_json_value_for_client( $config['default_layouts'], $schema['properties']['default_layouts'] ),
'view_list' => wp_prepare_json_value_for_client( $config['view_list'], $schema['properties']['view_list'] ),
'form' => wp_prepare_json_value_for_client( $config['form'], $schema['properties']['form'] ),
);

return rest_ensure_response( $response );
Expand All @@ -169,16 +169,16 @@ public function get_items( $request ) {
* objects.
*
* @since 7.1.0
* @since 7.2.0 Moved the logic to _rest_cast_empty_objects_from_schema().
* @since 7.2.0 Uses wp_prepare_json_value_for_client().
*
* @see _rest_cast_empty_objects_from_schema()
* @see wp_prepare_json_value_for_client()
*
* @param mixed $value The value to normalize.
* @param array $schema The schema node describing the value.
* @return mixed The normalized value, with empty object-typed arrays cast to objects.
*/
protected function cast_empty_objects( $value, $schema ) {
return _rest_cast_empty_objects_from_schema( $value, $schema );
return is_array( $schema ) ? wp_prepare_json_value_for_client( $value, $schema ) : $value;
}

/**
Expand Down
163 changes: 0 additions & 163 deletions tests/phpunit/tests/rest-api/restCastEmptyObjectsFromSchema.php

This file was deleted.

Loading