Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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);
}
}
19 changes: 19 additions & 0 deletions tests/Functional/InputOutputNameConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
Loading