Skip to content

Commit

Permalink
Close #238
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowhand authored and neomerx committed Jun 18, 2019
1 parent 95cc4aa commit 7a64109
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Encoder/EncoderPropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ protected function getUrlPrefix(): string
*/
public function withIncludedPaths(iterable $paths): EncoderInterface
{
$paths = $this->iterableToArray($paths);

\assert(
\call_user_func(
function (array $paths): bool {
Expand Down
29 changes: 29 additions & 0 deletions tests/Encoder/EncodeIncludedObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1045,4 +1045,33 @@ public function testEncodeRelationshipsAsLinks(): void
EOL;
self::assertJsonStringEqualsJsonString($expected, $actual);
}

/**
* Test include paths as generator.
*
* @see https://github.com/neomerx/json-api/issues/238
*/
public function testIterableParameterForWithIncludedPaths(): void
{
$author = Author::instance(238, 'Susan', 'Smith');
$post = Post::instance(11, 'Generators and Arrays', 'A tale of incompatible types', $author);

$encoder = Encoder::instance([
Author::class => AuthorSchema::class,
Post::class => PostSchema::class,
]);

$encoder = $encoder->withIncludedPaths($this->generateIncludeList());

$json = $encoder->encodeData($post);

self::assertJson($json);
}

private function generateIncludeList(): iterable
{
foreach (['author'] as $item) {
yield $item;
}
}
}

0 comments on commit 7a64109

Please sign in to comment.