Skip to content

Commit

Permalink
Fix fatal error when providing invalid type in fields and include que…
Browse files Browse the repository at this point in the history
…ry parameters
  • Loading branch information
kocsismate committed Feb 6, 2018
1 parent ec09233 commit 4ffaccd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ REMOVED:

FIXED:

## 2.0.6 - unreleased

FIXED:

- [#69](https://github.com/woohoolabs/yin/pull/69): Fatal error when providing invalid types in `fields`, `include` and `sort` query parameters

## 2.0.5 - 2018-01-31

FIXED:

- [#68](https://github.com/woohoolabs/yin/pull/68): Fix fatal error when resource ID is not string
- [#68](https://github.com/woohoolabs/yin/pull/68): Fatal error when resource ID is not string

## 2.0.4 - 2017-09-13

Expand Down
2 changes: 0 additions & 2 deletions src/JsonApi/Exception/QueryParamMalformed.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class QueryParamMalformed extends JsonApiException
protected $malformedQueryParamValue;

/**
* QueryParamMalformed constructor.
* @param string $malformedQueryParam
* @param mixed $malformedQueryParamValue
*/
public function __construct(string $malformedQueryParam, $malformedQueryParamValue)
Expand Down
20 changes: 14 additions & 6 deletions src/JsonApi/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,15 @@ protected function setIncludedFields()
$this->includedFields = [];
$fields = $this->getQueryParam("fields", []);
if (is_array($fields) === false) {
return;
throw $this->exceptionFactory->createQueryParamMalformedException($this, "fields", $fields);
}

foreach ($fields as $resourceType => $resourceFields) {
if (is_string($resourceFields)) {
$this->includedFields[$resourceType] = array_flip(explode(",", $resourceFields));
if (is_string($resourceFields) === false) {
throw $this->exceptionFactory->createQueryParamMalformedException($this, "fields", $fields);
}

$this->includedFields[$resourceType] = array_flip(explode(",", $resourceFields));
}
}

Expand Down Expand Up @@ -187,6 +189,11 @@ protected function setIncludedRelationships()
$this->includedRelationships = [];

$includeQueryParam = $this->getQueryParam("include", "");

if (is_string($includeQueryParam) === false) {
throw $this->exceptionFactory->createQueryParamMalformedException($this, "include", $includeQueryParam);
}

if ($includeQueryParam === "") {
return;
}
Expand Down Expand Up @@ -235,9 +242,9 @@ public function getIncludedRelationships(string $baseRelationshipPath): array

if (isset($this->includedRelationships[$baseRelationshipPath])) {
return array_values($this->includedRelationships[$baseRelationshipPath]);
} else {
return [];
}

return [];
}

/**
Expand Down Expand Up @@ -279,9 +286,10 @@ public function getSorting(): array
protected function setSorting()
{
$sortingQueryParam = $this->getQueryParam("sort", "");
if (!is_string($sortingQueryParam)) {
if (is_string($sortingQueryParam) === false) {
throw $this->exceptionFactory->createQueryParamMalformedException($this, "sort", $sortingQueryParam);
}

if ($sortingQueryParam === "") {
$this->sorting = [];

Expand Down
39 changes: 39 additions & 0 deletions tests/JsonApi/Request/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,32 @@ public function getIncludedFieldsForUnspecifiedResource()
$this->assertEquals([], $request->getIncludedFields($resourceType));
}

/**
* @test
*/
public function getIncludedFieldWhenMalformed()
{
$this->expectException(QueryParamMalformed::class);

$queryParams = ["fields" => ""];

$request = $this->createRequestWithQueryParams($queryParams);
$request->getIncludedFields("");
}

/**
* @test
*/
public function getIncludedFieldWhenFieldMalformed()
{
$this->expectException(QueryParamMalformed::class);

$queryParams = ["fields" => ["book" => []]];

$request = $this->createRequestWithQueryParams($queryParams);
$request->getIncludedFields("");
}

/**
* @test
*/
Expand Down Expand Up @@ -314,6 +340,19 @@ public function getIncludedRelationshipsForMultipleEmbeddedResource()
$this->assertEquals($includedRelationships, $request->getIncludedRelationships($baseRelationshipPath));
}

/**
* @test
*/
public function getIncludedRelationshipsWhenMalformed()
{
$this->expectException(QueryParamMalformed::class);

$queryParams = ["include" => []];

$request = $this->createRequestWithQueryParams($queryParams);
$request->getIncludedRelationships("");
}

/**
* @test
*/
Expand Down

0 comments on commit 4ffaccd

Please sign in to comment.