From 0408c0a4df90155d8fc2037033d27f6b54de70e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Pillevesse?= Date: Fri, 23 Feb 2024 13:18:33 +0100 Subject: [PATCH 1/3] Add MapRequestPayload tips for Enum --- controller.rst | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/controller.rst b/controller.rst index ab14b0552d1..392ed827bee 100644 --- a/controller.rst +++ b/controller.rst @@ -539,6 +539,37 @@ if you want to map a nested array of specific DTOs:: ) {} } +.. tip:: + + If using typed properties with `MapRequestPayload`, it's recommanded to use built-in types (like `int`, `bool`, `string`...) for mapping. Using custom types can expose your application implementation outside with errors during denormalization. + Validating an Enum in your `#[MapRequestPayload]` class should look like this:: + + class LuckyController + { + #[Route('/lucky/number/{max}', name: 'app_lucky_number', methods: ['POST'])] + public function number(#[MapRequestPayload] MyInput $input, int $max): Response + { + // use it like this : $input->myInputAttribute; + } + } + + class MyInput + { + #[Assert\Choice(callback: [MyEnum::class, 'values'])] + public string $myInputAttribute; + } + + enum MyEnum: string + { + case FIRST_CASE = 'first_case'; + case SECOND_CASE = 'second_case'; + + public static function values(): array + { + return array_column(self::cases(), 'value'); + } + } + Managing the Session -------------------- From 29f98a3577b58bd55f31a37a4f3594ac9580a437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Pillevesse?= Date: Tue, 12 Mar 2024 10:02:00 +0100 Subject: [PATCH 2/3] Update controller.rst --- controller.rst | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/controller.rst b/controller.rst index 392ed827bee..87344f54f6d 100644 --- a/controller.rst +++ b/controller.rst @@ -539,10 +539,18 @@ if you want to map a nested array of specific DTOs:: ) {} } -.. tip:: +.. caution:: + + If you're using typed properties with ``MapRequestPayload```, it is + recommended to use built-in types like ``int``, ``bool`` or ``string`` for + mapping. Using custom types could expose your application implementation in + errors during denormalization. For example, validating an enum when using + ``#[MapRequestPayload]`` could look like this:: - If using typed properties with `MapRequestPayload`, it's recommanded to use built-in types (like `int`, `bool`, `string`...) for mapping. Using custom types can expose your application implementation outside with errors during denormalization. - Validating an Enum in your `#[MapRequestPayload]` class should look like this:: + // src/Controller/LuckyController.php + use App\Model\MyInput; + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; class LuckyController { @@ -553,12 +561,14 @@ if you want to map a nested array of specific DTOs:: } } + // src/Model/MyInput.php class MyInput { #[Assert\Choice(callback: [MyEnum::class, 'values'])] public string $myInputAttribute; } + // src/Model/MyEnum.php enum MyEnum: string { case FIRST_CASE = 'first_case'; From c36e058ce66cb2e7a12de5e942c69e673f622a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Pillevesse?= Date: Tue, 12 Mar 2024 11:27:55 +0100 Subject: [PATCH 3/3] Update controller.rst --- controller.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/controller.rst b/controller.rst index 87344f54f6d..255ec6246c3 100644 --- a/controller.rst +++ b/controller.rst @@ -541,9 +541,9 @@ if you want to map a nested array of specific DTOs:: .. caution:: - If you're using typed properties with ``MapRequestPayload```, it is - recommended to use built-in types like ``int``, ``bool`` or ``string`` for - mapping. Using custom types could expose your application implementation in + If you're using typed properties with ``MapRequestPayload```, it is + recommended to use built-in types like ``int``, ``bool`` or ``string`` for + mapping. Using custom types could expose your application implementation in errors during denormalization. For example, validating an enum when using ``#[MapRequestPayload]`` could look like this::