Skip to content

Commit

Permalink
Merge pull request #3857 from api-platform/feature/violation-code
Browse files Browse the repository at this point in the history
Add the violation code to the violation properties
  • Loading branch information
soyuka authored Nov 29, 2020
2 parents f71b9e5 + 30218e4 commit dd105c0
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion features/hal/problem.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Feature: Error handling valid according to RFC 7807 (application/problem+json)
"violations": [
{
"propertyPath": "name",
"message": "This value should not be blank."
"message": "This value should not be blank.",
"code": "c1051bb4-d103-4f74-8988-acbcafc7fdc3"
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion features/hydra/error.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Feature: Error handling
"violations": [
{
"propertyPath": "name",
"message": "This value should not be blank."
"message": "This value should not be blank.",
"code": "c1051bb4-d103-4f74-8988-acbcafc7fdc3"
}
]
}
Expand Down
6 changes: 4 additions & 2 deletions features/main/validation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Feature: Using validations groups
"violations": [
{
"propertyPath": "name",
"message": "This value should not be null."
"message": "This value should not be null.",
"code": "ad32d13f-c3d4-423b-909a-857b961eb720"
}
]
}
Expand Down Expand Up @@ -64,7 +65,8 @@ Feature: Using validations groups
"violations": [
{
"propertyPath": "title",
"message": "This value should not be null."
"message": "This value should not be null.",
"code": "ad32d13f-c3d4-423b-909a-857b961eb720"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected function getMessagesAndViolations(ConstraintViolationListInterface $co
$violationData = [
'propertyPath' => $this->nameConverter ? $this->nameConverter->normalize($violation->getPropertyPath(), $class, static::FORMAT) : $violation->getPropertyPath(),
'message' => $violation->getMessage(),
'code' => $violation->getCode(),
];

$constraint = $violation->getConstraint();
Expand Down
4 changes: 3 additions & 1 deletion tests/Hydra/Serializer/ConstraintViolationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testNormalize(?array $fields, array $result)
$constraint = new NotNull();
$constraint->payload = ['severity' => 'warning', 'anotherField2' => 'aValue'];
$list = new ConstraintViolationList([
new ConstraintViolation('a', 'b', [], 'c', 'd', 'e', null, null, $constraint),
new ConstraintViolation('a', 'b', [], 'c', 'd', 'e', null, 'f24bdbad0becef97a6887238aa58221c', $constraint),
new ConstraintViolation('1', '2', [], '3', '4', '5'),
]);

Expand All @@ -75,10 +75,12 @@ public function testNormalize(?array $fields, array $result)
[
'propertyPath' => '_d',
'message' => 'a',
'code' => 'f24bdbad0becef97a6887238aa58221c',
],
[
'propertyPath' => '_4',
'message' => '1',
'code' => null,
],
],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testNormalize()
$constraint = new NotNull();
$constraint->payload = ['severity' => 'warning', 'anotherField2' => 'aValue'];
$list = new ConstraintViolationList([
new ConstraintViolation('a', 'b', [], 'c', 'd', 'e', null, null, $constraint),
new ConstraintViolation('a', 'b', [], 'c', 'd', 'e', null, 'f24bdbad0becef97a6887238aa58221c', $constraint),
new ConstraintViolation('1', '2', [], '3', '4', '5'),
]);

Expand All @@ -65,13 +65,15 @@ public function testNormalize()
[
'propertyPath' => '_d',
'message' => 'a',
'code' => 'f24bdbad0becef97a6887238aa58221c',
'payload' => [
'severity' => 'warning',
],
],
[
'propertyPath' => '_4',
'message' => '1',
'code' => null,
],
],
];
Expand Down

0 comments on commit dd105c0

Please sign in to comment.