Skip to content

Commit

Permalink
add support for array request body
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed Mar 23, 2024
1 parent 63916dc commit c39a083
Show file tree
Hide file tree
Showing 14 changed files with 545 additions and 4 deletions.
18 changes: 15 additions & 3 deletions config/services/controller.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use OpenSolid\OpenApiBundle\Controller\OpenApiController;
use OpenSolid\OpenApiBundle\Generator;
use OpenSolid\OpenApiBundle\HttpKernel\Controller\ArgumentResolver\RequestPayloadArrayResolver;
use OpenSolid\OpenApiBundle\HttpKernel\Controller\ControllerResultSubscriber;
use OpenSolid\OpenApiBundle\HttpKernel\Controller\ValueResolver\ConstraintGuesser\NativeConstraintGuesser;
use OpenSolid\OpenApiBundle\HttpKernel\Controller\ValueResolver\PathValueResolver;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;
Expand All @@ -34,6 +35,17 @@
])
->tag('controller.argument_value_resolver', ['priority' => 110])

->set(RequestPayloadArrayResolver::class)
->decorate('argument_resolver.request_payload')
->args([
service('serializer'),
service('validator')->nullOnInvalid(),
service('translator')->nullOnInvalid(),
])
->tag('controller.targeted_value_resolver', ['name' => RequestPayloadArrayResolver::class])
->tag('kernel.event_subscriber')
->lazy()

->set(NativeConstraintGuesser::class)
->tag('controller.path_constraint_guesser')
;
Expand Down
13 changes: 13 additions & 0 deletions src/Attribute/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

namespace OpenSolid\OpenApiBundle\Attribute;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestPayloadValueResolver;
use Symfony\Component\Validator\Constraints\GroupSequence;

