WordPress.org

Make WordPress Core

Opened 2 weeks ago

Last modified 5 days ago

#49147 new defect (bug)

Incorrect formatting of _links on OPTIONS request to embedded collections

Reported by: nsundberg Owned by:
Milestone: 5.4 Priority: normal
Severity: normal Version: 4.4
Component: REST API Keywords: good-first-bug has-patch needs-unit-tests
Focuses: Cc:
PR Number:

Description

When sending an OPTIONS request to a non-variable route (eg. /wp/v2/posts) with ?_embed=true and WP_DEBUG enabled the following warning appears (example from 5.3.2):

Warning: Invalid argument supplied for foreach() in /wp-includes/rest-api/class-wp-rest-server.php on line 569

This is caused by the following lines which sets the link as a string instead of an associative array with a href key, like the embed_links method expects: class-wp-rest-server.php:L1216-L1218

$data['_links'] = array(
  'self' => rest_url( $route ),
);

Expected:

$data['_links'] = array(
  'self' => array(
    'href' => rest_url( $route ),
  ),
);

Was about to submit a patch but when looking into it I got a bit unsure if the _links property even should be included in these responses. From what I can tell no other endpoints include the _links property in the response for OPTIONS requests, but please advise.

Attachments (2)

49147.diff (555 bytes) - added by nsundberg 2 weeks ago.
49147.2.diff (581 bytes) - added by nsundberg 13 days ago.

Download all attachments as: .zip

Change History (8)

#1 @TimothyBlynJacobs
2 weeks ago

  • Keywords needs-refresh good-first-bug added
  • Milestone changed from Awaiting Review to 5.4
  • Version changed from 4.5 to 4.4

Thanks for the ticket @nsundberg!

I think it'd be best to fix the link to be an associate array as you suggested. I think it'd be a good separate enhancement to add those self links to the OPTIONS response.

@nsundberg
2 weeks ago

#2 @nsundberg
2 weeks ago

  • Keywords has-patch added; needs-refresh removed

#3 @TimothyBlynJacobs
2 weeks ago

Thanks for the patch @nsundberg. There should be another layer of a wrapping array around the wrapping you added. In other words, each relation should be an array of links, where each link is an array with an href key.

For example the following JSON is expected.

{
  "_links": {
    "self": [
      {
        "href": "https://"
      }
    ]
  }
}

@nsundberg
13 days ago

#4 @nsundberg
13 days ago

Ah, sorry, didn't look close enough!

This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.


5 days ago

#6 @TimothyBlynJacobs
5 days ago

  • Keywords needs-unit-tests added

Thanks for the patch @nsundberg! Do you want to try tackling a unit test for this?

Note: See TracTickets for help on using tickets.