#34729 closed task (blessed) (fixed)
Use short CURIEs instead of full URIs
Reported by: | rmccue | Owned by: | joehoyle |
---|---|---|---|
Milestone: | 4.5 | Priority: | normal |
Severity: | normal | Version: | |
Component: | REST API | Keywords: | needs-patch |
Focuses: | Cc: |
Description
Originally https://github.com/WP-API/WP-API/issues/1488
One small annoyance in the API right now is that link relations are full URIs. In JavaScript, this means that while you have _links.self
and _links.author
, you have to do _links["http://v2.wp-api.org/term"]
. This is a bit ugly, and can be confusing.
We should instead switch to [CURIEs](https://en.wikipedia.org/wiki/CURIE). These are an abbreviated form of the full URI that's easier to work with, but can be expanded out to the full URI if needed. They look like this:
{ "_links": { "curies": [ { "name": "wp", "href": "http://v2.wp-api.org/{rel}", "templated": true } ], "wp:term": [ { "taxonomy": "category", "href": "..." }, { "taxonomy": "post_tag", "href": "..." } ] } }
Attachments (7)
Change History (41)
#2
@
9 years ago
- Milestone changed from Awaiting Review to 4.5
- Owner set to danielbachhuber
- Status changed from new to assigned
#4
@
9 years ago
Added initial patch that outputs the correct response data, need to upload some tests here too.
#5
@
9 years ago
Added new patch that supports any type of pattern in the CURIE href attribute such as http://docs.something.com/{rel}.html
This ticket was mentioned in Slack in #core-restapi by joehoyle. View the logs.
9 years ago
#8
@
9 years ago
Couple of notes on the patch:
- Singular form should be "CURIE", not "CURI"
- Should return/continue early if possible
Otherwise, looking good.
This ticket was mentioned in Slack in #core-restapi by jnylen. View the logs.
9 years ago
#12
@
9 years ago
- Status changed from assigned to reviewing
@rmccue new patch with tests, returning early, typo fixes and also the ability to filter the CURIEs so plugins etc can add their own if they so wish.
This ticket was mentioned in Slack in #core-restapi by joehoyle. View the logs.
9 years ago
This ticket was mentioned in Slack in #core-restapi by rmccue. View the logs.
9 years ago
#17
follow-up:
↓ 20
@
9 years ago
- Keywords needs-patch added; has-patch 2nd-opinion removed
- Resolution fixed deleted
- Status changed from closed to reopened
The regex doesn't handle https://api.w.org/post_type
https://github.com/WP-API/WP-API/commit/ede21aa1ccea94ff8e4f8c30b23cae741e9cf776 is one approach, but I'm not sure if https://api.w.org/?foo
would be considered a valid curie and need to be handled too
This ticket was mentioned in Slack in #core by jorbin. View the logs.
9 years ago
#20
in reply to:
↑ 17
@
9 years ago
Replying to danielbachhuber:
The regex doesn't handle
https://api.w.org/post_type
https://github.com/WP-API/WP-API/commit/ede21aa1ccea94ff8e4f8c30b23cae741e9cf776 is one approach, but I'm not sure if
https://api.w.org/?foo
would be considered a valid curie and need to be handled too
This should just be .+
I think; https://example.com/a/b?c#d
is valid. We should also be anchoring to the start/end of the string though, I think.
This ticket was mentioned in Slack in #core by chriscct7. View the logs.
9 years ago
This ticket was mentioned in Slack in #core-restapi by joehoyle. View the logs.
9 years ago
#23
@
9 years ago
Ok, I took another stab at the approach. I think the above is a red herring (though I think we should laxen the regex either way.)
Most of the issue stem from the fact that we generate the curies at the server level, this means things like collection resources get curies applied at a difference stage to top level ones, which screws up our unit tests.
Instead, I've taken the approach of curies being abstracted in the WP_REST_Response class via get_links, so virtually any "exposed" link, even internally will be "curie-ified". This makes things a lot simpler, as you don't need to know whether a response has come in via the Server's serve_request
method, or via dispatch.
For backwards compatibility, this might present a challenge however, because we can't shim the get_links
method of the WP_REST_Response class for WordPress 4.4. That might not be a huge issue, and it's possible we could fudge it somehow at the server level (similar to what @danielbachhuber tried to do in https://github.com/WP-API/WP-API/pull/2295.
_Ideally_ we could subclass WP_REST_Response in the plugin for backwards compat, but we don't support filtering the Response class so that wouldn't be possible I think.
Looking to get this wrapped up today, will chat with @rmccue when he is online.
#24
@
9 years ago
Also, while we are changing this, it might be a good idea to add a filter to the get_links
in the WP_REST_Response class so we can provide future flexibility.
This ticket was mentioned in Slack in #core-restapi by joehoyle. View the logs.
9 years ago
This ticket was mentioned in Slack in #core by chriscct7. View the logs.
9 years ago
This ticket was mentioned in Slack in #core by mike. View the logs.
9 years ago
#29
@
9 years ago
Added patch with maximum backwards compatibility. This makes the method easier to Shim in the rest api and also provides a way to get the non-compact links just as before. If needed we could also add the ability for the client to specify if it wants curies or not. Also, if anything was internally using get_response_links
that method is still now available, unchanged.
This ticket was mentioned in Slack in #core-restapi by joehoyle. View the logs.
9 years ago
@
8 years ago
Make links array order stays the same and curies output not processed as part of the data
#32
follow-up:
↓ 33
@
8 years ago
Items in a collection don't get CURIEs added, only the top level response object or embedded objects get processed this way.
Patch 7 works but there's definitely a neater way it could be done, I wasn't sure if there was a specific reason for handling it in the server rather than the response.
I'll add to the tests too.
#33
in reply to:
↑ 32
@
8 years ago
Replying to sanchothefat:
Items in a collection don't get CURIEs added, only the top level response object or embedded objects get processed this way.
This ticket was closed on a completed milestone, could you create a new one? Thanks!
#35086 was marked as a duplicate.