Skip to content

Instantly share code, notes, and snippets.

@enleur
Last active May 18, 2022 10:31
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save enleur/5348eeef144d51e3fb7b0dc45ce46255 to your computer and use it in GitHub Desktop.
Save enleur/5348eeef144d51e3fb7b0dc45ce46255 to your computer and use it in GitHub Desktop.
Symfony json body to request object auto mapping
<?php
namespace App\Request\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\ConstraintViolationListInterface;
final class ConstraintViolationsConverter implements ParamConverterInterface
{
/**
* @param Request $request
* @param ParamConverter $configuration
*
* @return bool|void
*/
public function apply(Request $request, ParamConverter $configuration)
{
$violations = $request->attributes->get('_violations');
$request->attributes->remove('_violations');
$request->attributes->set($configuration->getName(), $violations);
}
/**
* @param ParamConverter $configuration
*
* @return bool
*/
public function supports(ParamConverter $configuration)
{
return ConstraintViolationListInterface::class === $configuration->getClass();
}
}
<?php
namespace App\Controller;
final class ExampleController extends AbstractController
{
public function registerAction(
RegistrationRequest $registrationRequest,
ConstraintViolationListInterface $violations,
Registration $registration
): Response {
if ($violations->count() > 0) {
return $this->validationError($violations);
}
//...
return $this->json(..);
}
}
<?php
namespace App\Request\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
final class JsonBodySerializableConverter implements ParamConverterInterface
{
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @var ValidatorInterface
*/
private $validator;
/**
* @param SerializerInterface $serializer
* @param ValidatorInterface $validator
*/
public function __construct(SerializerInterface $serializer, ValidatorInterface $validator)
{
$this->serializer = $serializer;
$this->validator = $validator;
}
/**
* @param Request $request
* @param ParamConverter $configuration
*
* @return bool|void
*/
public function apply(Request $request, ParamConverter $configuration)
{
$body = $request->getContent();
$obj = $this->serializer->deserialize($body, $configuration->getClass(), 'json');
$violationList = $this->validator->validate($obj);
$request->attributes->set('_violations', $violationList);
$request->attributes->set($configuration->getName(), $obj);
}
/**
* @param ParamConverter $configuration
*
* @return bool
*/
public function supports(ParamConverter $configuration)
{
$class = $configuration->getClass();
if (!is_string($class)) {
return false;
}
return in_array(JsonBodySerializableInterface::class, class_implements($class));
}
}
<?php
namespace App\Request;
use App\Request\ParamConverter\JsonBodySerializableInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Validator\Constraints as Assert;
final class RegistrationRequest implements JsonBodySerializableInterface
{
/**
* @Assert\NotBlank
* @Assert\Type("string")
*
* @var string
*/
public $username;
/**
* @Assert\NotBlank
* @Assert\Type("string")
*
* @var string
*/
public $email;
}
@raitocz
Copy link

raitocz commented Jan 20, 2021

both JsonBodySerializableInterface is missing but thanks for sharing the converters anyway

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment