Make WordPress Core

Opened 20 months ago

Last modified 8 months ago

#63186 assigned defect (bug)

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

Reported by: flixos90 Owned by: flixos90
Priority: normal Milestone: Future Release
Component: REST API Version: 4.7
Severity: normal Keywords: has-patch has-unit-tests changes-requested
Cc: Focuses:

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 (9)

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


20 months ago
#1

  • Keywords has-patch added; needs-patch removed

Trac ticket:

#2 @abcd95
20 months ago

  • Keywords needs-unit-tests removed

#3 @abcd95
20 months ago

  • Keywords has-unit-tests added

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


13 months ago

#5 @kirasong
13 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
12 months ago

  • Milestone 6.9 → 7.0

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

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


8 months ago

#9 @audrasjb
8 months ago

  • Milestone 7.0 → Future Release

As per today's 7.0 pre-RC1 bug scrub:

The PR is still a draft/wip so I'm moving it to Future Release.

Note: See TracTickets for help on using tickets.