laminas / laminas-diactoros

PSR HTTP Message implementations
https://docs.laminas.dev/laminas-diactoros/
BSD 3-Clause "New" or "Revised" License
472 stars 62 forks source link

Generators are not supported for JSON responses. #5

Open weierophinney opened 4 years ago

weierophinney commented 4 years ago

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 https://github.com/zendframework/zend-diactoros/issues/365

Xerkus commented 1 year ago

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.