-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getEntityRecords returns empty list for custom endpoint #61348
Comments
I haven't started debugging this yet, but I would guess that the returned response "shape" is the cause, which is usually an array of objects with a unique key. The // Returns null.
wp.data.select( 'core' ).getEntityRecords( 'root', 'site' );
// Returns settings object.
wp.data.select( 'core' ).getEntityRecord( 'root', 'site' ); Can you test if |
Using Changing the API output to an array of objects and use [{
"role": "editor"
}, {
"role": "administrator"
}]
|
Okay, after some more investigation, it seems there was a breaking change and [{
"role": "editor",
"id": 1
}, {
"role": "administrator",
"id": 2
}] |
Thanks for the additional details, @MatzeKitt! Can you share an example of the endpoint code you're testing with? I have something locally, but I want to be as close to the source as possible. The entities have always required a primary key; that's how they're stored. However, this is less documented due to the entities' semi-private API. |
The API consists of these files: This is the current working example with ID. The original has a slightly different /**
* Get user information.
*
* @param string $option The current object name
* @param \WP_REST_Request $request The current request
* @return mixed|void|\WP_Error|\WP_HTTP_Response|\WP_REST_Response The API response
*/
public function get_value( string $option, WP_REST_Request $request ) {
$user = wp_get_current_user();
switch ( $request->get_param( 'get' ) ) {
case 'current_roles':
if ( empty( $user->roles ) ) {
return rest_ensure_response( [ 'no_roles' ] );
}
// make super admin always to administrator
if ( is_super_admin() ) {
return rest_ensure_response( array_merge( $user->roles, [ 'administrator' ] ) );
}
return rest_ensure_response( $user->roles );
case 'restricted_roles':
return rest_ensure_response( Heimdall::get_instance()->get_restricted_roles( 'name' ) );
case 'roles':
return rest_ensure_response( Heimdall::get_instance()->get_roles( 'name' ) );
}
} The actual request is done via Thank you for investigating!
Interesting, since it worked before 6.5 quite fine without an ID whatsoever. It seems something changed there. Since it’s semi-private, what is the intented public API for such a function? A custom store with |
I call everything that's not documented "semi-private" :) But I agree that if it was working previously without an ID, and unless that was a bug, we'll have to restore the previous behavior. cc @jsnajdr, @youknowriad |
Yeah, entities were always meant to have an "ID", I wonder how it was working before and what was considered "primary key" internally for this use-case. |
The items returned by the API endpoint are expected to be objects. The code cannot handle strings. Mainly because every item needs to have an unique "key". You can register a key field name when adding the entity: addEntities( [
{
name: 'user_roles',
kind: 'namespace',
baseURL: '/namespace/v1/roles',
key: 'role', // <<=== this is new
},
] ) Then the version of the endpoint that returns If we wanted to support the I believe it was always like this, this bug report is not a regression. And @MatzeKitt doesn't say anywhere that it is. |
@jsnajdr, the latest comment from @MatzeKitt mentions that this was working without a My understanding is the same. Entity record lists need an array of objects with a unique key. |
Yeah, it definitely worked before. But if it was never intended to work that way, I’m totally fine with it not being changed again. I now know what I did wrong and could fix it on my end. 🙂 Also thank you @jsnajdr for the |
OK, I'll try to find what changed. |
One of the PRs that affect this is #55832 by @ntsekouras. In But there must be also something else. If all the items have an |
I close it since even though it worked before for me, I now adjusted my code accordingly and since it previously was used in the wrong way by myself, any deeper investigation would be waste of time. |
Thank you @MatzeKitt for reporting the issue though! |
One actionable thing to do is to figure out why it used to work before, and when exactly it stopped working. As far as I know, entities were always indexed by a |
Description
I have a custom API endpoint to request the user roles of a user, which returns basically this:
This endpoint is accessible via
/namespace/v1/roles
and works fine.Then, I use this endpoint in a
PluginSidebar
like so:The
data.roles
is just an empty array despite the API endpoint returns the correct data as array (e.g.["subscriber", "administrator"]
), as also seen in the network tab in the developer tools.Step-by-step reproduction instructions
data.roles
, which is emptyScreenshots, screen recording, code snippet
No response
Environment info
No response
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
The text was updated successfully, but these errors were encountered: