As stated in the general design considerations, in most cases the DTO pattern should be implemented using an API Resource class representing the public data model exposed through the API and a custom State Provider. In such cases, the class marked with #[ApiResource]
will act as a DTO.
However, it’s sometimes useful to use a specific class to represent the input or output data structure related to an operation. These techniques are very useful to document your API properly (using Hydra or OpenAPI) and will often be used on POST
operations.
Using an input, the request body will be denormalized to the input instead of your resource class.
<?php
// api/src/Dto/UserResetPasswordDto.php
namespace App\Dto;
use Symfony\Component\Validator\Constraints as Assert;
final class UserResetPasswordDto
{
#[Assert\Email]
public $email;
}
<?php
// api/src/Model/User.php
namespace App\Model;
use ApiPlatform\Metadata\Post;
use App\Dto\UserResetPasswordDto;
use App\State\UserResetPasswordProcessor;
#[Post(input: UserResetPasswordDto::class, processor: UserResetPasswordProcessor::class)]
final class User {}
And the processor:
<?php
namespace App\State;
use App\Dto\UserResetPasswordDto;
use ApiPlatform\State\ProcessorInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
final class UserResetPasswordProcessor implements ProcessorInterface
{
/**
* @param UserResetPasswordDto $data
*/
public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
{
if ('[email protected]' === $data->email) {
return new User(email: $data->email, id: 1);
}
throw new NotFoundHttpException();
}
}
In some cases, using an input DTO is a way to avoid serialization groups.
Let’s use a message that will be processed by Symfony Messenger. API Platform has an integration with messenger, to use a DTO as input you need to specify the input
attribute:
<?php
namespace App\Model;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Symfony\Messenger\Processor as MessengerProcessor;
use App\Dto\Message;
#[Post(input: Message::class, processor: MessengerProcessor::class)]
class SendMessage {}
This will dispatch the App\Dto\Message
via Symfony Messenger.
To return another representation of your data in a State Provider we advise to specify the output
attribute of the resource. Note that this technique works without any changes to the resource but your API documentation would be wrong.
<?php
namespace App\Entity;
use ApiPlatform\Metadata\Get;
use App\Dto\AnotherRepresentation;
use App\State\BookRepresentationProvider;
#[Get(output: AnotherRepresentation::class, provider: BookRepresentationProvider::class)]
class Book {}
<?php
namespace App\State;
use App\Dto\AnotherRepresentation;
use App\Model\Book;
use ApiPlatform\State\ProviderInterface;
final class BookRepresentationProvider implements ProviderInterface
{
public function provide(Operation $operation, array $uriVariables = [], array $context = [])
{
return new AnotherRepresentation();
}
}
Made with love by
Les-Tilleuls.coop can help you design and develop your APIs and web projects, and train your teams in API Platform, Symfony, Next.js, Kubernetes and a wide range of other technologies.
Learn more