Skip to content

Fix MultipartDecoder to support more denormalization types #2064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 61 additions & 4 deletions core/file-upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,15 @@ We need to create our own decoder to do it:
<?php
// api/src/Encoder/MultipartDecoder.php

declare(strict_types=1);

namespace App\Encoder;

use Doctrine\Common\Collections\Collection;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use ReflectionClass;
use ReflectionNamedType;

final class MultipartDecoder implements DecoderInterface
{
Expand All @@ -411,23 +416,75 @@ final class MultipartDecoder implements DecoderInterface
{
$request = $this->requestStack->getCurrentRequest();

if (!$request) {
if (!$request || !isset($context['resource_class'])) {
return null;
}

return array_map(static function (string $element) {
// Retrieve the target class from the context
$targetClass = $context['resource_class'];

// Get the expected types for the target entity
$expectedTypes = $this->getPropertyTypes($targetClass);

$arrayMapWithKeys = function (callable $callback, array $array) {
$result = [];

foreach ($array as $key => $value) {
$result[$key] = $callback($value, $key);
}

return $result;
};

$map = $arrayMapWithKeys(static function (string $element, string $key) use ($expectedTypes) {
// Multipart form values will be encoded in JSON.
$decoded = json_decode($element, true);

return \is_array($decoded) ? $decoded : $element;
}, $request->request->all()) + $request->files->all();
if (is_array($decoded)) {
return $decoded;
}

if ('null' == $element) {
return null;
}

if (!isset($expectedTypes[$key])) {
return $element;
}

return match ($expectedTypes[$key]) {
'bool' => filter_var($element, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
'int' => is_numeric($element) ? (int) $element : $element,
'float' => is_numeric($element) ? (float) $element : $element,
Collection::class => '' == $element ? [] : $element,
default => $element,
};
}, $request->request->all());

return $map + $request->files->all();
}

public function supportsDecoding(string $format): bool
{
return self::FORMAT === $format;
}

private function getPropertyTypes(string $className): array
{
$reflectionClass = new ReflectionClass($className);
$types = [];

foreach ($reflectionClass->getProperties() as $property) {
$type = $property->getType();
if ($type instanceof ReflectionNamedType) {
$types[$property->getName()] = $type->getName();
}
}

return $types;
}
}

```

If you're not using `autowiring` and `autoconfiguring`, don't forget to register the service and tag it as `serializer.encoder`.
Expand Down