Make WordPress Core

Opened 9 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: 7.0 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch has-unit-tests changes-requested
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 (6)

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


9 months ago
#1

  • Keywords has-patch added; needs-patch removed

Trac ticket:

#2 @abcd95
9 months ago

  • Keywords needs-unit-tests removed

#3 @abcd95
9 months ago

  • Keywords has-unit-tests added

This ticket was mentioned in Slack in #core by wildworks. View the logs.


2 months ago

#5 @kirasong
2 months ago

  • Keywords changes-requested added

Reviewed this ticket during triage with @wildworks.

PR/Patch applies cleanly, but there was feedback left by @flixos90 on the PR that hasn't been responded to yet, so adding changes-requested to make the status of the ticket a bit clearer.

#6 @wildworks
7 weeks ago

  • Milestone changed from 6.9 to 7.0

Since the 6.9 RC1 release is coming soon, I will punt this ticket to 7.0.

Note: See TracTickets for help on using tickets.