[Serializer] Fix and improve constraintViolationListNormalizer's RFC7807 compliance
This commit is contained in:
parent
68eda49322
commit
3c789c610a
@ -32,21 +32,37 @@ class ConstraintViolationListNormalizer implements NormalizerInterface, Cacheabl
|
|||||||
$violations = array();
|
$violations = array();
|
||||||
$messages = array();
|
$messages = array();
|
||||||
foreach ($object as $violation) {
|
foreach ($object as $violation) {
|
||||||
$violations[] = array(
|
|
||||||
'propertyPath' => $violation->getPropertyPath(),
|
|
||||||
'message' => $violation->getMessage(),
|
|
||||||
'code' => $violation->getCode(),
|
|
||||||
);
|
|
||||||
$propertyPath = $violation->getPropertyPath();
|
$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) : '';
|
$prefix = $propertyPath ? sprintf('%s: ', $propertyPath) : '';
|
||||||
$messages[] = $prefix.$violation->getMessage();
|
$messages[] = $prefix.$violation->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
return array(
|
$result = array(
|
||||||
'title' => isset($context['title']) ? $context['title'] : 'An error occurred',
|
'type' => $context['type'] ?? 'https://symfony.com/errors/validation',
|
||||||
'detail' => $messages ? implode("\n", $messages) : '',
|
'title' => $context['title'] ?? 'Validation Failed',
|
||||||
'violations' => $violations,
|
|
||||||
);
|
);
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,19 +43,20 @@ class ConstraintViolationListNormalizerTest extends TestCase
|
|||||||
));
|
));
|
||||||
|
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'title' => 'An error occurred',
|
'type' => 'https://symfony.com/errors/validation',
|
||||||
|
'title' => 'Validation Failed',
|
||||||
'detail' => 'd: a
|
'detail' => 'd: a
|
||||||
4: 1',
|
4: 1',
|
||||||
'violations' => array(
|
'violations' => array(
|
||||||
array(
|
array(
|
||||||
'propertyPath' => 'd',
|
'propertyPath' => 'd',
|
||||||
'message' => 'a',
|
'title' => 'a',
|
||||||
'code' => 'f',
|
'type' => 'urn:uuid:f',
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'propertyPath' => '4',
|
'propertyPath' => '4',
|
||||||
'message' => '1',
|
'title' => '1',
|
||||||
'code' => '6',
|
'type' => 'urn:uuid:6',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user