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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"require-dev": {
"roave/security-advisories": "dev-latest",
"phpunit/phpunit": "^10.0",
"symfony/browser-kit": "^8.0",
"symfony/browser-kit": "^7.4|^8.0",
"friendsofphp/php-cs-fixer": "^3.54"
},
"autoload": {
Expand Down
1 change: 1 addition & 0 deletions config/services/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
service('serializer'),
service('validator')->nullOnInvalid(),
service('translator')->nullOnInvalid(),
service('.inner'),
])
->tag('controller.targeted_value_resolver', ['name' => RequestPayloadArrayResolver::class])
->tag('kernel.event_subscriber')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestPayloadValueResolver;
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
use Symfony\Component\Serializer\Exception\UnsupportedFormatException;
Expand Down Expand Up @@ -57,6 +57,7 @@ public function __construct(
private readonly SerializerInterface&DenormalizerInterface $serializer,
private readonly ?ValidatorInterface $validator = null,
private readonly ?TranslatorInterface $translator = null,
private readonly ?RequestPayloadValueResolver $decorated = null,
) {
}

Expand All @@ -67,7 +68,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
?? null;

if (!$attribute) {
return [];
return $this->decorated?->resolve($request, $argument) ?? [];
}

if ($argument->isVariadic()) {
Expand Down Expand Up @@ -160,13 +161,13 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo
}

$event->setArguments($arguments);

$this->decorated?->onKernelControllerArguments($event);
}

public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER_ARGUMENTS => 'onKernelControllerArguments',
];
return RequestPayloadValueResolver::getSubscribedEvents();
}

private function mapQueryString(Request $request, string $type, MapQueryString $attribute): ?object
Expand Down
19 changes: 19 additions & 0 deletions tests/Functional/App/NativeRequestMapping/Controller/Closed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

/*
* This file is part of OpenSolid package.
*
* (c) Yonel Ceruto <open@yceruto.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OpenSolid\Tests\OpenApiBundle\Functional\App\NativeRequestMapping\Controller;

#[\Attribute(\Attribute::TARGET_METHOD)]
class Closed
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/*
* This file is part of OpenSolid package.
*
* (c) Yonel Ceruto <open@yceruto.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OpenSolid\Tests\OpenApiBundle\Functional\App\NativeRequestMapping\Controller;

use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class ClosedListener
{
public function onClosed(): void
{
throw new AccessDeniedHttpException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

/*
* This file is part of OpenSolid package.
*
* (c) Yonel Ceruto <open@yceruto.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OpenSolid\Tests\OpenApiBundle\Functional\App\NativeRequestMapping\Controller;

class ClosedPayload
{
public string $name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/*
* This file is part of OpenSolid package.
*
* (c) Yonel Ceruto <open@yceruto.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OpenSolid\Tests\OpenApiBundle\Functional\App\NativeRequestMapping\Controller;

use OpenSolid\OpenApiBundle\Attribute\Payload;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class ClosedPayloadAction
{
#[Route('/closed', methods: 'POST')]
#[Closed]
public function __invoke(#[Payload] ClosedPayload $payload): Response
{
return new Response($payload->name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/*
* This file is part of OpenSolid package.
*
* (c) Yonel Ceruto <open@yceruto.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OpenSolid\Tests\OpenApiBundle\Functional\App\NativeRequestMapping\Controller;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile;
use Symfony\Component\Routing\Attribute\Route;

class UploadFileAction
{
#[Route('/upload', methods: 'POST')]
public function __invoke(#[MapUploadedFile] UploadedFile $file): Response
{
return new Response($file->getClientOriginalName());
}
}
7 changes: 7 additions & 0 deletions tests/Functional/App/NativeRequestMapping/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
imports:
- { resource: ../config.yaml }

services:
OpenSolid\Tests\OpenApiBundle\Functional\App\NativeRequestMapping\Controller\ClosedListener:
tags:
- { name: kernel.event_listener, event: 'kernel.controller_arguments.OpenSolid\Tests\OpenApiBundle\Functional\App\NativeRequestMapping\Controller\Closed', method: onClosed }
5 changes: 5 additions & 0 deletions tests/Functional/App/NativeRequestMapping/routes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
controllers:
resource:
path: ./Controller/
namespace: OpenSolid\Tests\OpenApiBundle\Functional\App\NativeRequestMapping\Controller
type: attribute
41 changes: 41 additions & 0 deletions tests/Functional/NativeRequestMappingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* This file is part of OpenSolid package.
*
* (c) Yonel Ceruto <open@yceruto.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OpenSolid\Tests\OpenApiBundle\Functional;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpKernel\EventListener\ControllerAttributesListener;

class NativeRequestMappingTest extends AbstractWebTestCase
{
public function testMapUploadedFile(): void
{
$client = self::createClient();
$client->request('POST', '/upload', [], ['file' => new UploadedFile(__FILE__, 'upload.txt', 'text/plain', null, true)]);

self::assertResponseIsSuccessful();
$this->assertSame('upload.txt', $client->getResponse()->getContent());
}

public function testControllerAttributesRunBeforeThePayloadIsMapped(): void
{
if (!class_exists(ControllerAttributesListener::class)) {
$this->markTestSkipped('Controller attribute events require Symfony 8.1 or later.');
}

$client = self::createClient();
$client->request('POST', '/closed', [], [], ['CONTENT_TYPE' => 'application/json'], '{"name": ');

self::assertResponseStatusCodeSame(403);
}
}
Loading