[MapRequestPayload] Fail because property documentation #58742
-
Symfony version(s) affected6.4 DescriptionI was using Everything worked fine until PHPStan prompted me to describe the array property. After adding the annotation Interestingly, replacing How to reproduce<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
#[Route('bug')]
class BugController extends AbstractController
{
public function __invoke(
#[MapRequestPayload] BugDTO $dto
): JsonResponse {
return $this->json($dto);
}
}
class BugDTO
{
/** @var string[] The culprit is here */
public readonly array $items;
public function __construct(
string $items
) {
$this->items = explode(',', $items);
}
} Possible SolutionNo response Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Greetings! <?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
#[Route('bug')]
class BugController extends AbstractController
{
public function __invoke(
#[MapRequestPayload] BugDTO $dto
): JsonResponse {
return $this->json($dto);
}
}
class BugDTO
{
/** @var string[] The culprit is here */
public readonly array $list;
public function __construct(
string $items
) {
$this->list = explode(',', $items);
}
} So the simplest fix is change property name, at least to make your code works |
Beta Was this translation helpful? Give feedback.
-
Hello, thank you for your response! You’re right; that’s also a solution I tested. However, as I mentioned in my first message, I prefer using Since this behavior didn’t seem normal to me, I decided to open an issue about it. If it isn’t resolved, this issue can at least serve as a reference for other developers facing the same problem. Best regards. |
Beta Was this translation helpful? Give feedback.
-
I also encountered a similar issue (Symfony 7.2), this doesn't work and throws a
while according to the docs it should work. Unless I'm missing something. |
Beta Was this translation helpful? Give feedback.
Greetings!
It seems like when normalizer parse DTO name overlapping occurs and produce this undefined behavior
You have constructor param and property with same name but different types and normalizer look at property type definition then constructor param
If change the property name everything works fine
for example