From 3c789c610aa0696d1ff09cfc990ce61c81475a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 17 May 2018 07:48:00 +0200 Subject: [PATCH] [Serializer] Fix and improve constraintViolationListNormalizer's RFC7807 compliance --- .../ConstraintViolationListNormalizer.php | 34 ++++++++++++++----- .../ConstraintViolationListNormalizerTest.php | 11 +++--- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php index 2ba258ecb7..bc9aa60bd3 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php @@ -32,21 +32,37 @@ class ConstraintViolationListNormalizer implements NormalizerInterface, Cacheabl $violations = array(); $messages = array(); foreach ($object as $violation) { - $violations[] = array( - 'propertyPath' => $violation->getPropertyPath(), - 'message' => $violation->getMessage(), - 'code' => $violation->getCode(), - ); $propertyPath = $violation->getPropertyPath(); + + $violationEntry = array( + 'propertyPath' => $propertyPath, + 'title' => $violation->getMessage(), + ); + if (null !== $code = $violation->getCode()) { + $violationEntry['type'] = sprintf('urn:uuid:%s', $code); + } + + $violations[] = $violationEntry; + $prefix = $propertyPath ? sprintf('%s: ', $propertyPath) : ''; $messages[] = $prefix.$violation->getMessage(); } - return array( - 'title' => isset($context['title']) ? $context['title'] : 'An error occurred', - 'detail' => $messages ? implode("\n", $messages) : '', - 'violations' => $violations, + $result = array( + 'type' => $context['type'] ?? 'https://symfony.com/errors/validation', + 'title' => $context['title'] ?? 'Validation Failed', ); + if (isset($context['status'])) { + $result['status'] = $context['status']; + } + if ($messages) { + $result['detail'] = implode("\n", $messages); + } + if (isset($context['instance'])) { + $result['instance'] = $context['instance']; + } + + return $result + array('violations' => $violations); } /** diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php index 5c9c55028f..9e8aec5147 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php @@ -43,19 +43,20 @@ class ConstraintViolationListNormalizerTest extends TestCase )); $expected = array( - 'title' => 'An error occurred', + 'type' => 'https://symfony.com/errors/validation', + 'title' => 'Validation Failed', 'detail' => 'd: a 4: 1', 'violations' => array( array( 'propertyPath' => 'd', - 'message' => 'a', - 'code' => 'f', + 'title' => 'a', + 'type' => 'urn:uuid:f', ), array( 'propertyPath' => '4', - 'message' => '1', - 'code' => '6', + 'title' => '1', + 'type' => 'urn:uuid:6', ), ), );