Skip to content

Commit

Permalink
Middleware: ResponseJsonNested and ResponseJsonFlat implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
charjr committed Dec 15, 2022
1 parent dfc5e31 commit 394a392
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 50 deletions.
5 changes: 0 additions & 5 deletions src/Middleware/RequestValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Closure;
use Illuminate\Http\Request;
use Membrane\Laravel\Http\Request as MembraneHttpRequest;
use Membrane\Laravel\Http\Response as MembraneHttpResponse;
use Membrane\Laravel\ToPsr7;
use Membrane\Membrane;
use Membrane\OpenAPI\Specification\Request as MembraneRequestSpec;
Expand All @@ -33,10 +32,6 @@ public function handle(Request $request, Closure $next): SymfonyResponse

$result = $this->membrane->process($psr7Request, $specification);

if (!$result->isValid()) {
return new MembraneHttpResponse(status: 400, result: $result);
}

$membraneRequest = MembraneHttpRequest::createFromResult($result, $request);

return $next($membraneRequest);
Expand Down
28 changes: 28 additions & 0 deletions src/Middleware/ResponseJsonFlat.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,35 @@

namespace Membrane\Laravel\Middleware;

use Closure;
use Crell\ApiProblem\ApiProblem;
use Crell\ApiProblem\HttpConverter;
use Membrane\Laravel\Http\Request as MembraneHttpRequest;
use Membrane\Laravel\ToSymfony;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

class ResponseJsonFlat
{
public function handle(MembraneHttpRequest $request, Closure $next): SymfonyResponse
{
$result = $request->getResult();

if (!$result->isValid()) {
$renderer = new JsonFlat($result);

$problem = (new ApiProblem('Request payload failed validation'))
->setStatus(400);
$problem['errors'] = $renderer->jsonSerialize();

$factory = new Psr17Factory();
$converter = new HttpConverter($factory);
$response = $converter->toJsonResponse($problem);
$toSymfony = new ToSymfony();

return $toSymfony($response);
}

return $next($request);
}
}
28 changes: 28 additions & 0 deletions src/Middleware/ResponseJsonNested.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,35 @@

namespace Membrane\Laravel\Middleware;

use Closure;
use Crell\ApiProblem\ApiProblem;
use Crell\ApiProblem\HttpConverter;
use Membrane\Laravel\Http\Request as MembraneHttpRequest;
use Membrane\Laravel\ToSymfony;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

class ResponseJsonNested
{
public function handle(MembraneHttpRequest $request, Closure $next): SymfonyResponse
{
$result = $request->getResult();

if (!$result->isValid()) {
$renderer = new JsonNested($result);

$problem = (new ApiProblem('Request payload failed validation'))
->setStatus(400);
$problem['errors'] = $renderer->jsonSerialize();

$factory = new Psr17Factory();
$converter = new HttpConverter($factory);
$response = $converter->toJsonResponse($problem);
$toSymfony = new ToSymfony();

return $toSymfony($response);
}

return $next($request);
}
}
58 changes: 13 additions & 45 deletions tests/Middleware/RequestValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use Illuminate\Http\Request as IlluminateRequest;
use Membrane\Laravel\Http\Response as MembraneResponse;
use Membrane\Result\FieldName;
use Membrane\Result\Message;
use Membrane\Result\MessageSet;
use Membrane\Result\Result;
use PHPUnit\Framework\TestCase;

Expand All @@ -20,52 +17,23 @@
*/
class RequestValidationTest extends TestCase
{
public function dataSetsToHandle(): array
/** @test */
public function handleTest(): void
{
$expected = new MembraneResponse(
result: Result::valid([
'path' => [],
'query' => [],
'header' => [],
'cookie' => [],
'body' => '',
])
);
$api = __DIR__ . '/../fixtures/petstore-expanded.json';
return [
[
$api,
IlluminateRequest::create('/pets'),
new MembraneResponse(result: Result::valid(1)),
],
[
$api,
IlluminateRequest::create('/pets?tags[]=Ben'),
new MembraneResponse(result: Result::valid(1)),
],
[
$api,
IlluminateRequest::create('/pets?tags=Ben'),
new MembraneResponse(
status: 400,
result: Result::invalid(
[
'path' => [],
'query' => ['tags' => 'Ben'],
'header' => [],
'cookie' => [],
'body' => '',
],
new MessageSet(
new FieldName('', '', 'query', 'tags'),
new Message('IsList validator expects list value, %s passed instead', ['string'])
)
)
),
],
];
}

/**
* @test
* @dataProvider dataSetsToHandle
*/
public function handleTest(string $api, IlluminateRequest $request, MembraneResponse $expected): void
{
$sut = new RequestValidation($api);
$request = IlluminateRequest::create('/pets');

$actual = $sut->handle($request, fn($var) => new MembraneResponse(status: 200, result: $expected->result));
$actual = $sut->handle($request, fn($var) => new MembraneResponse(status: 200, result: $var->getResult()));

self::assertEquals($expected, $actual);
}
Expand Down

0 comments on commit 394a392

Please sign in to comment.