Skip to content

Commit

Permalink
Fix disappearing resources with an ID of "0"
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Dec 6, 2017
1 parent 1814ca6 commit 6a6ab89
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 21 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ REMOVED:

FIXED:

## 1.3.2 - unreleased

FIXED:

- [#9](https://github.com/woohoolabs/yang/issues/9): Resources with an ID of "0" disappear when using `ClassHydrator`
- Other issues with "0" affecting `ResourceObject::toString()` and `JsonApiRequestBuilder::toString()`

## 1.3.1 - 2017-11-23

FIXED:
Expand Down
15 changes: 10 additions & 5 deletions src/JsonApi/Request/JsonApiRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,23 @@ public function setUri(string $uri): JsonApiRequestBuilder
return $this;
}

if (empty($parsedUrl["scheme"]) === false) {
if ($this->isBlank($parsedUrl["scheme"]) === false) {
$this->scheme = $parsedUrl["scheme"];
}

if (empty($parsedUrl["port"]) === false) {
if ($this->isBlank($parsedUrl["port"]) === false) {
$this->port = (int) $parsedUrl["port"];
}

if (empty($parsedUrl["host"]) === false) {
if ($this->isBlank($parsedUrl["host"]) === false) {
$this->host = $parsedUrl["host"];
}

if (empty($parsedUrl["path"]) === false) {
if ($this->isBlank($parsedUrl["path"]) === false) {
$this->path = $parsedUrl["path"];
}

if (empty($parsedUrl["query"]) === false) {
if ($this->isBlank($parsedUrl["query"]) === false) {
parse_str($parsedUrl["query"], $this->queryString);
}

Expand Down Expand Up @@ -308,4 +308,9 @@ private function setListQueryParam(string $name, $queryParam): void
$this->queryString[$name] = $queryParam;
}
}

private function isBlank($value): bool
{
return empty($value) && !is_numeric($value);
}
}
9 changes: 7 additions & 2 deletions src/JsonApi/Request/ResourceObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public function toArray(): array
$resource = [
"data" => [
"type" => $this->type,
]
],
];

if (empty($this->id) === false) {
if ($this->isBlank($this->id) === false) {
$resource["data"]["id"] = $this->id;
}

Expand All @@ -94,4 +94,9 @@ public function toArray(): array

return $resource;
}

private function isBlank($value): bool
{
return empty($value) && !is_numeric($value);
}
}
9 changes: 7 additions & 2 deletions src/JsonApi/Schema/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private static function createToOneFromArray(
$resourceMap = [];
$isToOneRelationship = true;

if (empty($data["type"]) === false && empty($data["id"]) === false) {
if (self::isBlank($data["type"]) === false && self::isBlank($data["id"]) === false) {
$resourceMap = [
[
"type" => $data["type"],
Expand All @@ -105,7 +105,7 @@ private static function createToManyFromArray(
$resourceMap = [];

foreach ($data as $item) {
if (empty($item["type"]) === false && empty($item["id"]) === false) {
if (self::isBlank($item["type"]) === false && self::isBlank($item["id"]) === false) {
$resource = [
"type" => $item["type"],
"id" => $item["id"],
Expand Down Expand Up @@ -292,4 +292,9 @@ private static function isArrayKey(array $array, string $key): bool
{
return isset($array[$key]) && is_array($array[$key]);
}

private static function isBlank($value): bool
{
return empty($value) && !is_numeric($value);
}
}
13 changes: 6 additions & 7 deletions tests/JsonApi/Hydrator/ClassHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

namespace WoohooLabs\Yang\Tests\JsonApi\Hydrator;

use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use WoohooLabs\Yang\JsonApi\Hydrator\ClassHydrator;
use WoohooLabs\Yang\JsonApi\Schema\Document;
use WoohooLabs\Yang\JsonApi\Schema\JsonApi;
use WoohooLabs\Yang\JsonApi\Serializer\JsonDeserializer;

class ClassHydratorTest extends TestCase
{
Expand Down Expand Up @@ -90,7 +87,7 @@ public function hydrateObjectsWithAttributes()
],
[
"type" => "a",
"id" => "2",
"id" => "0",
"attributes" => [
"a" => "D",
"b" => "E",
Expand All @@ -105,10 +102,12 @@ public function hydrateObjectsWithAttributes()
$objects = $hydrator->hydrate($document);

$this->assertCount(2, $objects);
$this->assertAttributeSame("1", "id", $objects[0]);
$this->assertAttributeSame("A", "a", $objects[0]);
$this->assertAttributeSame("B", "b", $objects[0]);
$this->assertAttributeSame("C", "c", $objects[0]);

$this->assertAttributeSame("0", "id", $objects[1]);
$this->assertAttributeSame("D", "a", $objects[1]);
$this->assertAttributeSame("E", "b", $objects[1]);
$this->assertAttributeSame("F", "c", $objects[1]);
Expand Down Expand Up @@ -152,15 +151,15 @@ public function hydrateObjectWithIncludedToOneRelationship()
"x" => [
"data" => [
"type" => "b",
"id" => "1",
"id" => "0",
],
],
],
],
"included" => [
[
"type" => "b",
"id" => "1",
"id" => "0",
"attributes" => [
"a" => "A",
"b" => "B",
Expand All @@ -175,7 +174,7 @@ public function hydrateObjectWithIncludedToOneRelationship()

$this->assertObjectHasAttribute("x", $object);
$this->assertAttributeSame("b", "type", $object->x);
$this->assertAttributeSame("1", "id", $object->x);
$this->assertAttributeSame("0", "id", $object->x);
$this->assertAttributeSame("A", "a", $object->x);
}

Expand Down
18 changes: 15 additions & 3 deletions tests/JsonApi/Request/JsonApiRequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ public function setUri()
$this->assertSame("http://example.com/api/users", $requestBuilder->getRequest()->getUri()->__toString());
}

/**
* @test
*/
public function setUriWithZero()
{
$requestBuilder = $this->createRequestBuilder();

$requestBuilder->setUri("http://example.com/api/users?0");

$this->assertSame("http://example.com/api/users?0", $requestBuilder->getRequest()->getUri()->__toString());
}

/**
* @test
*/
Expand Down Expand Up @@ -144,13 +156,13 @@ public function setUriPath()
/**
* @test
*/
public function setUriQueryParam()
public function setUriQueryParamWithZeroValue()
{
$requestBuilder = $this->createRequestBuilder();

$requestBuilder->setUriQueryParam("a", "b");
$requestBuilder->setUriQueryParam("a", "0");

$this->assertSame("a=b", $requestBuilder->getRequest()->getUri()->getQuery());
$this->assertSame("a=0", $requestBuilder->getRequest()->getUri()->getQuery());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/JsonApi/Request/ResourceObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public function getType()
*/
public function getId()
{
$resource = new ResourceObject("", "b");
$resource = new ResourceObject("", "0");

$this->assertSame(
[
"data" => [
"type" => "",
"id" => "b",
"id" => "0",
],
],
$resource->toArray()
Expand Down

0 comments on commit 6a6ab89

Please sign in to comment.