Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CollectionRepresentation resolve generator and make StreamedResponse not possible #254

Open
alexander-schranz opened this issue Dec 6, 2017 · 2 comments

Comments

@alexander-schranz
Copy link

alexander-schranz commented Dec 6, 2017

This lines: https://github.com/willdurand/Hateoas/blob/master/src/Hateoas/Representation/CollectionRepresentation.php#L64-L66
will resolve a Generator and throws an error in my StreamedResponse: An Exception was thrown while handling: Cannot rewind a generator that was already run.

The changes seems to come from 1944881#diff-2961d2c1218fee48b9674553eeab647a so I'm not sure why @adrienbrault did add it.

Maybe we could go with:

if ($resources instanceof \Traversable && !$resources instanceof \Generator) {
    $resources = iterator_to_array($resources);
}

so that the generator is not resolved.

Code to reproduce:

function testGenerator() {
    for ($i = 0; $i <= 5; ++$i) {
        yield = ['id' => $id];
    }
}

$entries = testGenerator();
$collectionRepresentation = new CollectionRepresentation($entries);
// Following code will error: An Exception was thrown while handling: Cannot rewind a generator that was already run
$entries->rewind();
@alexander-schranz
Copy link
Author

Found the discussion here: #133 (diff)
was this maybe fixed in the serializer that Traversable are handled the same way as arrays? /cc @schmittjoh

@adrienbrault
Copy link
Contributor

@alexander-schranz Feel free to open a pull request with your proposal and a new test case 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants