@@ -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