#[\Attribute(\Attribute::TARGET_PARAMETER)]
class Body extends MapRequestPayload
{
public function __construct(
array|string|null $acceptFormat = null,
array $serializationContext = [],
string|GroupSequence|array|null $validationGroups = null,
string $resolver = RequestPayloadValueResolver::class,
int $validationFailedStatusCode = Response::HTTP_UNPROCESSABLE_ENTITY,
public ?string $itemsType = null,
) {
parent::__construct($acceptFormat, $serializationContext, $validationGroups, $resolver, $validationFailedStatusCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

namespace OpenSolid\OpenApiBundle\HttpKernel\Controller\ArgumentResolver;

use OpenSolid\OpenApiBundle\Attribute\Body;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
use Symfony\Component\Serializer\Exception\UnsupportedFormatException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Exception\ValidationFailedException;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class RequestPayloadArrayResolver implements ValueResolverInterface, EventSubscriberInterface
{
/**
* @see \Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT
* @see DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS
*/
private const CONTEXT_DENORMALIZE = [
'disable_type_enforcement' => true,
'collect_denormalization_errors' => true,
];

/**
* @see DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS
*/
private const CONTEXT_DESERIALIZE = [
'collect_denormalization_errors' => true,
];

public function __construct(
private readonly SerializerInterface&DenormalizerInterface $serializer,
private readonly ?ValidatorInterface $validator = null,
private readonly ?TranslatorInterface $translator = null,
) {
}

public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
$attribute = $argument->getAttributesOfType(MapQueryString::class, ArgumentMetadata::IS_INSTANCEOF)[0]
?? $argument->getAttributesOfType(MapRequestPayload::class, ArgumentMetadata::IS_INSTANCEOF)[0]
?? null;

if (!$attribute) {
return [];
}

if ($argument->isVariadic()) {
throw new \LogicException(sprintf('Mapping variadic argument "$%s" is not supported.', $argument->getName()));
}

if ('array' !== $argument->getType()) {
return [$attribute];
}

if ($attribute instanceof Body && null !== $attribute->itemsType) {
$attribute->metadata = new ArgumentMetadata(
$argument->getName(),
$attribute->itemsType.'[]',
$argument->isVariadic(),
$argument->hasDefaultValue(),
$argument->hasDefaultValue() ? $argument->getDefaultValue() : null,
$argument->isNullable(),
$argument->getAttributes(),
);
} else {
$attribute->metadata = $argument;
}

return [$attribute];
}

public function onKernelControllerArguments(ControllerArgumentsEvent $event): void
{
$arguments = $event->getArguments();

foreach ($arguments as $i => $argument) {
if ($argument instanceof MapQueryString) {
$payloadMapper = 'mapQueryString';
$validationFailedCode = $argument->validationFailedStatusCode;
} elseif ($argument instanceof MapRequestPayload) {
$payloadMapper = 'mapRequestPayload';
$validationFailedCode = $argument->validationFailedStatusCode;
} else {
continue;
}
$request = $event->getRequest();

if (!$type = $argument->metadata->getType()) {
throw new \LogicException(sprintf('Could not resolve the "$%s" controller argument: argument should be typed.', $argument->metadata->getName()));
}

if ($this->validator) {
$violations = new ConstraintViolationList();
try {
$payload = $this->$payloadMapper($request, $type, $argument);
} catch (PartialDenormalizationException $e) {
$trans = $this->translator ? $this->translator->trans(...) : fn ($m, $p) => strtr($m, $p);
foreach ($e->getErrors() as $error) {
$parameters = ['{{ type }}' => implode('|', $error->getExpectedTypes())];
if ($error->canUseMessageForUser()) {
$parameters['hint'] = $error->getMessage();
}
$template = 'This value should be of type {{ type }}.';
$message = $trans($template, $parameters, 'validators');
$violations->add(new ConstraintViolation($message, $template, $parameters, null, $error->getPath(), null));
}
$payload = $e->getData();
}

if (null !== $payload && !\count($violations)) {
$violations->addAll($this->validator->validate($payload, null, $argument->validationGroups ?? null));
}

if (\count($violations)) {
throw new HttpException($validationFailedCode, implode("\n", array_map(static fn ($e) => $e->getMessage(), iterator_to_array($violations))), new ValidationFailedException($payload, $violations));
}
} else {
try {
$payload = $this->$payloadMapper($request, $type, $argument);
} catch (PartialDenormalizationException $e) {
throw new HttpException($validationFailedCode, implode("\n", array_map(static fn ($e) => $e->getMessage(), $e->getErrors())), $e);
}
}

if (null === $payload) {
$payload = match (true) {
$argument->metadata->hasDefaultValue() => $argument->metadata->getDefaultValue(),
$argument->metadata->isNullable() => null,
default => throw new HttpException($validationFailedCode)
};
}

$arguments[$i] = $payload;
}

$event->setArguments($arguments);
}

public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER_ARGUMENTS => 'onKernelControllerArguments',
];
}

private function mapQueryString(Request $request, string $type, MapQueryString $attribute): ?object
{
if (!$data = $request->query->all()) {
return null;
}

return $this->serializer->denormalize($data, $type, null, $attribute->serializationContext + self::CONTEXT_DENORMALIZE);
}

private function mapRequestPayload(Request $request, string $type, MapRequestPayload $attribute): object|array|null
{
if (null === $format = $request->getContentTypeFormat()) {
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, 'Unsupported format.');
}

if ($attribute->acceptFormat && !\in_array($format, (array) $attribute->acceptFormat, true)) {
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, sprintf('Unsupported format, expects "%s", but "%s" given.', implode('", "', (array) $attribute->acceptFormat), $format));
}

if ($data = $request->request->all()) {
return $this->serializer->denormalize($data, $type, null, $attribute->serializationContext + self::CONTEXT_DENORMALIZE);
}

if ('' === $data = $request->getContent()) {
return null;
}

if ('form' === $format) {
throw new HttpException(Response::HTTP_BAD_REQUEST, 'Request payload contains invalid "form" data.');
}

