25
25
use Symfony \Component \Serializer \Encoder \XmlEncoder ;
26
26
use Symfony \Component \Serializer \Exception \NotNormalizableValueException ;
27
27
use Symfony \Component \Serializer \Exception \PartialDenormalizationException ;
28
+ use Symfony \Component \Serializer \Normalizer \ChainDenormalizer ;
29
+ use Symfony \Component \Serializer \Normalizer \ChainNormalizer ;
28
30
use Symfony \Component \Serializer \Normalizer \DenormalizerInterface ;
29
31
use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
30
32
use Symfony \Component \Serializer \Serializer ;
@@ -202,7 +204,10 @@ public function testQueryNullPayloadAndNotDefaultOrNullableArgument()
202
204
}
203
205
}
204
206
205
- public function testWithoutValidatorAndCouldNotDenormalize ()
207
+ /**
208
+ * @group legacy
209
+ */
210
+ public function testWithoutValidatorAndCouldNotDenormalizeWithLegacySerializer ()
206
211
{
207
212
$ content = '{"price": 50, "title": ["not a string"]} ' ;
208
213
$ serializer = new Serializer ([new ObjectNormalizer ()], ['json ' => new JsonEncoder ()]);
@@ -226,10 +231,34 @@ public function testWithoutValidatorAndCouldNotDenormalize()
226
231
}
227
232
}
228
233
234
+ public function testWithoutValidatorAndCouldNotDenormalize ()
235
+ {
236
+ $ content = '{"price": 50, "title": ["not a string"]} ' ;
237
+ $ serializer = new Serializer ([], ['json ' => new JsonEncoder ()], new ChainNormalizer ([new ObjectNormalizer ()]), new ChainDenormalizer ([new ObjectNormalizer ()]));
238
+
239
+ $ resolver = new RequestPayloadValueResolver ($ serializer );
240
+
241
+ $ argument = new ArgumentMetadata ('invalid ' , RequestPayload::class, false , false , null , false , [
242
+ MapRequestPayload::class => new MapRequestPayload (),
243
+ ]);
244
+ $ request = Request::create ('/ ' , 'POST ' , server: ['CONTENT_TYPE ' => 'application/json ' ], content: $ content );
245
+
246
+ $ kernel = $ this ->createMock (HttpKernelInterface::class);
247
+ $ arguments = $ resolver ->resolve ($ request , $ argument );
248
+ $ event = new ControllerArgumentsEvent ($ kernel , function () {}, $ arguments , $ request , HttpKernelInterface::MAIN_REQUEST );
249
+
250
+ try {
251
+ $ resolver ->onKernelControllerArguments ($ event );
252
+ $ this ->fail (sprintf ('Expected "%s" to be thrown. ' , HttpException::class));
253
+ } catch (HttpException $ e ) {
254
+ $ this ->assertInstanceOf (PartialDenormalizationException::class, $ e ->getPrevious ());
255
+ }
256
+ }
257
+
229
258
public function testValidationNotPassed ()
230
259
{
231
260
$ content = '{"price": 50, "title": ["not a string"]} ' ;
232
- $ serializer = new Serializer ([new ObjectNormalizer () ], ['json ' => new JsonEncoder ()]);
261
+ $ serializer = new Serializer ([], ['json ' => new JsonEncoder ()], new ChainNormalizer ([ new ObjectNormalizer ()]), new ChainDenormalizer ([ new ObjectNormalizer ()]) );
233
262
234
263
$ validator = $ this ->createMock (ValidatorInterface::class);
235
264
$ validator ->expects ($ this ->never ())
@@ -260,7 +289,7 @@ public function testValidationNotPassed()
260
289
public function testValidationNotPerformedWhenPartialDenormalizationReturnsViolation ()
261
290
{
262
291
$ content = '{"password": "abc"} ' ;
263
- $ serializer = new Serializer ([new ObjectNormalizer () ], ['json ' => new JsonEncoder ()]);
292
+ $ serializer = new Serializer ([], ['json ' => new JsonEncoder ()], new ObjectNormalizer (), new ObjectNormalizer () );
264
293
265
294
$ validator = $ this ->createMock (ValidatorInterface::class);
266
295
$ validator ->expects ($ this ->never ())
@@ -314,7 +343,7 @@ public function testRequestContentValidationPassed()
314
343
{
315
344
$ content = '{"price": 50} ' ;
316
345
$ payload = new RequestPayload (50 );
317
- $ serializer = new Serializer ([new ObjectNormalizer () ], ['json ' => new JsonEncoder ()]);
346
+ $ serializer = new Serializer ([], ['json ' => new JsonEncoder ()], new ChainNormalizer ([ new ObjectNormalizer ()]), new ChainDenormalizer ([ new ObjectNormalizer ()]) );
318
347
319
348
$ validator = $ this ->createMock (ValidatorInterface::class);
320
349
$ validator ->expects ($ this ->once ())
@@ -370,7 +399,7 @@ public function testQueryStringValidationPassed()
370
399
$ payload = new RequestPayload (50 );
371
400
$ query = ['price ' => '50 ' ];
372
401
373
- $ serializer = new Serializer ([new ObjectNormalizer () ], ['json ' => new JsonEncoder ()]);
402
+ $ serializer = new Serializer ([], ['json ' => new JsonEncoder ()], new ChainNormalizer ([ new ObjectNormalizer ()]), new ChainDenormalizer ([ new ObjectNormalizer ()]) );
374
403
375
404
$ validator = $ this ->createMock (ValidatorInterface::class);
376
405
$ validator ->expects ($ this ->once ())
@@ -398,7 +427,7 @@ public function testRequestInputValidationPassed()
398
427
$ input = ['price ' => '50 ' ];
399
428
$ payload = new RequestPayload (50 );
400
429
401
- $ serializer = new Serializer ([new ObjectNormalizer () ], ['json ' => new JsonEncoder ()]);
430
+ $ serializer = new Serializer ([], ['json ' => new JsonEncoder ()], new ChainNormalizer ([ new ObjectNormalizer ()]), new ChainDenormalizer ([ new ObjectNormalizer ()]) );
402
431
403
432
$ validator = $ this ->createMock (ValidatorInterface::class);
404
433
$ validator ->expects ($ this ->once ())
@@ -443,7 +472,7 @@ public function testItThrowsOnVariadicArgument()
443
472
public function testAcceptFormatPassed (mixed $ acceptFormat , string $ contentType , string $ content )
444
473
{
445
474
$ encoders = ['json ' => new JsonEncoder (), 'xml ' => new XmlEncoder ()];
446
- $ serializer = new Serializer ([new ObjectNormalizer ()], $ encoders );
475
+ $ serializer = new Serializer ([], $ encoders , new ObjectNormalizer (), new ObjectNormalizer () );
447
476
$ validator = (new ValidatorBuilder ())->getValidator ();
448
477
$ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
449
478
@@ -506,7 +535,7 @@ public static function provideMatchedFormatContext(): iterable
506
535
*/
507
536
public function testAcceptFormatNotPassed (mixed $ acceptFormat , string $ contentType , string $ content , string $ expectedExceptionMessage )
508
537
{
509
- $ serializer = new Serializer ([new ObjectNormalizer ()] );
538
+ $ serializer = new Serializer ([], [], new ObjectNormalizer (), new ObjectNormalizer () );
510
539
$ validator = (new ValidatorBuilder ())->getValidator ();
511
540
$ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
512
541
@@ -577,7 +606,7 @@ public function testValidationGroupsPassed(string $method, ValueResolver $attrib
577
606
$ payload = new RequestPayload (50 );
578
607
$ payload ->title = 'A long title, so the validation passes ' ;
579
608
580
- $ serializer = new Serializer ([new ObjectNormalizer ()] );
609
+ $ serializer = new Serializer ([], [], new ObjectNormalizer (), new ObjectNormalizer () );
581
610
$ validator = (new ValidatorBuilder ())->enableAttributeMapping ()->getValidator ();
582
611
$ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
583
612
@@ -603,7 +632,7 @@ public function testValidationGroupsNotPassed(string $method, ValueResolver $att
603
632
{
604
633
$ input = ['price ' => '50 ' , 'title ' => 'Too short ' ];
605
634
606
- $ serializer = new Serializer ([new ObjectNormalizer ()] );
635
+ $ serializer = new Serializer ([], [], new ObjectNormalizer (), new ObjectNormalizer () );
607
636
$ validator = (new ValidatorBuilder ())->enableAttributeMapping ()->getValidator ();
608
637
$ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
609
638
@@ -662,7 +691,7 @@ public static function provideValidationGroupsOnManyTypes(): iterable
662
691
663
692
public function testQueryValidationErrorCustomStatusCode ()
664
693
{
665
- $ serializer = new Serializer ([new ObjectNormalizer ()], [] );
694
+ $ serializer = new Serializer ([], [], new ObjectNormalizer (), new ObjectNormalizer () );
666
695
667
696
$ validator = $ this ->createMock (ValidatorInterface::class);
668
697
@@ -695,7 +724,7 @@ public function testQueryValidationErrorCustomStatusCode()
695
724
public function testRequestPayloadValidationErrorCustomStatusCode ()
696
725
{
697
726
$ content = '{"price": 50, "title": ["not a string"]} ' ;
698
- $ serializer = new Serializer ([new ObjectNormalizer () ], ['json ' => new JsonEncoder ()]);
727
+ $ serializer = new Serializer ([], ['json ' => new JsonEncoder ()], new ChainNormalizer ([ new ObjectNormalizer ()]), new ChainDenormalizer ([ new ObjectNormalizer ()]) );
699
728
700
729
$ validator = $ this ->createMock (ValidatorInterface::class);
701
730
$ validator ->expects ($ this ->never ())
0 commit comments