Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/Http/Serializer/DeserializeParameterResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use ReflectionFunctionAbstract;
use ReflectionNamedType;
use Slim\Exception\HttpBadRequestException;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
Expand Down Expand Up @@ -50,10 +52,16 @@ public function getParameters(
PayloadSource::Get => $request->getQueryParams(),
};

$format = match ($attribute->source) {
PayloadSource::Post => JsonEncoder::FORMAT,
PayloadSource::Get => CsvEncoder::FORMAT,
};

try {
$resolvedParameters[$index] = $this->serializer->denormalize(
$data,
$parameterClass,
format: $format,
context: $attribute->context,
);
} catch (NotNormalizableValueException $e) {
Expand Down
9 changes: 8 additions & 1 deletion src/ServiceFactory/SlimServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Slim\Middleware\ErrorMiddleware;
use Slim\Psr7\Factory\ResponseFactory;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Expand All @@ -38,7 +40,12 @@ public function __invoke(ServicesBuilder $builder): iterable {
yield Serializer::class => static fn () => new Serializer([
new ArrayDenormalizer(),
new ObjectNormalizer(
propertyTypeExtractor: new PhpDocExtractor(),
propertyTypeExtractor: new PropertyInfoExtractor(
typeExtractors: [
new PhpDocExtractor(),
new ReflectionExtractor(),
],
),
),
]);
yield DenormalizerInterface::class => get(Serializer::class);
Expand Down
12 changes: 12 additions & 0 deletions tests/Http/Serializer/BooleansInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace WonderNetwork\SlimKernel\Http\Serializer;

final class BooleansInput {
public bool $true;
public bool $false;
public bool $one;
public bool $zero;
}
22 changes: 21 additions & 1 deletion tests/Http/Serializer/DeserializeParameterResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,37 @@ public function testDeserialize(): void {
['name' => 'cats'],
['name' => 'dogs'],
],
'tag' => [
'name' => 'alpha',
],
];
$get = [
'page' => 3,
'lists' => ['alpha', 'bravo'],
'arrays' => ['charlie' => 'delta'],
'booleans' => [
'true' => true,
'false' => false,
'one' => 1,
'zero' => 0,
],
];

$request = (new ServerRequestFactory())
->createServerRequest('GET', '/?'.http_build_query($get))
->withParsedBody($post);
$response = $app->handle($request);

$body = $response->getBody()->getContents();

self::assertSame(
200,
$response->getStatusCode(),
$body,
);

$actual = json_decode(
$response->getBody()->getContents(),
$body,
associative: true,
depth: 12,
flags: JSON_THROW_ON_ERROR,
Expand Down
11 changes: 6 additions & 5 deletions tests/Http/Serializer/EchoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ public function __invoke(
#[Payload(source: PayloadSource::Get)] SampleGetInput $get,
ResponseInterface $response,
): ResponseInterface {
return $response->withBody(
(new StreamFactory())->createStream(
json_encode(compact('post', 'get'), JSON_THROW_ON_ERROR),
),
);
$streamFactory = new StreamFactory();
$payload = compact('post', 'get');
$json = json_encode($payload, JSON_THROW_ON_ERROR);
$body = $streamFactory->createStream($json);

return $response->withBody($body);
}
}
9 changes: 9 additions & 0 deletions tests/Http/Serializer/SampleGetInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@
final class SampleGetInput {
public int $page = 1;
public int $perPage = 100;
/**
* @var string[]
*/
public array $lists;
/**
* @var array<string, string>
*/
public array $arrays;
public BooleansInput $booleans;
}
1 change: 1 addition & 0 deletions tests/Http/Serializer/SamplePostInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ final class SamplePostInput {
public int $value;
/** @var TagInput[] */
public array $tags;
public TagInput $tag;
}