diff --git a/src/Encoder/EncoderPropertiesTrait.php b/src/Encoder/EncoderPropertiesTrait.php index 885ce401..703354e2 100644 --- a/src/Encoder/EncoderPropertiesTrait.php +++ b/src/Encoder/EncoderPropertiesTrait.php @@ -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 { diff --git a/tests/Encoder/EncodeIncludedObjectsTest.php b/tests/Encoder/EncodeIncludedObjectsTest.php index fefd074b..c67895a3 100644 --- a/tests/Encoder/EncodeIncludedObjectsTest.php +++ b/tests/Encoder/EncodeIncludedObjectsTest.php @@ -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; + } + } }