Skip to content

Commit 385953a

Browse files
authored
fix(jsonapi): handle type error when handling validation errors (#7330)
1 parent d1abfc0 commit 385953a

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/JsonApi/Serializer/ConstraintViolationListNormalizer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ private function getSourcePointerFromViolation(ConstraintViolationInterface $vio
7171
return 'data';
7272
}
7373

74-
$class = $violation->getRoot()::class;
74+
$root = $violation->getRoot();
75+
76+
if (!\is_object($root)) {
77+
return "data/attributes/$fieldName";
78+
}
79+
80+
$class = $root::class;
7581
$propertyMetadata = $this->propertyMetadataFactory
7682
->create(
7783
// Im quite sure this requires some thought in case of validations over relationships

src/JsonApi/Tests/Serializer/ConstraintViolationNormalizerTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,61 @@ public function testNormalize(): void
9191
(new ConstraintViolationListNormalizer($propertyMetadataFactoryProphecy->reveal(), $nameConverterProphecy->reveal()))->normalize($constraintViolationList)
9292
);
9393
}
94+
95+
public function testNormalizeWithStringRoot(): void
96+
{
97+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
98+
99+
// Create a violation with a string root (simulating query parameter validation)
100+
$constraintViolationList = new ConstraintViolationList([
101+
new ConstraintViolation('Invalid page value.', 'Invalid page value.', [], 'page', 'page', 'invalid'),
102+
]);
103+
104+
$normalizer = new ConstraintViolationListNormalizer($propertyMetadataFactoryProphecy->reveal());
105+
106+
$result = $normalizer->normalize($constraintViolationList);
107+
108+
$this->assertEquals(
109+
[
110+
'errors' => [
111+
[
112+
'detail' => 'Invalid page value.',
113+
'source' => [
114+
'pointer' => 'data/attributes/page',
115+
],
116+
],
117+
],
118+
],
119+
$result
120+
);
121+
}
122+
123+
public function testNormalizeWithNullRoot(): void
124+
{
125+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
126+
127+
// Create a violation with a null root
128+
$constraintViolationList = new ConstraintViolationList([
129+
new ConstraintViolation('Invalid value.', 'Invalid value.', [], null, 'field', 'invalid'),
130+
]);
131+
132+
$normalizer = new ConstraintViolationListNormalizer($propertyMetadataFactoryProphecy->reveal());
133+
134+
// This should not throw a TypeError and should handle the null root gracefully
135+
$result = $normalizer->normalize($constraintViolationList);
136+
137+
$this->assertEquals(
138+
[
139+
'errors' => [
140+
[
141+
'detail' => 'Invalid value.',
142+
'source' => [
143+
'pointer' => 'data/attributes/field',
144+
],
145+
],
146+
],
147+
],
148+
$result
149+
);
150+
}
94151
}

0 commit comments

Comments
 (0)