From 5de077e7de94f2e07ca615efc5ecf1b32b37a10e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Nork=C5=ABnas?= Date: Sat, 23 Dec 2023 10:15:42 +0200 Subject: [PATCH 1/2] fix(symfony): use Type constraint violation code instead of exception code (#6064) --- features/main/validation.feature | 14 +++++++------- src/Symfony/EventListener/DeserializeListener.php | 2 +- .../EventListener/DeserializeListenerTest.php | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/features/main/validation.feature b/features/main/validation.feature index 0ed47344dcb..570976f4d65 100644 --- a/features/main/validation.feature +++ b/features/main/validation.feature @@ -102,39 +102,39 @@ Feature: Using validations groups { "propertyPath": "", "message": "This value should be of type unknown.", - "code": "0", + "code": "ba785a8c-82cb-4283-967c-3cf342181b40", "hint": "Failed to create object because the class misses the \"baz\" property." }, { "propertyPath": "qux", "message": "This value should be of type string.", - "code": "0" + "code": "ba785a8c-82cb-4283-967c-3cf342181b40" }, { "propertyPath": "foo", "message": "This value should be of type bool.", - "code": "0" + "code": "ba785a8c-82cb-4283-967c-3cf342181b40" }, { "propertyPath": "bar", "message": "This value should be of type int.", - "code": "0" + "code": "ba785a8c-82cb-4283-967c-3cf342181b40" }, { "propertyPath": "uuid", "message": "This value should be of type uuid.", - "code": "0", + "code": "ba785a8c-82cb-4283-967c-3cf342181b40", "hint": "Invalid UUID string: y" }, { "propertyPath": "relatedDummy", "message": "This value should be of type array|string.", - "code": "0" + "code": "ba785a8c-82cb-4283-967c-3cf342181b40" }, { "propertyPath": "relatedDummies", "message": "This value should be of type array.", - "code": "0" + "code": "ba785a8c-82cb-4283-967c-3cf342181b40" } ] } diff --git a/src/Symfony/EventListener/DeserializeListener.php b/src/Symfony/EventListener/DeserializeListener.php index a56f8475bcf..9f147522ed0 100644 --- a/src/Symfony/EventListener/DeserializeListener.php +++ b/src/Symfony/EventListener/DeserializeListener.php @@ -111,7 +111,7 @@ public function onKernelRequest(RequestEvent $event): void if ($exception->canUseMessageForUser()) { $parameters['hint'] = $exception->getMessage(); } - $violations->add(new ConstraintViolation($this->translator->trans($message, ['{{ type }}' => implode('|', $exception->getExpectedTypes() ?? [])], 'validators'), $message, $parameters, null, $exception->getPath(), null, null, (string) $exception->getCode())); + $violations->add(new ConstraintViolation($this->translator->trans($message, ['{{ type }}' => implode('|', $exception->getExpectedTypes() ?? [])], 'validators'), $message, $parameters, null, $exception->getPath(), null, null, Type::INVALID_TYPE_ERROR)); } if (0 !== \count($violations)) { throw new ValidationException($violations); diff --git a/tests/Symfony/EventListener/DeserializeListenerTest.php b/tests/Symfony/EventListener/DeserializeListenerTest.php index 197dfa1c22d..8290d47b801 100644 --- a/tests/Symfony/EventListener/DeserializeListenerTest.php +++ b/tests/Symfony/EventListener/DeserializeListenerTest.php @@ -366,7 +366,7 @@ public function testTurnPartialDenormalizationExceptionIntoValidationException() $this->assertSame($violation->getPropertyPath(), 'foo'); $this->assertNull($violation->getInvalidValue()); $this->assertNull($violation->getPlural()); - $this->assertSame($violation->getCode(), '0'); + $this->assertSame($violation->getCode(), 'ba785a8c-82cb-4283-967c-3cf342181b40'); } } } From 36850102ac750797f1acabe0c753d17f233a87c6 Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 29 Dec 2023 17:24:25 +0100 Subject: [PATCH 2/2] style: latest cs-fixer --- src/JsonApi/Serializer/ConstraintViolationListNormalizer.php | 2 +- .../NotExposedOperationResourceMetadataCollectionFactory.php | 2 +- tests/Behat/DoctrineContext.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/JsonApi/Serializer/ConstraintViolationListNormalizer.php b/src/JsonApi/Serializer/ConstraintViolationListNormalizer.php index 148119dd57c..90c87a881cf 100644 --- a/src/JsonApi/Serializer/ConstraintViolationListNormalizer.php +++ b/src/JsonApi/Serializer/ConstraintViolationListNormalizer.php @@ -22,7 +22,7 @@ use Symfony\Component\Validator\ConstraintViolationListInterface; /** - * Converts {@see \Symfony\Component\Validator\ConstraintViolationListInterface} to a JSON API error representation. + * Converts {@see ConstraintViolationListInterface} to a JSON API error representation. * * @author Héctor Hurtarte */ diff --git a/src/Metadata/Resource/Factory/NotExposedOperationResourceMetadataCollectionFactory.php b/src/Metadata/Resource/Factory/NotExposedOperationResourceMetadataCollectionFactory.php index bf3afc2bec9..cf70ce228e4 100644 --- a/src/Metadata/Resource/Factory/NotExposedOperationResourceMetadataCollectionFactory.php +++ b/src/Metadata/Resource/Factory/NotExposedOperationResourceMetadataCollectionFactory.php @@ -60,7 +60,7 @@ public function create(string $resourceClass): ResourceMetadataCollection foreach ($resourceMetadataCollection as $resource) { $operations = $resource->getOperations(); - /** @var \ApiPlatform\Metadata\HttpOperation $operation */ + /** @var HttpOperation $operation */ foreach ($operations as $operation) { // An item operation has been found, nothing to do anymore in this factory if ((HttpOperation::METHOD_GET === $operation->getMethod() && !$operation instanceof CollectionOperationInterface) || ($operation->getExtraProperties()['is_legacy_resource_metadata'] ?? false)) { diff --git a/tests/Behat/DoctrineContext.php b/tests/Behat/DoctrineContext.php index 17cc076c3e7..8b5db33a3b6 100644 --- a/tests/Behat/DoctrineContext.php +++ b/tests/Behat/DoctrineContext.php @@ -2168,7 +2168,7 @@ public function thereIsADummyWithSubEntity(string $strId, string $name): void public function thereIsAGroupWithUuidAndNUsers(string $uuid, int $nbUsers): void { $group = new Group(); - $group->setUuid(\Symfony\Component\Uid\Uuid::fromString($uuid)); + $group->setUuid(SymfonyUuid::fromString($uuid)); $this->manager->persist($group);