File tree Expand file tree Collapse file tree 3 files changed +33
-3
lines changed Expand file tree Collapse file tree 3 files changed +33
-3
lines changed Original file line number Diff line number Diff line change 4
4
5
5
use ArrayObject ;
6
6
use HttpAccept \AcceptParser ;
7
+ use JsonException ;
7
8
use Nyholm \Psr7 \Response ;
8
9
use Nyholm \Psr7 \Stream ;
9
10
use Psr \Http \Message \ResponseInterface ;
10
11
use Psr \Http \Message \ServerRequestInterface ;
11
12
use RuntimeException ;
13
+ use Tobyz \JsonApiServer \Exception \Data \InvalidJsonException ;
12
14
use Tobyz \JsonApiServer \Exception \ErrorProvider ;
13
15
use Tobyz \JsonApiServer \Exception \Field \InvalidFieldValueException ;
14
16
use Tobyz \JsonApiServer \Exception \JsonApiErrorsException ;
@@ -122,9 +124,17 @@ public function currentUrl(array $queryParams = []): string
122
124
*/
123
125
public function body (): ?array
124
126
{
125
- return $ this ->body ??=
126
- (array ) $ this ->request ->getParsedBody () ?:
127
- json_decode ($ this ->request ->getBody ()->getContents (), true );
127
+ try {
128
+ return $ this ->body ??=
129
+ (array ) $ this ->request ->getParsedBody () ?:
130
+ json_decode (
131
+ $ this ->request ->getBody ()->getContents (),
132
+ true ,
133
+ flags: JSON_THROW_ON_ERROR ,
134
+ );
135
+ } catch (JsonException $ e ) {
136
+ throw new InvalidJsonException ($ e ->getMessage ());
137
+ }
128
138
}
129
139
130
140
public function id (Resource $ resource , $ model ): string
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Tobyz \JsonApiServer \Exception \Data ;
4
+
5
+ use Tobyz \JsonApiServer \Exception \BadRequestException ;
6
+
7
+ class InvalidJsonException extends BadRequestException
8
+ {
9
+ }
Original file line number Diff line number Diff line change 2
2
3
3
namespace Tobyz \Tests \JsonApiServer \specification ;
4
4
5
+ use Nyholm \Psr7 \Stream ;
5
6
use Tobyz \JsonApiServer \Endpoint \Create ;
6
7
use Tobyz \JsonApiServer \Endpoint \Show ;
7
8
use Tobyz \JsonApiServer \Exception \BadRequestException ;
8
9
use Tobyz \JsonApiServer \Exception \ConflictException ;
10
+ use Tobyz \JsonApiServer \Exception \Data \InvalidJsonException ;
9
11
use Tobyz \JsonApiServer \Exception \ForbiddenException ;
10
12
use Tobyz \JsonApiServer \Exception \ResourceNotFoundException ;
11
13
use Tobyz \JsonApiServer \JsonApi ;
@@ -37,6 +39,15 @@ public function setUp(): void
37
39
$ this ->api ->resource (new MockResource ('pets ' ));
38
40
}
39
41
42
+ public function test_bad_request_error_if_body_is_invalid_json ()
43
+ {
44
+ $ this ->expectException (InvalidJsonException::class);
45
+
46
+ $ this ->api ->handle (
47
+ $ this ->buildRequest ('POST ' , '/users ' )->withBody (Stream::create ('invalid json ' )),
48
+ );
49
+ }
50
+
40
51
public function test_bad_request_error_if_body_does_not_contain_data_type ()
41
52
{
42
53
$ this ->expectException (BadRequestException::class);
You can’t perform that action at this time.
0 commit comments