Skip to content

Commit

Permalink
Merge pull request #1261 from kinow/fix-rest-search-zero
Browse files Browse the repository at this point in the history
Fix REST search when using zero ('0') as the query parameter
  • Loading branch information
osma authored Jan 20, 2022
2 parents b7fd768 + aff32dd commit 52e7079
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
15 changes: 9 additions & 6 deletions controller/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,27 +169,30 @@ private function transformSearchResults($request, $results, $parameters)
* Performs the search function calls. And wraps the result in a json-ld object.
* @param Request $request
*/
public function search($request)
public function search(Request $request): void
{
$maxhits = $request->getQueryParam('maxhits');
$offset = $request->getQueryParam('offset');
$term = $request->getQueryParamRaw('query');

if (!$term && (!$request->getQueryParam('group') && !$request->getQueryParam('parent'))) {
return $this->returnError(400, "Bad Request", "query parameter missing");
if ((!isset($term) || strlen(trim($term)) === 0) && (!$request->getQueryParam('group') && !$request->getQueryParam('parent'))) {
$this->returnError(400, "Bad Request", "query parameter missing");
return;
}
if ($maxhits && (!is_numeric($maxhits) || $maxhits <= 0)) {
return $this->returnError(400, "Bad Request", "maxhits parameter is invalid");
$this->returnError(400, "Bad Request", "maxhits parameter is invalid");
return;
}
if ($offset && (!is_numeric($offset) || $offset < 0)) {
return $this->returnError(400, "Bad Request", "offset parameter is invalid");
$this->returnError(400, "Bad Request", "offset parameter is invalid");
return;
}

$parameters = $this->constructSearchParameters($request);
$results = $this->model->searchConcepts($parameters);
$ret = $this->transformSearchResults($request, $results, $parameters);

return $this->returnJson($ret);
$this->returnJson($ret);
}

/** Vocabulary-specific methods **/
Expand Down
35 changes: 35 additions & 0 deletions tests/RestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,41 @@ public function testSearchJsonLdWithAdditionalFields() {
$this->assertJsonStringEqualsJsonString('{"@context":{"skos":"http:\/\/www.w3.org\/2004\/02\/skos\/core#","isothes":"http:\/\/purl.org\/iso25964\/skos-thes#","onki":"http:\/\/schema.onki.fi\/onki#","uri":"@id","type":"@type","results":{"@id":"onki:results","@container":"@list"},"prefLabel":"skos:prefLabel","altLabel":"skos:altLabel","hiddenLabel":"skos:hiddenLabel","broader":"skos:broader","relatedMatch":"skos:relatedMatch"},"uri":"","results":[{"uri":"http:\/\/www.skosmos.skos\/test\/ta117","type":["skos:Concept","meta:TestClass"],"broader":[{"uri":"http:\/\/www.skosmos.skos\/test\/ta1"}],"relatedMatch":[{"uri":"http:\/\/www.skosmos.skos\/test\/ta115"}],"prefLabel":"3D Bass","lang":"en","vocab":"test"},{"uri":"http:\/\/www.skosmos.skos\/test\/ta116","type":["skos:Concept","meta:TestClass"],"broader":[{"uri":"http:\/\/www.skosmos.skos\/test\/ta1"}],"prefLabel":"Bass","lang":"en","vocab":"test"},{"uri":"http:\/\/www.skosmos.skos\/test\/ta122","type":["skos:Concept","meta:TestClass"],"broader":[{"uri":"http:\/\/www.skosmos.skos\/test\/ta116"}],"prefLabel":"Black sea bass","lang":"en","vocab":"test"}]}', $out);
}

/**
* Data provider for testSearchWithZero test.
*/
public function testSearchWithZeroData(): array {
return [
['0', true],
['1', true],
['-1', true],
['skosmos', true],
['', false],
[' ', false]
];
}

/**
* @covers RestController::search
* @dataProvider testSearchWithZeroData
* @param $query string the search query
* @param $isSuccess bool whether the request must succeed or fail
*/
public function testSearchWithZero(string $query, bool $isSuccess) {
$request = new Request($this->model);
$request->setQueryParam('format', 'application/json');
$request->setQueryParam('query', $query);
$request->setQueryParam('fields', 'broader relatedMatch');
$this->controller->search($request);

$out = $this->getActualOutput();
if ($isSuccess) {
$this->assertStringNotContainsString("400 Bad Request", $out, "The REST search call returned an unexpected 400 bad request error!");
} else {
$this->assertStringContainsString("400 Bad Request", $out, "The REST search call DID NOT return an expected 400 bad request error!");
}
}

/**
* @covers RestController::indexLetters
*/
Expand Down

0 comments on commit 52e7079

Please sign in to comment.