8000 [Bugfix] Prevent fatal error when resolving without inbound request · startupengine/laravel-json-api@54e06bc · GitHub
[go: up one dir, main page]

Skip to content

Commit 54e06bc

Browse files
committed
[Bugfix] Prevent fatal error when resolving without inbound request
When resolving JSON API document objects out of the service container when there was no inbound JSON API request, a fatal error would occur. This commit fixes that and throws a runtime exception instead. Fixes cloudcreativity#88
1 parent d262adc commit 54e06bc

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
All notable changes to this project will be documented in this file. This project adheres to
33
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
44

5+
## [0.10.1] - 2017-08-15
6+
7+
### Fixed
8+
- [#88] Fixed fatal error caused when resolving request objects out of the service container when there was no
9+
inbound request bound in the service container.
10+
511
## [0.10.0] - 2017-07-29
612

713
### Added

src/ServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,11 @@ protected function bindInboundRequest()
215215
});
216216

217217
$this->app->bind(DocumentInterface::class, function () {
218-
if (!$document = json_api_request()->getDocument()) {
218+
if (!$request = json_api_request()) {
219+
throw new RuntimeException('No inbound JSON API request.');
220+
}
221+
222+
if (!$document = $request->getDocument()) {
219223
throw new RuntimeException('No request document on inbound JSON API request.');
220224
}
221225

src/Services/JsonApiService.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ public function request()
8383
return $this->container->make('json-api.request');
8484
}
8585

86+
/**
87+
* @return RequestInterface
88+
*/
89+
public function requestOrFail()
90+
{
91+
if (!$request = $this->request()) {
92+
throw new RuntimeException('No inbound JSON API request.');
93+
}
94+
95+
return $request;
96+
}
97+
8698
/**
8799
* Get the API that is handling the inbound HTTP request.
88100
*

tests/Integration/ServicesTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace CloudCreativity\LaravelJsonApi\Tests\Integration;
4+
5+
use CloudCreativity\JsonApi\Contracts\Object\DocumentInterface;
6+
use CloudCreativity\JsonApi\Contracts\Object\RelationshipInterface;
7+
use CloudCreativity\JsonApi\Contracts\Object\ResourceObjectInterface;
8+
use CloudCreativity\JsonApi\Exceptions\RuntimeException;
9+
10+
class ServicesTest extends TestCase
11+
{
12+
13+
/**
14+
* @see Issue 88
15+
*/
16+
public function testDocumentWithoutInboundRequest()
17+
{
18+
$this->expectException(RuntimeException::class);
19+
app(DocumentInterface::class);
20+
}
21+
22+
public function testResourceObjectWithoutInboundRequest()
23+
{
24+
$this->expectException(RuntimeException::class);
25+
app(ResourceObjectInterface::class);
26+
}
27+
28+
public function testRelationshipObjectWithoutInboundRequest()
29+
{
30+
$this->expectException(RuntimeException::class);
31+
app(RelationshipInterface::class);
32+
}
33+
}

0 commit comments

Comments
 (0)
0