Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Data Transfer Object classes generation from swagger spec

## [0.0.8] - 2020-06-25
### Added
- fig/http-message-util package dependency (StatusCodeInterface is using)

### Fixed
- Respond with 400 instead 500 on invalid json, handle it with BodyParser middleware
- Set type in DTO\AbstractResourceObject

## [0.0.7] - 2020-06-12
### Added
- Relationships support in DTO classes
Expand Down Expand Up @@ -54,7 +62,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- FastRoute Dispatcher generation from swagger operationIds

[Unreleased]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.7...HEAD
[Unreleased]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.8...HEAD
[0.0.8]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.7...0.0.8
[0.0.7]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.6...0.0.7
[0.0.6]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.5...0.0.6
[0.0.5]: https://github.com/FreeElephants/json-api-php-toolkit/compare/0.0.4...0.0.5
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ext-intl": "*",
"ext-json": "*",
"cebe/php-openapi": "^1.4",
"fig/http-message-util": "^1.1",
"free-elephants/i18n": "^0.0.1",
"laminas/laminas-stratigility": "^3.2",
"neomerx/json-api": "^4.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AbstractResourceObject
public function __construct(array $data)
{
$this->id = $data['id'];
$this->type = $data['type'];

$concreteClass = new \ReflectionClass($this);

Expand Down
22 changes: 21 additions & 1 deletion src/FreeElephants/JsonApiToolkit/Middleware/BodyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@

namespace FreeElephants\JsonApiToolkit\Middleware;

use Fig\Http\Message\StatusCodeInterface;
use FreeElephants\JsonApiToolkit\Psr\JsonApiResponseFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class BodyParser implements MiddlewareInterface
{
private JsonApiResponseFactory $responseFactory;

public function __construct(JsonApiResponseFactory $responseFactory)
{
$this->responseFactory = $responseFactory;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$request->getBody()->rewind();
$request = $request->withParsedBody(json_decode($request->getBody()->getContents(), true));

$decodedBody = json_decode($request->getBody()->getContents(), true);

if ($decodedBody === null && json_last_error() !== JSON_ERROR_NONE) {
return $this->responseFactory->createSingleErrorResponse(
'Provided json is invalid',
StatusCodeInterface::STATUS_BAD_REQUEST,
$request
);
}

$request = $request->withParsedBody($decodedBody);

return $handler->handle($request);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/FreeElephants/JsonApiToolkit/AbstractHttpTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace FreeElephants\JsonApiToolkit;

use FreeElephants\JsonApiToolkit\Psr\ErrorFactory;
use FreeElephants\JsonApiToolkit\Psr\JsonApiResponseFactory;
use Helmich\Psr7Assert\Psr7Assertions;
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\ServerRequest;
use Psr\Http\Message\ResponseFactoryInterface;
Expand Down Expand Up @@ -42,4 +45,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface
}
};
}


protected function createJsonApiResponseFactory(): JsonApiResponseFactory
{
return new JsonApiResponseFactory($this->createMock(EncoderInterface::class), $this, $this->createMock(ErrorFactory::class));
}
}
1 change: 1 addition & 0 deletions tests/FreeElephants/JsonApiToolkit/DTO/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function testFromRequest()

$this->assertInstanceOf(FooResource::class, $fooDTO->data);
$this->assertInstanceOf(FooAttributes::class, $fooDTO->data->attributes);
$this->assertSame('foo', $fooDTO->data->type);
$this->assertSame('bar', $fooDTO->data->attributes->foo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace FreeElephants\JsonApiToolkit\Middleware\Auth;

use FreeElephants\JsonApiToolkit\AbstractHttpTestCase;
use FreeElephants\JsonApiToolkit\Psr\ErrorFactory;
use FreeElephants\JsonApiToolkit\Psr\JsonApiResponseFactory;
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
use Psr\Http\Message\ServerRequestInterface;

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

return $policy;
}

private function createJsonApiResponseFactory(): JsonApiResponseFactory
{
return new JsonApiResponseFactory($this->createMock(EncoderInterface::class), $this, $this->createMock(ErrorFactory::class));
}
}

This file was deleted.

53 changes: 43 additions & 10 deletions tests/FreeElephants/JsonApiToolkit/Middleware/BodyParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

namespace FreeElephants\JsonApiToolkit\Middleware;

use Fig\Http\Message\StatusCodeInterface;
use FreeElephants\JsonApiToolkit\AbstractHttpTestCase;
use Psr\Http\Message\ServerRequestInterface;

class BodyParserTest extends AbstractHttpTestCase
{

public function testProcess()
{
$request = $this->createServerRequest('POST', '/foo');
$request->getBody()->write(<<<JSON
$request->getBody()->write(
<<<JSON
{
"data": {
"id": "foo",
Expand All @@ -19,18 +22,48 @@ public function testProcess()
}
JSON
);
$handler = $this->createRequestHandlerWithAssertions(function (ServerRequestInterface $request) {
$this->assertSame([
'data' => [
'id' => 'foo',
'type' => 'bar',
],
], $request->getParsedBody());
$handler = $this->createRequestHandlerWithAssertions(
function (ServerRequestInterface $request) {
$this->assertSame(
[
'data' => [
'id' => 'foo',
'type' => 'bar',
],
],
$request->getParsedBody()
);

return $this->createResponse();
}
);

$bodyParser = new BodyParser($this->createJsonApiResponseFactory());
$response = $bodyParser->process($request, $handler);

$this->assertResponseHasStatus($response, StatusCodeInterface::STATUS_OK);
}

public function testInvalidBody()
{
$request = $this->createServerRequest('POST', '/foo');
$request->getBody()->write(
<<<JSON
{
"data": {
"id": "foo",
"type": "bar"
}
JSON
);
$handler = $this->createRequestHandlerWithAssertions(function () {
return $this->createResponse();
});

$bodyParser = new BodyParser();
$bodyParser->process($request, $handler);
$bodyParser = new BodyParser($this->createJsonApiResponseFactory());
$response = $bodyParser->process($request, $handler);

$this->assertResponseHasStatus($response, StatusCodeInterface::STATUS_BAD_REQUEST);
}

}