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

Generators are not supported for JSON responses. #5

Open
weierophinney opened this issue Dec 31, 2019 · 1 comment
Open

Generators are not supported for JSON responses. #5

weierophinney opened this issue Dec 31, 2019 · 1 comment

Comments

@weierophinney
Copy link
Member

Code to reproduce the issue

function getSomeData(): Generator {
    yield 1 => 'One';
    yield 2 => 'Two';
    yield 3 => 'Three';
}

$data = getSomeData();
$json = new Zend\Diactoros\Response\JsonResponse($data);

Expected results

Response should consume the generator and treat it as if a normal PHP array was passed in:

$data = [
    1 => 'One',
    2 => 'Two',
    3 => 'Three'
];

$json = new Zend\Diactoros\Response\JsonResponse($data);

Actual results

An error is thrown instead: "Trying to clone an uncloneable object of class Generator..."


Originally posted by @nbish11 at zendframework/zend-diactoros#365

@Xerkus
Copy link
Member

Xerkus commented May 2, 2023

JsonResponse expectation for $data is array or value bag object such as stdclass, same as values passed to json_encode().

phpdoc and documentation needs an update to clarify this expectation.

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

No branches or pull requests

2 participants