Skip to content

Commit

Permalink
Improve error normalizer (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
fre5h authored Apr 30, 2024
1 parent dcdb382 commit 2bfb06b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ jobs:

- name: 'Upload to Codecov'
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
files: ./coverage.xml
fail_ci_if_error: true
Expand Down
16 changes: 16 additions & 0 deletions Serializer/Normalizer/ConstraintViolationListNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function normalize(mixed $object, string $format = null, array $context =
{
$result = $this->symfonyConstraintViolationListNormalizer->normalize($object, $format, $context);

$this->removeInternalViolationFields($result);

if (\is_array($result) && \array_key_exists('detail', $result) && $result['detail']) {
$messages = explode("\n", $result['detail']);

Expand All @@ -79,4 +81,18 @@ public function normalize(mixed $object, string $format = null, array $context =

return $result;
}

/**
* @param array $data
*/
private function removeInternalViolationFields(array &$data): void
{
if (isset($data['violations'])) {
foreach ($data['violations'] as &$violation) {
unset($violation['template'], $violation['parameters']);
$this->removeInternalViolationFields($violation);
}
unset($violation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,32 @@ public function testNormalize(string $originDetail, string $resultDetail): void
->expects(self::once())
->method('normalize')
->with($object, $format, $context)
->willReturn(['detail' => $originDetail])
->willReturn([
'detail' => $originDetail,
'violations' => [
[
'propertyPath' => 'test',
'title' => 'test',
'type' => 'test',
'template' => 'test',
'parameters' => 'test',
]
]
])
;

$result = (array) $this->normalizer->normalize($object, $format, $context);

self::assertArrayHasKey('detail', $result);
self::assertSame($resultDetail, $result['detail']);
self::assertSame(
[
'propertyPath' => 'test',
'title' => 'test',
'type' => 'test',
],
$result['violations'][0],
);
}

public static function dataProviderForTestNormalize(): iterable
Expand Down

0 comments on commit 2bfb06b

Please sign in to comment.