Skip to content

Conversation

@lyrixx
Copy link
Contributor

@lyrixx lyrixx commented May 6, 2025

Q A
Branch? main
Tickets
License MIT
Doc PR

I have a use case where I need to send a "code" (2FA code) when deleting a resource.

With this PR, I'm able to send the code along with a DELETE request:

curl --request DELETE \
  --url https://redirection-io.test/app_dev.php/api/users/424e19b0-0db9-4595-bc43-f03632b3fe65/two-factor/devices/4d9853c3-f53c-422d-80cd-6c4c4fa64650 \
  --header 'Authorization: Bearer .....'
  --header 'Content-Type: application/json' \
  --header 'accept: application/ld+json' \
  --data '{
    "code": "610432"
}'

I use a custom DTO to do so:

#[ApiResource(
    operations: [
        new Delete(
            uriTemplate: '/users/{id}/two-factor/devices/{deviceId}',
            provider: RemoveDeviceProvider::class,
            input: RemoveDeviceInput::class,
            deserialize: true, // We force since it's delete
            validate: true, // We force since it's delete
            processor: RemoveDeviceProcessor::class,
            // More properties, but not relevant here
        ),
    ]
)]
final class RemoveDevice
{
}

The input:

#[AssertTwoFactorCode(withDeviceId: true)]
final class RemoveDeviceInput
{
    #[Assert\NotBlank]
    public string $code;

    public function __construct(
        #[Ignore]
        #[Assert\NotBlank]
        public ?User $user,
        #[Ignore]
        #[Assert\NotBlank]
        public string $deviceId,
    ) {
    }
}

with a custom provider:

class RemoveDeviceProvider implements ProviderInterface
{
    public function __construct(
        private UserRepository $userRepository,
    ) {
    }

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): RemoveDeviceInput
    {
        return new RemoveDeviceInput(
            user: $this->userRepository->find($uriVariables['id']),
            deviceId: $uriVariables['deviceId'],
        );
    }
}

So I want APIP to create the RemoveDeviceInput with the RemoveDeviceProvider (it's works)
But then I want it to hydrates this object from the request body (it's not, this PR fix that)

@soyuka
Copy link
Member

soyuka commented May 6, 2025

let's go with #7124 which is now a mix between the two solutions

@soyuka soyuka closed this May 6, 2025
@lyrixx lyrixx deleted the main branch May 6, 2025 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants