Skip to content

ApiProblemResponse/RenderErrorListener issue while encoding binary data to Json #5

@weierophinney

Description

@weierophinney

Assume displayExceptions is true.

If in the exception stack trace a function argument contains binary data that json_encode could not handle, then json_encode will return false. As a result the response content is empty.

This happens at following lines:
RenderErrorListener.php#L94
ApiProblemResponse.php#L59

This situation can occur in various debugging scenarios, e.g. uploading a file and passing its blob to a function, and it can be pretty difficult to overcome because the empty response.

Proposed solution
A better choice would be to use \Zend\Json\Encoder::encode because it can correctly handle the encoding of binary data that json_encode can not encode.

// Assuming $data['blob'] contains the binary data of a PDF file

json_encode($data); // return false

\Zend\Json\Json::encode($data); // return false if the json extension is available, otherwise \Zend\Json\Encoder::encode is used

\Zend\Json\Encoder::encode($data); // return the encoded json

Originally posted by @leogr at zfcampus/zf-api-problem#29

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions