Skip to content

Commit 19efc16

Browse files
soyukaclaude
andcommitted
test(serializer): cover controller use case for name converter on input DTOs
| Q | A | ------------- | --- | Branch? | fix/controller-output | Tickets | #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 <noreply@anthropic.com>
1 parent 191a461 commit 19efc16

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

tests/Fixtures/TestBundle/ApiResource/DummyDtoNameConverted.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use ApiPlatform\Metadata\Get;
1818
use ApiPlatform\Metadata\Operation;
1919
use ApiPlatform\Metadata\Post;
20+
use ApiPlatform\Tests\Fixtures\TestBundle\Controller\InputDtoWithNameConverterController;
2021
use ApiPlatform\Tests\Fixtures\TestBundle\Dto\InputDtoWithNameConverter;
2122
use ApiPlatform\Tests\Fixtures\TestBundle\Dto\OutputDtoWithNameConverter;
2223

@@ -33,6 +34,14 @@
3334
processor: [self::class, 'process'],
3435
provider: [self::class, 'provide'],
3536
),
37+
new Post(
38+
uriTemplate: '/dummy_dto_name_converted_controller',
39+
controller: InputDtoWithNameConverterController::class,
40+
input: InputDtoWithNameConverter::class,
41+
output: InputDtoWithNameConverter::class,
42+
read: false,
43+
write: false,
44+
),
3645
]
3746
)]
3847
class DummyDtoNameConverted
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Controller;
15+
16+
use ApiPlatform\Tests\Fixtures\TestBundle\Dto\InputDtoWithNameConverter;
17+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
18+
use Symfony\Component\HttpFoundation\JsonResponse;
19+
use Symfony\Component\HttpFoundation\Request;
20+
use Symfony\Component\HttpFoundation\Response;
21+
use Symfony\Component\Serializer\SerializerInterface;
22+
23+
/**
24+
* Reproduces the controller use case from issue #7705:
25+
* the name converter must be applied when deserializing input DTOs via SerializerInterface.
26+
*/
27+
class InputDtoWithNameConverterController extends AbstractController
28+
{
29+
public function __construct(
30+
private readonly SerializerInterface $serializer,
31+
) {
32+
}
33+
34+
public function __invoke(Request $request): Response
35+
{
36+
$input = $this->serializer->deserialize($request->getContent(), InputDtoWithNameConverter::class, 'json');
37+
38+
return new JsonResponse(['name_converted' => $input->nameConverted]);
39+
}
40+
}

tests/Functional/InputOutputNameConverterTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,23 @@ public function testOutputDtoNameConverterIsApplied(): void
6363
$this->assertArrayHasKey('name_converted', $data);
6464
$this->assertSame('converted', $data['name_converted']);
6565
}
66+
67+
/**
68+
* Reproduces the controller use case from issue #7705:
69+
* when a custom controller deserializes the input DTO via SerializerInterface,
70+
* the API Platform name converter must still be applied.
71+
*
72+
* @see https://github.com/api-platform/core/issues/7705
73+
*/
74+
public function testInputDtoNameConverterIsAppliedWithController(): void
75+
{
76+
$response = self::createClient()->request('POST', '/dummy_dto_name_converted_controller', [
77+
'headers' => ['Content-Type' => 'application/ld+json'],
78+
'json' => ['name_converted' => 'converted'],
79+
]);
80+
81+
$this->assertResponseStatusCodeSame(200);
82+
$data = $response->toArray(false);
83+
$this->assertSame('converted', $data['name_converted']);
84+
}
6685
}

0 commit comments

Comments
 (0)