Skip to content

Commit dd105c0

Browse files
authored
Merge pull request api-platform#3857 from api-platform/feature/violation-code
Add the violation code to the violation properties
2 parents f71b9e5 + 30218e4 commit dd105c0

File tree

6 files changed

+15
-6
lines changed

6 files changed

+15
-6
lines changed

features/hal/problem.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Feature: Error handling valid according to RFC 7807 (application/problem+json)
2222
"violations": [
2323
{
2424
"propertyPath": "name",
25-
"message": "This value should not be blank."
25+
"message": "This value should not be blank.",
26+
"code": "c1051bb4-d103-4f74-8988-acbcafc7fdc3"
2627
}
2728
]
2829
}

features/hydra/error.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Feature: Error handling
2222
"violations": [
2323
{
2424
"propertyPath": "name",
25-
"message": "This value should not be blank."
25+
"message": "This value should not be blank.",
26+
"code": "c1051bb4-d103-4f74-8988-acbcafc7fdc3"
2627
}
2728
]
2829
}

features/main/validation.feature

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ Feature: Using validations groups
3636
"violations": [
3737
{
3838
"propertyPath": "name",
39-
"message": "This value should not be null."
39+
"message": "This value should not be null.",
40+
"code": "ad32d13f-c3d4-423b-909a-857b961eb720"
4041
}
4142
]
4243
}
@@ -64,7 +65,8 @@ Feature: Using validations groups
6465
"violations": [
6566
{
6667
"propertyPath": "title",
67-
"message": "This value should not be null."
68+
"message": "This value should not be null.",
69+
"code": "ad32d13f-c3d4-423b-909a-857b961eb720"
6870
}
6971
]
7072
}

src/Serializer/AbstractConstraintViolationListNormalizer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ protected function getMessagesAndViolations(ConstraintViolationListInterface $co
6363
$violationData = [
6464
'propertyPath' => $this->nameConverter ? $this->nameConverter->normalize($violation->getPropertyPath(), $class, static::FORMAT) : $violation->getPropertyPath(),
6565
'message' => $violation->getMessage(),
66+
'code' => $violation->getCode(),
6667
];
6768

6869
$constraint = $violation->getConstraint();

tests/Hydra/Serializer/ConstraintViolationNormalizerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testNormalize(?array $fields, array $result)
6262
$constraint = new NotNull();
6363
$constraint->payload = ['severity' => 'warning', 'anotherField2' => 'aValue'];
6464
$list = new ConstraintViolationList([
65-
new ConstraintViolation('a', 'b', [], 'c', 'd', 'e', null, null, $constraint),
65+
new ConstraintViolation('a', 'b', [], 'c', 'd', 'e', null, 'f24bdbad0becef97a6887238aa58221c', $constraint),
6666
new ConstraintViolation('1', '2', [], '3', '4', '5'),
6767
]);
6868

@@ -75,10 +75,12 @@ public function testNormalize(?array $fields, array $result)
7575
[
7676
'propertyPath' => '_d',
7777
'message' => 'a',
78+
'code' => 'f24bdbad0becef97a6887238aa58221c',
7879
],
7980
[
8081
'propertyPath' => '_4',
8182
'message' => '1',
83+
'code' => null,
8284
],
8385
],
8486
];

tests/Problem/Serializer/ConstraintViolationNormalizerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testNormalize()
5353
$constraint = new NotNull();
5454
$constraint->payload = ['severity' => 'warning', 'anotherField2' => 'aValue'];
5555
$list = new ConstraintViolationList([
56-
new ConstraintViolation('a', 'b', [], 'c', 'd', 'e', null, null, $constraint),
56+
new ConstraintViolation('a', 'b', [], 'c', 'd', 'e', null, 'f24bdbad0becef97a6887238aa58221c', $constraint),
5757
new ConstraintViolation('1', '2', [], '3', '4', '5'),
5858
]);
5959

@@ -65,13 +65,15 @@ public function testNormalize()
6565
[
6666
'propertyPath' => '_d',
6767
'message' => 'a',
68+
'code' => 'f24bdbad0becef97a6887238aa58221c',
6869
'payload' => [
6970
'severity' => 'warning',
7071
],
7172
],
7273
[
7374
'propertyPath' => '_4',
7475
'message' => '1',
76+
'code' => null,
7577
],
7678
],
7779
];

0 commit comments

Comments
 (0)