Skip to content

Commit c377b37

Browse files
authored
Merge pull request #9 from FreeElephants/develop
Develop
2 parents c00bd5f + 545d95e commit c377b37

File tree

9 files changed

+86
-56
lines changed

9 files changed

+86
-56
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
- Data Transfer Object classes generation from swagger spec
1010

11+
## [0.0.8] - 2020-06-25
12+
### Added
13+
- fig/http-message-util package dependency (StatusCodeInterface is using)
14+
15+
### Fixed
16+
- Respond with 400 instead 500 on invalid json, handle it with BodyParser middleware
17+
- Set type in DTO\AbstractResourceObject
18+
1119
## [0.0.7] - 2020-06-12
1220
### Added
1321
- Relationships support in DTO classes
@@ -54,7 +62,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5462
### Added
5563
- FastRoute Dispatcher generation from swagger operationIds
5664

57-
[Unreleased]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.7...HEAD
65+
[Unreleased]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.8...HEAD
66+
[0.0.8]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.7...0.0.8
5867
[0.0.7]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.6...0.0.7
5968
[0.0.6]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.5...0.0.6
6069
[0.0.5]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.4...0.0.5

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"ext-intl": "*",
1616
"ext-json": "*",
1717
"cebe/php-openapi": "^1.4",
18+
"fig/http-message-util": "^1.1",
1819
"free-elephants/i18n": "^0.0.1",
1920
"laminas/laminas-stratigility": "^3.2",
2021
"neomerx/json-api": "^4.0",

src/FreeElephants/JsonApiToolkit/DTO/AbstractResourceObject.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class AbstractResourceObject
1414
public function __construct(array $data)
1515
{
1616
$this->id = $data['id'];
17+
$this->type = $data['type'];
1718

1819
$concreteClass = new \ReflectionClass($this);
1920

src/FreeElephants/JsonApiToolkit/Middleware/BodyParser.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,37 @@
22

33
namespace FreeElephants\JsonApiToolkit\Middleware;
44

5+
use Fig\Http\Message\StatusCodeInterface;
6+
use FreeElephants\JsonApiToolkit\Psr\JsonApiResponseFactory;
57
use Psr\Http\Message\ResponseInterface;
68
use Psr\Http\Message\ServerRequestInterface;
79
use Psr\Http\Server\MiddlewareInterface;
810
use Psr\Http\Server\RequestHandlerInterface;
911

1012
class BodyParser implements MiddlewareInterface
1113
{
14+
private JsonApiResponseFactory $responseFactory;
15+
16+
public function __construct(JsonApiResponseFactory $responseFactory)
17+
{
18+
$this->responseFactory = $responseFactory;
19+
}
20+
1221
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
1322
{
1423
$request->getBody()->rewind();
15-
$request = $request->withParsedBody(json_decode($request->getBody()->getContents(), true));
24+
25+
$decodedBody = json_decode($request->getBody()->getContents(), true);
26+
27+
if ($decodedBody === null && json_last_error() !== JSON_ERROR_NONE) {
28+
return $this->responseFactory->createSingleErrorResponse(
29+
'Provided json is invalid',
30+
StatusCodeInterface::STATUS_BAD_REQUEST,
31+
$request
32+
);
33+
}
34+
35+
$request = $request->withParsedBody($decodedBody);
1636

1737
return $handler->handle($request);
1838
}

tests/FreeElephants/JsonApiToolkit/AbstractHttpTestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace FreeElephants\JsonApiToolkit;
44

5+
use FreeElephants\JsonApiToolkit\Psr\ErrorFactory;
6+
use FreeElephants\JsonApiToolkit\Psr\JsonApiResponseFactory;
57
use Helmich\Psr7Assert\Psr7Assertions;
8+
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
69
use Nyholm\Psr7\Response;
710
use Nyholm\Psr7\ServerRequest;
811
use Psr\Http\Message\ResponseFactoryInterface;
@@ -42,4 +45,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface
4245
}
4346
};
4447
}
48+
49+
50+
protected function createJsonApiResponseFactory(): JsonApiResponseFactory
51+
{
52+
return new JsonApiResponseFactory($this->createMock(EncoderInterface::class), $this, $this->createMock(ErrorFactory::class));
53+
}
4554
}

tests/FreeElephants/JsonApiToolkit/DTO/DocumentTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function testFromRequest()
2828

2929
$this->assertInstanceOf(FooResource::class, $fooDTO->data);
3030
$this->assertInstanceOf(FooAttributes::class, $fooDTO->data->attributes);
31+
$this->assertSame('foo', $fooDTO->data->type);
3132
$this->assertSame('bar', $fooDTO->data->attributes->foo);
3233
}
3334
}

tests/FreeElephants/JsonApiToolkit/Middleware/Auth/AuthorizationTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
namespace FreeElephants\JsonApiToolkit\Middleware\Auth;
44

55
use FreeElephants\JsonApiToolkit\AbstractHttpTestCase;
6-
use FreeElephants\JsonApiToolkit\Psr\ErrorFactory;
7-
use FreeElephants\JsonApiToolkit\Psr\JsonApiResponseFactory;
8-
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
96
use Psr\Http\Message\ServerRequestInterface;
107

118
class AuthorizationTest extends AbstractHttpTestCase
@@ -62,9 +59,4 @@ private function createPolicyMock(int $result = PolicyInterface::RESULT_ALLOW)
6259

6360
return $policy;
6461
}
65-
66-
private function createJsonApiResponseFactory(): JsonApiResponseFactory
67-
{
68-
return new JsonApiResponseFactory($this->createMock(EncoderInterface::class), $this, $this->createMock(ErrorFactory::class));
69-
}
7062
}

tests/FreeElephants/JsonApiToolkit/Middleware/BodyParserMiddlewareTest.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/FreeElephants/JsonApiToolkit/Middleware/BodyParserTest.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
namespace FreeElephants\JsonApiToolkit\Middleware;
44

5+
use Fig\Http\Message\StatusCodeInterface;
56
use FreeElephants\JsonApiToolkit\AbstractHttpTestCase;
67
use Psr\Http\Message\ServerRequestInterface;
78

89
class BodyParserTest extends AbstractHttpTestCase
910
{
11+
1012
public function testProcess()
1113
{
1214
$request = $this->createServerRequest('POST', '/foo');
13-
$request->getBody()->write(<<<JSON
15+
$request->getBody()->write(
16+
<<<JSON
1417
{
1518
"data": {
1619
"id": "foo",
@@ -19,18 +22,48 @@ public function testProcess()
1922
}
2023
JSON
2124
);
22-
$handler = $this->createRequestHandlerWithAssertions(function (ServerRequestInterface $request) {
23-
$this->assertSame([
24-
'data' => [
25-
'id' => 'foo',
26-
'type' => 'bar',
27-
],
28-
], $request->getParsedBody());
25+
$handler = $this->createRequestHandlerWithAssertions(
26+
function (ServerRequestInterface $request) {
27+
$this->assertSame(
28+
[
29+
'data' => [
30+
'id' => 'foo',
31+
'type' => 'bar',
32+
],
33+
],
34+
$request->getParsedBody()
35+
);
36+
37+
return $this->createResponse();
38+
}
39+
);
2940

41+
$bodyParser = new BodyParser($this->createJsonApiResponseFactory());
42+
$response = $bodyParser->process($request, $handler);
43+
44+
$this->assertResponseHasStatus($response, StatusCodeInterface::STATUS_OK);
45+
}
46+
47+
public function testInvalidBody()
48+
{
49+
$request = $this->createServerRequest('POST', '/foo');
50+
$request->getBody()->write(
51+
<<<JSON
52+
{
53+
"data": {
54+
"id": "foo",
55+
"type": "bar"
56+
}
57+
JSON
58+
);
59+
$handler = $this->createRequestHandlerWithAssertions(function () {
3060
return $this->createResponse();
3161
});
3262

33-
$bodyParser = new BodyParser();
34-
$bodyParser->process($request, $handler);
63+
$bodyParser = new BodyParser($this->createJsonApiResponseFactory());
64+
$response = $bodyParser->process($request, $handler);
65+
66+
$this->assertResponseHasStatus($response, StatusCodeInterface::STATUS_BAD_REQUEST);
3567
}
68+
3669
}

0 commit comments

Comments
 (0)