Home > Software engineering >  Api-platform 2.7 - Can't use custom state provider to customize POST response
Api-platform 2.7 - Can't use custom state provider to customize POST response

Time:10-02

I'm using api-platform 2.7 to make an API. I like to control what the requests can accept and what the response send back so I use DTO. I try to customize what a POST request will return. I only want to show specific data from my User entity, so I've made an UserPostOutput DTO and an UserPostOutputProvider, but the UserPostOutputProvider is never reached (I put breakpoints and I use xDebug) and the response keep showing me all datas from my User entity instead of the UserPostOutput DTO.

This is my UserPostOutput DTO :

<?php

namespace App\DTO\User;

class UserPostOutput
{
    public int $id;

    public string $email;
}

This is my User entity (I cut all the getters and setters) :

<?php

namespace App\Entity;

use ApiPlatform\Metadata\Post;
use App\DataTransformer\User\UserPostInputProcessor;
use App\DataTransformer\User\UserPostOutputProvider;
use App\DTO\User\UserPostInput;
use App\DTO\User\UserPostOutput;
use App\Repository\UserRepository;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

#[Post(
    input: UserPostInput::class,
    output: UserPostOutput::class,
    provider: UserPostOutputProvider::class,
    processor: UserPostInputProcessor::class,
)]
#[ORM\Entity(repositoryClass: UserRepository::class)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 180, unique: true)]
    private ?string $email = null;

    #[ORM\Column]
    private array $roles = [];

    #[ORM\Column]
    private ?string $password = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $firstName = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $lastName = null;

    #[ORM\Column(length: 255)]
    private ?string $userName = null;

And this is my UserPostOutputProvider :

<?php

namespace App\DataTransformer\User;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\DTO\User\UserPostOutput;

class UserPostOutputProvider implements ProviderInterface
{
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): UserPostOutput
    {
        $userPostOutput = new UserPostOutput();
        $userPostOutput->email = $uriVariables['email'];
        $userPostOutput->id = $uriVariables['id'];

        return $userPostOutput;
    }
}

CodePudding user response:

I was on the same track as you but after some more try an error i now think this is not how its ment to be. I think only Get requests use the provider and Post,Put,Delete use the processor.

If you want to return the UserPostOutput from your Post you can return it from the processor.

I hope this helps, the documentation on Dtos from Apiplatform is a bit short, so I am also still figuring out things.

  • Related