Skip to content

Commit ccc7480

Browse files
authored
Merge pull request #18 from mlanin/bugfix/empty-array
Fix responses with empty objects
2 parents d6f9a0a + de73e5c commit ccc7480

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/Debugger.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected function updateResponse(Request $request, Response $response)
115115
return;
116116
}
117117

118-
$data[$this->responseKey] = $this->storage->getData();
118+
$data->{$this->responseKey} = $this->storage->getData();
119119

120120
$this->setResponseData($response, $data);
121121
}
@@ -129,8 +129,8 @@ protected function updateResponse(Request $request, Response $response)
129129
*/
130130
protected function needToUpdateResponse(Response $response)
131131
{
132-
$isJsonResponse = $response instanceof JsonResponse || $response->headers->contains('content-type',
133-
'application/json');
132+
$isJsonResponse = $response instanceof JsonResponse ||
133+
$response->headers->contains('content-type', 'application/json');
134134

135135
return $isJsonResponse && !$this->storage->isEmpty();
136136
}
@@ -139,28 +139,28 @@ protected function needToUpdateResponse(Response $response)
139139
* Fetches the contents of the response and parses them to an assoc array
140140
*
141141
* @param Response $response
142-
* @return array|bool
142+
* @return object|bool
143143
*/
144144
protected function getResponseData(Response $response)
145145
{
146146
if ($response instanceof JsonResponse) {
147147
/** @var $response JsonResponse */
148-
return $response->getData(true) ?: [];
148+
return $response->getData() ?: new \StdClass();
149149
}
150150

151151
$content = $response->getContent();
152152

153-
return json_decode($content, true) ?: false;
153+
return json_decode($content) ?: false;
154154
}
155155

156156
/**
157157
* Updates the response content
158158
*
159159
* @param Response $response
160-
* @param array $data
160+
* @param object $data
161161
* @return JsonResponse|Response
162162
*/
163-
protected function setResponseData(Response $response, array $data)
163+
protected function setResponseData(Response $response, $data)
164164
{
165165
if ($response instanceof JsonResponse) {
166166
/** @var $response JsonResponse */

tests/DebuggerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,34 @@ public function it_can_show_cache_events()
199199
],
200200
]);
201201
}
202+
203+
/** @test */
204+
public function it_preserves_object()
205+
{
206+
$this->app['router']->get('foo', function () {
207+
return response()->json([
208+
'foo' => 'bar',
209+
'baz' => (object)[],
210+
]);
211+
});
212+
213+
$this->json('get', '/foo')
214+
->assertStatus(200)
215+
->assertSeeText('"baz":{}');
216+
}
217+
218+
/** @test */
219+
public function it_preserves_array()
220+
{
221+
$this->app['router']->get('foo', function () {
222+
return response()->json([
223+
'foo' => 'bar',
224+
'baz' => [],
225+
]);
226+
});
227+
228+
$this->json('get', '/foo')
229+
->assertStatus(200)
230+
->assertSeeText('"baz":[]');
231+
}
202232
}

0 commit comments

Comments
 (0)