Filter out search results that have invalid encoding

this prevents a single invalid search results from erroring the entire search request

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2020-06-16 15:22:36 +02:00
parent 13f119d48d
commit fda55db4c9
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
1 changed files with 20 additions and 3 deletions

View File

@ -28,20 +28,28 @@ namespace OC\Core\Controller;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\ILogger;
use OCP\IRequest;
use OCP\ISearch;
use OCP\Search\Result;
class SearchController extends Controller {
/** @var ISearch */
private $searcher;
/** @var ILogger */
private $logger;
public function __construct(string $appName,
IRequest $request,
ISearch $search) {
public function __construct(
string $appName,
IRequest $request,
ISearch $search,
ILogger $logger
) {
parent::__construct($appName, $request);
$this->searcher = $search;
$this->logger = $logger;
}
/**
@ -50,6 +58,15 @@ class SearchController extends Controller {
public function search(string $query, array $inApps = [], int $page = 1, int $size = 30): JSONResponse {
$results = $this->searcher->searchPaged($query, $inApps, $page, $size);
$results = array_filter($results, function (Result $result) {
if (json_encode($result, JSON_HEX_TAG) === false) {
$this->logger->warning("Skipping search result due to invalid encoding: {type: " . $result->type . ", id: " . $result->id . "}");
return false;
} else {
return true;
}
});
return new JSONResponse($results);
}
}