From 71a1bf48ffd938b92db0f60b7c8dd5b50b271d7b Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 20 Feb 2026 11:40:09 +0100 Subject: [PATCH] test(serializer): cover controller use case for name converter on input DTOs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit | Q | A | ------------- | --- | Branch? | 4.2 | Tickets | https://github.com/api-platform/core/issues/7705 | License | MIT | Doc PR | ∅ Ensures the API Platform name converter is applied when a custom controller deserializes input DTOs via SerializerInterface directly. Co-Authored-By: Claude Opus 4.6 --- .../ApiResource/DummyDtoNameConverted.php | 7 ++++ .../InputDtoWithNameConverterController.php | 40 +++++++++++++++++++ .../InputOutputNameConverterTest.php | 19 +++++++++ 3 files changed, 66 insertions(+) create mode 100644 tests/Fixtures/TestBundle/Controller/InputDtoWithNameConverterController.php diff --git a/tests/Fixtures/TestBundle/ApiResource/DummyDtoNameConverted.php b/tests/Fixtures/TestBundle/ApiResource/DummyDtoNameConverted.php index e0c54b8cecb..cd0ad1f352e 100644 --- a/tests/Fixtures/TestBundle/ApiResource/DummyDtoNameConverted.php +++ b/tests/Fixtures/TestBundle/ApiResource/DummyDtoNameConverted.php @@ -17,6 +17,7 @@ use ApiPlatform\Metadata\Get; use ApiPlatform\Metadata\Operation; use ApiPlatform\Metadata\Post; +use ApiPlatform\Tests\Fixtures\TestBundle\Controller\InputDtoWithNameConverterController; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\InputDtoWithNameConverter; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\OutputDtoWithNameConverter; @@ -33,6 +34,12 @@ processor: [self::class, 'process'], provider: [self::class, 'provide'], ), + new Post( + uriTemplate: '/dummy_dto_name_converted_controller', + controller: InputDtoWithNameConverterController::class, + input: InputDtoWithNameConverter::class, + output: InputDtoWithNameConverter::class, + ), ] )] class DummyDtoNameConverted diff --git a/tests/Fixtures/TestBundle/Controller/InputDtoWithNameConverterController.php b/tests/Fixtures/TestBundle/Controller/InputDtoWithNameConverterController.php new file mode 100644 index 00000000000..c4cd6a30d25 --- /dev/null +++ b/tests/Fixtures/TestBundle/Controller/InputDtoWithNameConverterController.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Tests\Fixtures\TestBundle\Controller; + +use ApiPlatform\Tests\Fixtures\TestBundle\Dto\InputDtoWithNameConverter; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Serializer\SerializerInterface; + +/** + * Reproduces the controller use case from issue #7705: + * the name converter must be applied when deserializing input DTOs via SerializerInterface. + */ +class InputDtoWithNameConverterController extends AbstractController +{ + public function __construct( + private readonly SerializerInterface $serializer, + ) { + } + + public function __invoke(Request $request): Response + { + $input = $this->serializer->deserialize($request->getContent(), InputDtoWithNameConverter::class, 'json'); + + return new JsonResponse($input); + } +} diff --git a/tests/Functional/InputOutputNameConverterTest.php b/tests/Functional/InputOutputNameConverterTest.php index 1126c66135a..bbb5028b7fb 100644 --- a/tests/Functional/InputOutputNameConverterTest.php +++ b/tests/Functional/InputOutputNameConverterTest.php @@ -63,4 +63,23 @@ public function testOutputDtoNameConverterIsApplied(): void $this->assertArrayHasKey('name_converted', $data); $this->assertSame('converted', $data['name_converted']); } + + /** + * Reproduces the controller use case from issue #7705: + * when a custom controller deserializes the input DTO via SerializerInterface, + * the API Platform name converter must still be applied. + * + * @see https://github.com/api-platform/core/issues/7705 + */ + public function testInputDtoNameConverterIsAppliedWithController(): void + { + $response = self::createClient()->request('POST', '/dummy_dto_name_converted_controller', [ + 'headers' => ['Content-Type' => 'application/ld+json'], + 'json' => ['name_converted' => 'converted'], + ]); + + $this->assertResponseStatusCodeSame(200); + $data = $response->toArray(false); + $this->assertSame('converted', $data['nameConverted']); + } }