Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 13 days ago

#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)

51131.diff​ (514 bytes ) - added by stoyangeorgiev 6 years ago.
Remove absint from the result ids
51131.1.diff​ (1.1 KB ) - added by stoyangeorgiev 6 years ago.
Changed param type and schema type

Download all attachments as: .zip

Change History (14)

@stoyangeorgiev
6 years ago

Remove absint from the result ids

#1 @stoyangeorgiev
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 @TimothyBlynJacobs
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.

@stoyangeorgiev
6 years ago

Changed param type and schema type

#3 @stoyangeorgiev
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 @TimothyBlynJacobs
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.

​https://github.com/WordPress/wordpress-develop

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 @stoyangeorgiev
6 years ago

Hello there @TimothyBlynJacobs,

I have opened a Pull Request and all checks have passed successfully.

#7 @TimothyBlynJacobs
6 years ago

  • Owner set to TimothyBlynJacobs
  • Resolution → fixed
  • Status new → closed

In 49088:

REST API: Allow for string ids in the search controller.

Previously, the search controller infrastructure required that the id property was an integer. This prevents data models that use a string id from utilizing the search infrastructure.

This commit lifts the restraint that search handlers return integer ids. This will allow for the Post Formats search handler coming in 5.6 to use slugs instead of creating fake ids.

Props stoyangeorgiev.
Fixes #51131.

​TimothyBJacobs commented on ​PR #564:


6 years ago
#8

Fixed in a3eb0d8. Thanks again or the patch @stoyan-g!

#9 @SergeyBiryukov
6 years ago

In 49089:

Docs: Add @since notes for the $id parameter of REST API search controller accepting a string.

Follow-up to [49088].

See #51131.

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 the id property's schema type is array( 'integer', 'string' ), not just integer.
  • 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 like aside as ids) and asserts the returned id values 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.

#11 @lancewillett
13 days ago

In 65100:

Tests: Cover string IDs in REST search results.

Verify that the search endpoint schema accepts integer and string IDs. Check that post-format searches preserve string slugs rather than coercing them to integers.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14049

Props irozum, huzaifaalmesbah.
See #65819, #51131.

Note: See TracTickets for help on using tickets.