try {
return $this->serializer->deserialize($data, $type, $format, self::CONTEXT_DESERIALIZE + $attribute->serializationContext);
} catch (UnsupportedFormatException $e) {
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, sprintf('Unsupported format: "%s".', $format), $e);
} catch (NotEncodableValueException $e) {
throw new HttpException(Response::HTTP_BAD_REQUEST, sprintf('Request payload contains invalid "%s" data.', $format), $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,22 @@ public function guess(\Reflector $reflector, AbstractAnnotation $annotation, Con
}

foreach ($reflector->getParameters() as $rp) {
foreach ($rp->getAttributes(Body::class, \ReflectionAttribute::IS_INSTANCEOF) as $_) {
foreach ($rp->getAttributes(Body::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
$type = (($rnt = $rp->getType()) && $rnt instanceof \ReflectionNamedType) ? $rnt->getName() : null;

if (null === $type) {
continue;
}

/** @var Body $bodyAttribute */

Check failure on line 37 in src/OpenApi/Analyser/Guesser/Operation/OperationRequestBodyGuesser.php

View workflow job for this annotation

GitHub Actions / Psalm

UnnecessaryVarAnnotation

src/OpenApi/Analyser/Guesser/Operation/OperationRequestBodyGuesser.php:37:26: UnnecessaryVarAnnotation: The @var OpenSolid\OpenApiBundle\Attribute\Body annotation for $bodyAttribute is unnecessary (see https://psalm.dev/212)
$bodyAttribute = $attribute->newInstance();

$annotation->requestBody = new OA\RequestBody(required: !$rnt->allowsNull());
$annotation->requestBody->_context = new Context(['nested' => $annotation], $context);
$jsonContent = new OA\JsonContent(type: $type);
if ('array' === $type && null !== $bodyAttribute->itemsType) {
$jsonContent->items = new OA\Items(type: $bodyAttribute->itemsType);
}
$jsonContent->_context = new Context(['nested' => $annotation->requestBody], $context);
$annotation->requestBody->merge([$jsonContent]);
}
Expand Down
1 change: 1 addition & 0 deletions templates/openapi_ui.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
background-color: white;
}
</style>
<title>Swagger API Doc</title>
</head>
<body>
<?php
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace OpenSolid\Tests\OpenApiBundle\Functional\App\PostResourcesAction\Controller;

use OpenApi\Attributes\Schema;
use OpenSolid\OpenApiBundle\Attribute\Property;
use OpenSolid\Tests\OpenApiBundle\Functional\App\PostResourceAction\Model\ResourceStatus;

#[Schema(writeOnly: true)]
class PostResourceBody
{
#[Property(minLength: 3)]
public string $name;

#[Property(enum: ResourceStatus::class)]
public string $status;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace OpenSolid\Tests\OpenApiBundle\Functional\App\PostResourcesAction\Controller;

use OpenSolid\OpenApiBundle\Attribute\Body;
use OpenSolid\OpenApiBundle\Routing\Attribute\Post;
use OpenSolid\Tests\OpenApiBundle\Functional\App\PostResourcesAction\Model\ResourceView;

class PostResourcesAction
{
#[Post('/resources')]
public function __invoke(#[Body(itemsType: PostResourceBody::class)] array $body): ResourceView
{
return ResourceView::from('4f09d694-446a-4769-9929-dad96a071cad', $body[0]->name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace OpenSolid\Tests\OpenApiBundle\Functional\App\PostResourcesAction\Model;

enum ResourceStatus: string
{
case DRAFT = 'draft';
case PUBLISHED = 'published';
}
25 changes: 25 additions & 0 deletions tests/Functional/App/PostResourcesAction/Model/ResourceView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace OpenSolid\Tests\OpenApiBundle\Functional\App\PostResourcesAction\Model;

use OpenApi\Attributes\Schema;
use OpenSolid\OpenApiBundle\Attribute\Property;

#[Schema]
readonly class ResourceView
{
#[Property(format: 'uuid')]
public string $id;

#[Property]
public string $name;

public static function from(string $id, string $name): self
{
$self = new self();
$self->id = $id;
$self->name = $name;

return $self;
}
}
Loading

0 comments on commit c39a083

Please sign in to comment.