#51131 closed enhancement (fixed)
Remove constraint that search handlers must return integer ids
| Reported by: | TimothyBlynJacobs | Owned by: | TimothyBlynJacobs |
|---|---|---|---|
| Priority: | normal | Milestone: | 5.6 |
| Component: | REST API | Version: | 5.0 |
| Severity: | normal | Keywords: | good-first-bug has-patch has-unit-tests |
| Cc: | Focuses: |
Description
The WP_REST_Search_Controller currently enforces that the id property returned by WP_REST_Search_Handler::search_items is an integer. AFAICT this constraint isn't required. But it does mean that items that don't have integer ids can't be searched.
A string id isn't uncommon, for example uuids or hashids. But this also causes a particular issue in Gutenberg where we want to expose post formats via the API. Ideally the formats would be referred to by their slug, but currently Gutenberg has to create fake ids to satisfy the search controller.
Attachments (2)
Change History (14)
#1
@
6 years ago
- Keywords has-patch added; needs-patch removed
I have uploaded a patch where I have removed the absint for the result ids.
#2
@
6 years ago
Thanks for working on a patch @stoyangeorgiev!
There are a couple of other changes we need to make too. The type hints for the $id parameter should be updated to int|string. We'll also need to update the id schema in WP_REST_Search_Controller to be an array of array( 'integer', 'string' ) to reflect that it can be either of those types.
#3
@
6 years ago
Hello there @TimothyBlynJacobs,
I have done the changes, you mentioned above. Changed the parameter hint and also added the array of types to the schema.
#4
@
6 years ago
Thanks @stoyangeorgiev!
We also need to do similar doc block updates for WP_REST_Search_Handler.
Could you also open this as a Pull Request? That way we can automatically get tests and linting to run.
This ticket was mentioned in PR #564 on WordPress/wordpress-develop by stoyan-g.
6 years ago
#5
Removed the constraints in WP_REST_Search_Controller, updated id schema, and updated the docs. Edited the docs for WP_REST_Search_Handler.
Trac ticket: https://core.trac.wordpress.org/ticket/51131
#6
@
6 years ago
Hello there @TimothyBlynJacobs,
I have opened a Pull Request and all checks have passed successfully.
TimothyBJacobs commented on PR #564:
6 years ago
#8
Fixed in a3eb0d8. Thanks again or the patch @stoyan-g!
This ticket was mentioned in PR #14049 on WordPress/wordpress-develop by @irozum.
5 weeks ago
#10
- Keywords has-unit-tests added; needs-unit-tests removed
What this adds
Test-only PR. [49088] lifted the constraint that WP_REST_Search_Handler::search_items() results must use integer ids, and updated the WP_REST_Search_Controller item schema's id property to accept integer|string. That change shipped in 5.6 but was never covered by a dedicated test, and needs-unit-tests is still on the ticket.
test_get_item_schema_id_accepts_integer_and_string— asserts theidproperty's schematypeisarray( 'integer', 'string' ), not justinteger.test_get_items_search_type_post_format_returns_string_ids— exercises the real-world motivating case from the ticket (post format search, which core ships using format slugs likeasideas ids) and asserts the returnedidvalues are the unmodified string slugs, not coerced to integers.
Why these cases
The post-format handler (WP_REST_Post_Format_Search_Handler) is the concrete, already-shipped example of a non-integer-id search handler the ticket was written for, so testing through it (rather than a synthetic handler) pins down the actual behavior real consumers rely on. The schema assertion guards the other half of the fix — the public contract clients see via OPTIONS — which is easy to regress independently of the controller logic.
This is a follow-up to [49088], which already landed; no production code is touched here.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Picking the ticket, analyzing the existing fix and test file to design the test cases, writing the tests, and verifying each one fails without the fix and passes with it. Reviewed and verified by me before submission.
@lancewillett commented on PR #14049:
13 days ago
#12
PR #14049 landed in https://core.trac.wordpress.org/changeset/65100
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Remove absint from the result ids