Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/JsonApi/Serializer/ConstraintViolationListNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ private function getSourcePointerFromViolation(ConstraintViolationInterface $vio
return 'data';
}

$class = $violation->getRoot()::class;
$root = $violation->getRoot();

if (!\is_object($root)) {
return "data/attributes/$fieldName";
}

$class = $root::class;
$propertyMetadata = $this->propertyMetadataFactory
->create(
// Im quite sure this requires some thought in case of validations over relationships
Expand Down
57 changes: 57 additions & 0 deletions src/JsonApi/Tests/Serializer/ConstraintViolationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,61 @@ public function testNormalize(): void
(new ConstraintViolationListNormalizer($propertyMetadataFactoryProphecy->reveal(), $nameConverterProphecy->reveal()))->normalize($constraintViolationList)
);
}

public function testNormalizeWithStringRoot(): void
{
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);

// Create a violation with a string root (simulating query parameter validation)
$constraintViolationList = new ConstraintViolationList([
new ConstraintViolation('Invalid page value.', 'Invalid page value.', [], 'page', 'page', 'invalid'),
]);

$normalizer = new ConstraintViolationListNormalizer($propertyMetadataFactoryProphecy->reveal());

$result = $normalizer->normalize($constraintViolationList);

$this->assertEquals(
[
'errors' => [
[
'detail' => 'Invalid page value.',
'source' => [
'pointer' => 'data/attributes/page',
],
],
],
],
$result
);
}

public function testNormalizeWithNullRoot(): void
{
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);

// Create a violation with a null root
$constraintViolationList = new ConstraintViolationList([
new ConstraintViolation('Invalid value.', 'Invalid value.', [], null, 'field', 'invalid'),
]);

$normalizer = new ConstraintViolationListNormalizer($propertyMetadataFactoryProphecy->reveal());

// This should not throw a TypeError and should handle the null root gracefully
$result = $normalizer->normalize($constraintViolationList);

$this->assertEquals(
[
'errors' => [
[
'detail' => 'Invalid value.',
'source' => [
'pointer' => 'data/attributes/field',
],
],
],
],
$result
);
}
}
Loading