Skip to content

Commit ad15c65

Browse files
apply feedback
1 parent fb4c1d0 commit ad15c65

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/wp-includes/abilities.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ function wp_register_core_abilities(): void {
100100
),
101101
),
102102
'additionalProperties' => false,
103-
// Casting to object so when it is serilized to JSON it shows as {} instead of [].
104-
'default' => (object) array(),
103+
'default' => array(),
105104
),
106105
'output_schema' => array(
107106
'type' => 'object',

src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,21 @@ public function prepare_item_for_response( $ability, $request ) {
214214
'meta' => $ability->get_meta(),
215215
);
216216

217+
// Convert top-level defaults to an object when its type is object and
218+
// they are an empty array so they get serialized as {} instead of [].
219+
if ( isset( $data['input_schema']['type'] ) && 'object' === $data['input_schema']['type'] && isset( $data['input_schema']['default'] ) ) {
220+
$default = $data['input_schema']['default'];
221+
if ( is_array( $default ) && empty( $default ) ) {
222+
$data['input_schema']['default'] = (object) $default;
223+
}
224+
}
225+
if ( isset( $data['output_schema']['type'] ) && 'object' === $data['output_schema']['type'] && isset( $data['output_schema']['default'] ) ) {
226+
$default = $data['output_schema']['default'];
227+
if ( is_array( $default ) && empty( $default ) ) {
228+
$data['output_schema']['default'] = (object) $default;
229+
}
230+
}
231+
217232
$context = $request['context'] ?? 'view';
218233
$data = $this->add_additional_fields_to_object( $data, $request );
219234
$data = $this->filter_response_by_context( $data, $context );

0 commit comments

Comments
 (0)