I have entity Tag
, which have unique property tagValue
. When I make a POST
with already exists tagValue
I want to get it in response.
config/validator/tag.yaml
:
App\Entity\Tag:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: tagValue
properties:
tagValue:
- NotBlank: ~
src/Entity/Tag.php
:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use DateTimeInterface;
use DateTime;
use Exception;
/**
* @ORM\Table(name="tag")
* @ORM\Entity(repositoryClass="App\Repository\TagRepository")
* @ORM\HasLifecycleCallbacks
*/
class Tag
{
/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string
* @ORM\Column(type="string", length=255)
*/
private $tagValue;
// ...
}
When I make a POST
:
curl --request POST \
--url http://127.0.0.1:8888/api/tags \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header 'x-auth-token: xxxxxxxxxxxxxxxx' \
--data '{
"tagValue": "test"
}'
I got response with just created entity and code 201. Everything is ok, but if I'll make this request again, as expected, I'm getting response code 400 with response body:
{
"type": "https:\/\/tools.ietf.org\/html\/rfc2616#section-10",
"title": "An error occurred",
"detail": "tagValue: This value is already used.",
"violations": [
{
"propertyPath": "tagValue",
"message": "This value is already used."
}
]
}
But I want exist entity include to that response.
Any ideas how to do that without breaking REST rules?
(Symfony 4.2.5, api-platform/api-pack 1.2.0)
Finally I got an answer on GitHub from maks-rafalko (I really appreciate him for that) and if someone will stuck on the same issue, here's his solution:
You are lucky man, we have just implemented it inside our application. There is no built in functionality in API-Platform for this feature, we had to override some classes in order to add it.
First of all, this is how our response now looks like when the Unique constraint is vailoated:
{
"type": "https://tools.ietf.org/html/rfc2616#section-10",
"title": "An error occurred",
"detail": "number: This Usage Reference already exists with the same number and channel.",
"violations": [
{
"propertyPath": "number",
"message": "This Usage Reference already exists with the same number and channel."
}
],
"existingUniqueEntities": [
{
"uniquePropertyPaths": [
"number",
"channel"
],
"entity": {
"id": 1101,
"number": "23423423435",
"channel": "/api/channels/1",
"createdAt": "2019-07-17T07:25:50.721Z"
}
}
]
}
Please note, that you may have many unique violations, and such schema makes it possible to return many entities that already exist and conflicts with the provided request (e.g. entity can have 2 pairs of unique keys, one by email, another one is by reference)
Also, our implementation uses exactly those serialization groups that would be used by executing GET /resource, where resource is a resource you are trying to create. We get those serialozation groups from the api-platform metadata
So here is the code:
<?php
declare(strict_types=1);
namespace App\Serializer;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Serializer\AbstractConstraintViolationListNormalizer;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationListInterface;
/**
* This class completely overrides `ApiPlatform\Core\Problem\Serializer\ConstraintViolationListNormalizer` class
* since it's final
*
* Goal of overriding is to add `existingUniqueEntities` key when ViolationList contains unique entity violations
*
* @see \ApiPlatform\Core\Problem\Serializer\ConstraintViolationListNormalizer
*/
class ConstraintViolationListNormalizer extends AbstractConstraintViolationListNormalizer implements NormalizerAwareInterface
{
public const FORMAT = 'jsonproblem';
public const TYPE = 'type';
public const TITLE = 'title';
/**
* @var array<string, string>
*/
private $defaultContext = [
self::TYPE => 'https://tools.ietf.org/html/rfc2616#section-10',
self::TITLE => 'An error occurred',
];
/**
* @var ResourceMetadataFactoryInterface
*/
private $resourceMetadataFactory;
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @var NormalizerInterface
*/
private $normalizer;
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, array $serializePayloadFields = null, NameConverterInterface $nameConverter = null, array $defaultContext = [])
{
parent::__construct($serializePayloadFields, $nameConverter);
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}
public function setNormalizer(NormalizerInterface $normalizer): void
{
$this->normalizer = $normalizer;
}
/**
* @param mixed $object
* @param string|null $format
* @param array $context
*
* @return array
*/
public function normalize($object, $format = null, array $context = []): array
{
[$messages, $violations] = $this->getMessagesAndViolations($object);
$response = [
'type' => $context[self::TYPE] ?? $this->defaultContext[self::TYPE],
'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
'detail' => $messages ? implode("\n", $messages) : (string) $object,
'violations' => $violations,
];
$existingUniqueEntities = $this->getExistingUniqueEntities($object);
return \count($existingUniqueEntities) > 0 ?
array_merge($response, ['existingUniqueEntities' => $existingUniqueEntities])
: $response;
}
private function getExistingUniqueEntities(ConstraintViolationListInterface $constraintViolationList): array
{
$existingUniqueEntities = [];
/** @var ConstraintViolation $violation */
foreach ($constraintViolationList as $violation) {
$constraint = $violation->getConstraint();
if (!$constraint instanceof UniqueEntity) {
continue;
}
$rootEntity = \is_object($violation->getRoot()) ? $violation->getRoot() : null;
if ($rootEntity === null) {
continue;
}
$existingEntityCausedViolation = $violation->getCause()[0];
$metadata = $this->resourceMetadataFactory->create(\get_class($existingEntityCausedViolation));
// get normalization groups for `GET /resource` operation, fallback to global resource groups
$normalizationContext = $metadata->getItemOperationAttribute('get', 'normalization_context', [], true);
$groups = $normalizationContext['groups'] ?? [];
$entityNormalizationContext = \count($groups) > 0 ? ['groups' => $groups] : [];
$existingUniqueEntities[] = [
'uniquePropertyPaths' => $constraint->fields,
'entity' => $this->normalizer->normalize($existingEntityCausedViolation, null, $entityNormalizationContext),
];
}
return $existingUniqueEntities;
}
}
Everything is inside getExistingUniqueEntities, but unfortunately, we had to completely override ApiPlatform\Core\Problem\Serializer\ConstraintViolationListNormalizer class because it is final and we could not extend it.
We managed to override it using Compiler Pass:
# src/Kernel.php
class Kernel extends BaseKernel implements CompilerPassInterface
{
private const CONSTRAINT_VIOLATION_LIST_NORMALIZER_PRIORITY = -780;
...
public function process(ContainerBuilder $container)
{
...
$constraintViolationListNormalizerDefinition = new Definition(
ConstraintViolationListNormalizer::class,
[
$container->getDefinition('api_platform.metadata.resource.metadata_factory.cached'),
$container->getParameter('api_platform.validator.serialize_payload_fields'),
$container->hasDefinition('api_platform.name_converter') ? $container->getDefinition('api_platform.name_converter') : null,
[],
]
);
$constraintViolationListNormalizerDefinition->addTag('serializer.normalizer', ['priority' => self::CONSTRAINT_VIOLATION_LIST_NORMALIZER_PRIORITY]);
$container->setDefinition('api_platform.problem.normalizer.constraint_violation_list', $constraintViolationListNormalizerDefinition);
}
So, this solution is based on Symfony Validator and "listens" UniqueEntity vailoations. And if there are such violations, this normalizer adds already existing entity(ies) to the response.
Hope it helps!