Make WordPress Core

Opened 2 months ago

Last modified 7 weeks ago

#63186 assigned defect (bug)

Empty `properties` PHP array in REST API JSON schema leads to invalid JSON schema output

Reported by: flixos90's profile flixos90 Owned by: flixos90's profile flixos90
Milestone: 6.9 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

When a REST API schema in PHP uses 'properties' => array(), in JSON it becomes "properties": [], which is invalid JSON schema, as properties must always be an object.

The problem is that an empty array in PHP becomes [] when JSON-encoded, it doesn't make a difference whether it's supposed to be a regular (indexed) array (should be [] in JS/JSON) or an associative array (should be {} in JS/JSON).

The REST API should automatically sanitize empty arrays under the properties key to become empty objects (e.g. via new stdClass()) so that the output is valid JSON schema.

Worth noting: In most cases, the properties key won't be empty, as an object should always contain _something_ in its definition. But there are scenarios where any properties are allowed (via 'additionalProperties' => true), and in that situation it's totally reasonable to set properties to be empty.

Related to #54484, but with different implications: The other ticket is about REST response data being invalid based on a schema, while this ticket is about the schema itself being invalid.

Change History (3)

This ticket was mentioned in PR #8608 on WordPress/wordpress-develop by @abcd95.


2 months ago
#1

  • Keywords has-patch added; needs-patch removed

Trac ticket:

#2 @abcd95
7 weeks ago

  • Keywords needs-unit-tests removed

#3 @abcd95
7 weeks ago

  • Keywords has-unit-tests added
Note: See TracTickets for help on using tickets.