Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MapRequestPayload with PUT and PATCH requests #58724

@marcelowzd

Description

@marcelowzd

Symfony version(s) affected

7.1.5

Description

MapRequestPayload seems to work only with enctype=JSON when using a PUT or PATCH route

Edit

The problem also seems to happen when using MapUploadedFile in a PATCH route.

How to reproduce

BusinessDTO

readonly class BusinessDTO {
  public function __construct(
    public string $name
  ){}
}

Route

#[Route(path: "/business", name: "api-business", methods: ["PUT"], format:"json")]
public function create(  #[MapRequestPayload] BusinessDTO $b ): JsonResponse {
  return $this->json(["ok" => "ok"];
}

If i make a request using PUT and enctype=multipart/form-data, symfony can't create the DTO and immediately return error 400, but if i only change the route to POST, it works as expected.
Also, if i change the enctype to application/json but still uses PUT, it also works as expected.

Edit

The problem also seems to happen with MapUploadedFile. When using a route with PATCH and one attribute with MapUploadedFile, the same behavior from MapRequestPayload applies: using POST instead of PATCH resolves the issue.

Error returned

Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\BadRequestHttpException: "Request payload contains invalid "form" data." at RequestPayloadValueResolver.php line 221

Edit

Error when using MapUploadedFile and PATCH route together
Uncaught PHP Exception TypeError: "App\Controller\BusinessController::updateFile(): Argument #2 ($logo) must be of type Symfony\Component\HttpFoundation\File\UploadedFile, array given, (HttpKernel.php)

Possible Solution

From what i was investigating, it has something to do with the following lines inside RequestPayloadValueResolver.php

if ($data = $request->request->all()) {
  return $this->serializer->denormalize($data, $type, null, $attribute->serializationContext + self::CONTEXT_DENORMALIZE + 
     ('form' === $format ? ['filter_bool' => true] : []));
}

In the case of a PUT request, the $request->request->all() is empty, but when using POST it has the post data.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions