Skip to content

Commit

Permalink
Close #235
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Apr 12, 2019
1 parent 3356153 commit 1807c3b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/Parser/Parser.php
Expand Up @@ -232,8 +232,11 @@ private function parseResource(ResourceInterface $resource): iterable
continue;
} elseif ($relData->isCollection() === true) {
foreach ($relData->getResources() as $relResource) {
\assert($relResource instanceof ResourceInterface);
yield from $this->parseResource($relResource);
\assert($relResource instanceof ResourceInterface ||
$relResource instanceof IdentifierInterface);
if ($relResource instanceof ResourceInterface) {
yield from $this->parseResource($relResource);
}
}

continue;
Expand Down
11 changes: 7 additions & 4 deletions src/Parser/RelationshipData/RelationshipDataIsCollection.php
Expand Up @@ -19,10 +19,11 @@
*/

use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
use Neomerx\JsonApi\Contracts\Parser\IdentifierInterface;
use Neomerx\JsonApi\Contracts\Parser\IdentifierInterface as ParserIdentifierInterface;
use Neomerx\JsonApi\Contracts\Parser\PositionInterface;
use Neomerx\JsonApi\Contracts\Parser\RelationshipDataInterface;
use Neomerx\JsonApi\Contracts\Parser\ResourceInterface;
use Neomerx\JsonApi\Contracts\Schema\IdentifierInterface as SchemaIdentifierInterface;
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
use Neomerx\JsonApi\Exceptions\LogicException;
use function Neomerx\JsonApi\I18n\format as _;
Expand Down Expand Up @@ -97,7 +98,7 @@ public function isIdentifier(): bool
/**
* @inheritdoc
*/
public function getIdentifier(): IdentifierInterface
public function getIdentifier(): ParserIdentifierInterface
{
throw new LogicException(_(static::MSG_INVALID_OPERATION));
}
Expand All @@ -124,8 +125,10 @@ public function getResource(): ResourceInterface
public function getResources(): iterable
{
if ($this->parsedResources === null) {
foreach ($this->resources as $resource) {
$parsedResource = $this->createParsedResource($resource);
foreach ($this->resources as $resourceOrIdentifier) {
$parsedResource = $resourceOrIdentifier instanceof SchemaIdentifierInterface ?
$this->createParsedIdentifier($resourceOrIdentifier) :
$this->createParsedResource($resourceOrIdentifier);
$this->parsedResources[] = $parsedResource;

yield $parsedResource;
Expand Down
35 changes: 35 additions & 0 deletions tests/Data/Models/CommentIdentity.php
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace Neomerx\Tests\JsonApi\Data\Models;

/**
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use Neomerx\JsonApi\Schema\Identifier;

/**
* @package Neomerx\Tests\JsonApi
*/
class CommentIdentity extends Identifier
{
/**
* @param string $index
*/
public function __construct(string $index)
{
parent::__construct($index, 'comments');
}
}
50 changes: 50 additions & 0 deletions tests/Encoder/EncodeSimpleObjectsTest.php
Expand Up @@ -29,6 +29,7 @@
use Neomerx\Tests\JsonApi\Data\Models\AuthorCModel;
use Neomerx\Tests\JsonApi\Data\Models\AuthorIdentity;
use Neomerx\Tests\JsonApi\Data\Models\Comment;
use Neomerx\Tests\JsonApi\Data\Models\CommentIdentity;
use Neomerx\Tests\JsonApi\Data\Models\Site;
use Neomerx\Tests\JsonApi\Data\Schemas\AuthorCModelSchema;
use Neomerx\Tests\JsonApi\Data\Schemas\AuthorSchema;
Expand Down Expand Up @@ -1085,6 +1086,55 @@ public function testEncodeArrayBasedObject(): void
}
}]
}
EOL;
self::assertJsonStringEqualsJsonString($expected, $actual);
}

/**
* Test encode resource with an array of identifiers in a relationship.
*
* @see https://github.com/neomerx/json-api/pull/235
*/
public function testEncodingIdentifierArrays(): void
{
$commentId1 = new CommentIdentity('1');
$commentId2 = new CommentIdentity('2');
$author = Author::instance(9, 'Dan', 'Gebhardt', [$commentId1, $commentId2]);

$actual = Encoder::instance(
[
Author::class => AuthorSchema::class,
Comment::class => CommentSchema::class,
]
)->withUrlPrefix('http://example.com')->withIncludedPaths([Author::LINK_COMMENTS])->encodeData($author);

// The issue was that comment with id 1 was also added in `included` section
$expected = <<<EOL
{
"data": {
"type" : "people",
"id" : "9",
"attributes" : {
"first_name" : "Dan",
"last_name" : "Gebhardt"
},
"relationships" : {
"comments" : {
"data": [
{ "type" : "comments", "id" : "1" },
{ "type" : "comments", "id" : "2" }
],
"links" : {
"self" : "http://example.com/people/9/relationships/comments",
"related" : "http://example.com/people/9/comments"
}
}
},
"links" : {
"self" : "http://example.com/people/9"
}
}
}
EOL;
self::assertJsonStringEqualsJsonString($expected, $actual);
}
Expand Down

0 comments on commit 1807c3b

Please sign in to comment.