Skip to content

Commit a73ffcb

Browse files
committed
REST API: Allow-list ability schema response keywords.
1 parent b226691 commit a73ffcb

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,21 @@ public function get_item_permissions_check( $request ) {
189189
}
190190

191191
/**
192-
* WordPress-internal schema keywords to strip from REST responses.
192+
* Additional schema keywords to preserve in REST responses.
193193
*
194-
* @since 7.0.0
195-
* @var array<string, true>
194+
* These are not included in rest_get_allowed_schema_keywords(), but are
195+
* still recognized as schema traversal locations for ability schemas.
196+
*
197+
* @since 7.1.0
198+
* @var string[]
196199
*/
197-
private const INTERNAL_SCHEMA_KEYWORDS = array(
198-
'sanitize_callback' => true,
199-
'validate_callback' => true,
200-
'arg_options' => true,
200+
private const ADDITIONAL_ALLOWED_SCHEMA_KEYWORDS = array(
201+
'required',
202+
'allOf',
203+
'not',
204+
'definitions',
205+
'dependencies',
206+
'additionalItems',
201207
);
202208

203209
/**
@@ -217,12 +223,11 @@ private function is_associative_array( $value ): bool {
217223
/**
218224
* Transforms an ability schema for REST response output.
219225
*
220-
* Ability schemas may include WordPress-internal properties like
221-
* `sanitize_callback`, `validate_callback`, and `arg_options` that are
222-
* used server-side but are not valid JSON Schema keywords. This method
223-
* removes those specific keys so they are not exposed in REST responses.
224-
* It also converts empty array defaults to objects when the schema type is
225-
* 'object' to ensure proper JSON serialization as {} instead of [].
226+
* Ability schemas may include WordPress-internal properties or unsupported
227+
* schema keywords that should not be exposed in REST responses. This method
228+
* strips keys not recognized by the REST API schema handling. It also
229+
* converts empty array defaults to objects when the schema type is 'object'
230+
* to ensure proper JSON serialization as {} instead of [].
226231
*
227232
* @since 7.1.0
228233
*
@@ -237,7 +242,16 @@ private function prepare_schema_for_response( array $schema ): array {
237242
}
238243
}
239244

240-
$schema = array_diff_key( $schema, self::INTERNAL_SCHEMA_KEYWORDS );
245+
$schema = array_intersect_key(
246+
$schema,
247+
array_fill_keys(
248+
array_merge(
249+
rest_get_allowed_schema_keywords(),
250+
self::ADDITIONAL_ALLOWED_SCHEMA_KEYWORDS
251+
),
252+
true
253+
)
254+
);
241255

242256
// Sub-schema maps: keys are user-defined, values are sub-schemas.
243257
// Note: 'dependencies' values can also be property-dependency arrays

tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ public function test_internal_schema_keywords_stripped_from_response(): void {
846846
'content' => array(
847847
'type' => 'string',
848848
'description' => 'The content value.',
849+
'examples' => array( 'example content' ),
849850
'sanitize_callback' => 'sanitize_text_field',
850851
'validate_callback' => 'is_string',
851852
'arg_options' => array( 'sanitize_callback' => 'wp_kses_post' ),
@@ -880,6 +881,7 @@ public function test_internal_schema_keywords_stripped_from_response(): void {
880881
$this->assertArrayNotHasKey( 'sanitize_callback', $content_schema );
881882
$this->assertArrayNotHasKey( 'validate_callback', $content_schema );
882883
$this->assertArrayNotHasKey( 'arg_options', $content_schema );
884+
$this->assertArrayNotHasKey( 'examples', $content_schema );
883885

884886
// Verify valid JSON Schema keywords are preserved.
885887
$this->assertSame( 'string', $content_schema['type'] );

0 commit comments

Comments
 (0)