From 995ac974dc3a09b98a281dd3df9672e7d0dfa44a Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 6 Aug 2026 22:55:35 +0200 Subject: [PATCH] fix: improve type hinting for unified share listing methods Signed-off-by: Robin Appelman --- apps/sharing/lib/Controller/ApiV1Controller.php | 11 ++++++++++- apps/sharing/openapi.json | 3 ++- apps/sharing/tests/Controller/ApiV1ControllerTest.php | 5 ++--- lib/private/Sharing/SharingBackend.php | 1 + lib/private/Sharing/SharingManager.php | 2 +- lib/unstable/Sharing/ISharingBackend.php | 1 + lib/unstable/Sharing/ISharingManager.php | 1 + openapi.json | 3 ++- 8 files changed, 20 insertions(+), 7 deletions(-) diff --git a/apps/sharing/lib/Controller/ApiV1Controller.php b/apps/sharing/lib/Controller/ApiV1Controller.php index 6582beeb7ed02..0efcbc4c6eaff 100644 --- a/apps/sharing/lib/Controller/ApiV1Controller.php +++ b/apps/sharing/lib/Controller/ApiV1Controller.php @@ -557,7 +557,7 @@ public function getShare(string $id, ?string $secret = null, array $arguments = * Get multiple shares. * * @param ?class-string $filterSourceTypeClass Source type class to filter by. - * @param ?string $filterSourceTypeValue Source type value to filter by. + * @param ?non-empty-string $filterSourceTypeValue Source type value to filter by. * @param ?string $lastShareID The ID of the previous share. This is used as an offset and only shares with higher IDs are returned. * @param int<1, 100> $limit The number of shares to return. * @return DataResponse, array{}>|DataResponse @@ -578,6 +578,15 @@ public function getShares(?string $filterSourceTypeClass, ?string $filterSourceT return new DataResponse('The limit is too high.', Http::STATUS_BAD_REQUEST); } + /** @psalm-suppress TypeDoesNotContainType */ + if ($filterSourceTypeValue === '') { + return new DataResponse('Filter source value is empty.', Http::STATUS_BAD_REQUEST); + } + + if ($filterSourceTypeClass && !isset($this->registry->getSourceTypes()[$filterSourceTypeClass])) { + return new DataResponse('The filter source type is not registered: ' . $filterSourceTypeClass, Http::STATUS_BAD_REQUEST); + } + try { $this->dbConnection->beginTransaction(); diff --git a/apps/sharing/openapi.json b/apps/sharing/openapi.json index f5c11a9c6732d..191435bf86c60 100644 --- a/apps/sharing/openapi.json +++ b/apps/sharing/openapi.json @@ -3110,7 +3110,8 @@ "description": "Source type value to filter by.", "schema": { "type": "string", - "nullable": true + "nullable": true, + "minLength": 1 } }, { diff --git a/apps/sharing/tests/Controller/ApiV1ControllerTest.php b/apps/sharing/tests/Controller/ApiV1ControllerTest.php index 447d5b95dd993..eaf057a8f4aa6 100644 --- a/apps/sharing/tests/Controller/ApiV1ControllerTest.php +++ b/apps/sharing/tests/Controller/ApiV1ControllerTest.php @@ -8,7 +8,6 @@ declare(strict_types=1); use NCU\Sharing\ISharingManager; -use NCU\Sharing\ISharingRegistry; use NCU\Sharing\Permission\SharePermission; use NCU\Sharing\Property\ShareProperty; use NCU\Sharing\Recipient\ShareRecipient; @@ -47,7 +46,7 @@ public function testDefaultShareAccessContext(): void { Server::get(IRequest::class), Server::get(IUserSession::class), Server::get(ISharingManager::class), - Server::get(ISharingRegistry::class), + $this->registry, Server::get(IFactory::class), Server::get(IURLGenerator::class), Server::get(IUserManager::class), @@ -68,7 +67,7 @@ private function executeRequest(ShareAccessContext $accessContext, Closure $clos Server::get(IRequest::class), Server::get(IUserSession::class), Server::get(ISharingManager::class), - Server::get(ISharingRegistry::class), + $this->registry, Server::get(IFactory::class), Server::get(IURLGenerator::class), Server::get(IUserManager::class), diff --git a/lib/private/Sharing/SharingBackend.php b/lib/private/Sharing/SharingBackend.php index 5d6cb5d4017d2..13606b241b815 100644 --- a/lib/private/Sharing/SharingBackend.php +++ b/lib/private/Sharing/SharingBackend.php @@ -556,6 +556,7 @@ private function hideDisabledUserShares(): bool { /** * @param ?class-string $filterSourceTypeClass + * @param ?non-empty-string $filterSourceTypeValue * @return list */ private function list(ShareAccessContext $accessContext, ?string $filterShareID, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?string $lastShareID, ?int $limit): array { diff --git a/lib/private/Sharing/SharingManager.php b/lib/private/Sharing/SharingManager.php index 76593a8f4bc0b..dc96ad31a85ff 100644 --- a/lib/private/Sharing/SharingManager.php +++ b/lib/private/Sharing/SharingManager.php @@ -698,7 +698,7 @@ private function validateInteraction(ShareAccessContext $accessContext, Share $s $action = new ShareAction(null, array_values(array_map(static fn (SharePermission $permission): string => $permission->class, $share->getEnabledPermissions()))); $usersToCheck = []; - if ($share->owner->instance === null && ($ownerUser = $this->userManager->get($share->owner->userId)) !== null) { + if ($share->owner->instance === null && ($ownerUser = $this->userManager->get($share->owner->userId)) instanceof IUser) { $usersToCheck[] = $ownerUser; } diff --git a/lib/unstable/Sharing/ISharingBackend.php b/lib/unstable/Sharing/ISharingBackend.php index a4cb1401dd9cc..a483f4c87c866 100644 --- a/lib/unstable/Sharing/ISharingBackend.php +++ b/lib/unstable/Sharing/ISharingBackend.php @@ -174,6 +174,7 @@ public function getShare(ShareAccessContext $accessContext, string $id): Share; * Get multiple shares. * * @param ?class-string $filterSourceTypeClass + * @param ?non-empty-string $filterSourceTypeValue * @param ?positive-int $limit * @return list * @experimental 35.0.0 diff --git a/lib/unstable/Sharing/ISharingManager.php b/lib/unstable/Sharing/ISharingManager.php index 21128680aaefc..d2aec4f013c55 100644 --- a/lib/unstable/Sharing/ISharingManager.php +++ b/lib/unstable/Sharing/ISharingManager.php @@ -216,6 +216,7 @@ public function getShare(ShareAccessContext $accessContext, string $id): Share; * Get multiple shares. * * @param ?class-string $filterSourceTypeClass + * @param ?non-empty-string $filterSourceTypeValue * @param ?positive-int $limit * @return list * @experimental 35.0.0 diff --git a/openapi.json b/openapi.json index 25ef87591e40b..2404ec3ce530d 100644 --- a/openapi.json +++ b/openapi.json @@ -39006,7 +39006,8 @@ "description": "Source type value to filter by.", "schema": { "type": "string", - "nullable": true + "nullable": true, + "minLength": 1 } }, {