Fix select query handling to only return $id when no attributes specified

Co-authored-by: jakeb994 <jakeb994@gmail.com>
This commit is contained in:
Cursor Agent
2025-08-12 13:16:39 +00:00
co-authored by jakeb994
parent 7118146174
commit 1c0b5e9b6d
2 changed files with 20 additions and 9 deletions
+8 -2
View File
@@ -662,9 +662,11 @@ App::get('/v1/users')
$processedQueries = [];
$allSelectedAttributes = [];
$hasSelectQuery = false;
foreach ($queries as $query) {
if ($query->getMethod() === Query::TYPE_SELECT) {
$hasSelectQuery = true;
$selectedAttributes = $query->getValues();
foreach ($selectedAttributes as $attribute) {
@@ -679,8 +681,12 @@ App::get('/v1/users')
}
}
if (!empty($allSelectedAttributes)) {
$processedQueries[] = Query::select(array_unique($allSelectedAttributes));
if ($hasSelectQuery) {
if (!empty($allSelectedAttributes)) {
$processedQueries[] = Query::select(array_unique($allSelectedAttributes));
} else {
$processedQueries[] = Query::select(['$id']);
}
}
$queries = $processedQueries;
+12 -7
View File
@@ -1965,16 +1965,17 @@ trait UsersBase
$this->assertNotEmpty($response['body']);
$this->assertNotEmpty($response['body']['users']);
// When only subQuery attributes are selected, they should be available since their filters are not skipped
// When only subQuery attributes are selected, only $id is passed to database select
// but the subQuery attributes should be available since their filters are not skipped
$user = $response['body']['users'][0];
$this->assertArrayHasKey('$id', $user); // Only $id from database select
$this->assertArrayHasKey('sessions', $user);
$this->assertArrayHasKey('challenges', $user);
$this->assertArrayHasKey('authenticators', $user);
// Other attributes should also be present since no select query is passed to database
$this->assertArrayHasKey('$id', $user);
$this->assertArrayHasKey('name', $user);
$this->assertArrayHasKey('email', $user);
// Other attributes should NOT be present since only $id was selected from database
$this->assertArrayNotHasKey('name', $user);
$this->assertArrayNotHasKey('email', $user);
/**
* Test Query::select with system attributes
@@ -2168,10 +2169,14 @@ trait UsersBase
$this->assertNotEmpty($response['body']['users']);
// Since targets is the only selected attribute and it's a subQuery attribute,
// but subQueryTargets is not skipped by default, all attributes should be returned
// only $id should be passed to database select, but targets should be available
$user = $response['body']['users'][0];
$this->assertArrayHasKey('$id', $user);
$this->assertArrayHasKey('$id', $user); // Only $id from database select
$this->assertArrayHasKey('targets', $user);
// Other attributes should NOT be present since only $id was selected from database
$this->assertArrayNotHasKey('name', $user);
$this->assertArrayNotHasKey('email', $user);
/**
* Core functionality test: verify subQuery attributes handling