Skip to content
Merged
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
3 changes: 3 additions & 0 deletions backport-changelog/7.2/13911.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/13911

* https://github.com/WordPress/gutenberg/pull/84078
Original file line number Diff line number Diff line change
Expand Up @@ -142,88 +142,15 @@ public function get_items( $request ) {
'kind' => $kind,
'name' => $name,
'version' => Gutenberg_View_Config_Data::LATEST_VERSION,
'default_view' => $this->cast_empty_objects( $config['default_view'], $schema['properties']['default_view'] ),
'default_layouts' => $this->cast_empty_objects( $config['default_layouts'], $schema['properties']['default_layouts'] ),
'view_list' => $this->cast_empty_objects( $config['view_list'], $schema['properties']['view_list'] ),
'form' => $this->cast_empty_objects( $config['form'], $schema['properties']['form'] ),
'default_view' => gutenberg_rest_cast_empty_objects_from_schema( $config['default_view'], $schema['properties']['default_view'] ),
'default_layouts' => gutenberg_rest_cast_empty_objects_from_schema( $config['default_layouts'], $schema['properties']['default_layouts'] ),
'view_list' => gutenberg_rest_cast_empty_objects_from_schema( $config['view_list'], $schema['properties']['view_list'] ),
'form' => gutenberg_rest_cast_empty_objects_from_schema( $config['form'], $schema['properties']['form'] ),
);

return rest_ensure_response( $response );
}

/**
* 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 (`[]`). The
* REST schema, however, types several values as objects, 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, which is sufficient for the form schema
* where they never contain empty nested objects.
*
* @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 ) {
if ( ! is_array( $value ) || ! is_array( $schema ) ) {
return $value;
}

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

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

if ( in_array( 'array', $types, true ) && isset( $schema['items'] ) ) {
foreach ( $value as $index => $item ) {
$value[ $index ] = $this->cast_empty_objects( $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 ] = $this->cast_empty_objects( $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 ] = $this->cast_empty_objects( $item, $schema['additionalProperties'] );
}
}

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

return $value;
}

/**
* Retrieves the item's schema, conforming to JSON Schema.
*
Expand Down
Loading
Loading