Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita25d919

Browse files
committed
#[MapRequestPayload][Serializer] improve nested payload validation for MapRequestPayload using a new serialization context
1 parentaeb2489 commita25d919

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

‎src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/RequestPayloadValueResolverTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,50 @@ public function testValidationNotPerformedWhenPartialDenormalizationReturnsViola
285285
}
286286
}
287287

288+
publicfunctiontestNestedPayloadErrorReportingWhenPartialDenormalizationReturnsViolation()
289+
{
290+
$content ='{
291+
"name": "john doe",
292+
"address": {
293+
"address": "2332 street",
294+
"zipcode": "20220",
295+
"city": "Paris",
296+
"country": "75000",
297+
"geolocalization": {
298+
"lng": 32.423
299+
}
300+
}
301+
}';
302+
$serializer =newSerializer([newObjectNormalizer()], ['json' =>newJsonEncoder()]);
303+
304+
$validator =$this->createMock(ValidatorInterface::class);
305+
$validator->expects($this->never())
306+
->method('validate');
307+
308+
$resolver =newRequestPayloadValueResolver($serializer,$validator);
309+
$request = Request::create('/','POST', server: ['CONTENT_TYPE' =>'application/json'], content:$content);
310+
$kernel =$this->createMock(HttpKernelInterface::class);
311+
312+
// Test using use_class_as_default_expected_type context
313+
$argument =newArgumentMetadata('invalid-nested-payload', Employee::class,false,false,null,false, [
314+
MapRequestPayload::class =>newMapRequestPayload(serializationContext: ['use_class_as_default_expected_type' =>true]),
315+
]);
316+
$arguments =$resolver->resolve($request,$argument);
317+
$event =newControllerArgumentsEvent($kernel,function () {},$arguments,$request, HttpKernelInterface::MAIN_REQUEST);
318+
319+
try {
320+
$resolver->onKernelControllerArguments($event);
321+
$this->fail(sprintf('Expected "%s" to be thrown.', HttpException::class));
322+
}catch (HttpException$e) {
323+
$validationFailedException =$e->getPrevious();
324+
$this->assertInstanceOf(ValidationFailedException::class,$validationFailedException);
325+
$this->assertSame(
326+
sprintf('This value should be of type %s.', Geolocalization::class),
327+
$validationFailedException->getViolations()[0]->getMessage()
328+
);
329+
}
330+
}
331+
288332
publicfunctiontestUnsupportedMedia()
289333
{
290334
$serializer =newSerializer();
@@ -731,3 +775,34 @@ public function getPassword(): string
731775
return$this->password;
732776
}
733777
}
778+
779+
class Employee
780+
{
781+
publicfunction__construct(
782+
publicstring$name,
783+
#[Assert\Valid]
784+
public ?Address$address =null,
785+
) {
786+
}
787+
}
788+
789+
class Address
790+
{
791+
publicfunction__construct(
792+
publicstring$address,
793+
publicstring$zipcode,
794+
publicstring$city,
795+
publicstring$country,
796+
publicGeolocalization$geolocalization,
797+
) {
798+
}
799+
}
800+
801+
class Geolocalization
802+
{
803+
publicfunction__construct(
804+
publicstring$lat,
805+
publicstring$lng,
806+
) {
807+
}
808+
}

‎src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
* Add`AbstractNormalizer::USE_CLASS_AS_DEFAULT_EXPECTED_TYPE` in order to use the FQCN as the default value for NotNormalizableValueException's expectedTypes instead of unknown
7+
48
7.0
59
---
610

‎src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
118118
*/
119119
publicconstREQUIRE_ALL_PROPERTIES ='require_all_properties';
120120

121+
/**
122+
* Use class name as default expected type when throwing NotNormalizableValueException instead of unknown
123+
*/
124+
publicconstUSE_CLASS_AS_DEFAULT_EXPECTED_TYPE ='use_class_as_default_expected_type';
125+
121126
/**
122127
* @internal
123128
*/
@@ -380,7 +385,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
380385
$exception = NotNormalizableValueException::createForUnexpectedDataType(
381386
sprintf('Failed to create object because the class misses the "%s" property.',$constructorParameter->name),
382387
$data,
383-
['unknown'],
388+
[isset($context[self::USE_CLASS_AS_DEFAULT_EXPECTED_TYPE]) ?$class :'unknown'],
384389
$context['deserialization_path'] ??null,
385390
true
386391
);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp