Skip to content

Commit b458b55

Browse files
authored
Merge pull request #6772 from soyuka/4.0
2 parents 67fbe51 + b834d84 commit b458b55

File tree

6 files changed

+50
-8
lines changed

6 files changed

+50
-8
lines changed

features/main/validation.feature

+4-4
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ Feature: Using validations groups
113113
{
114114
"@context": "/contexts/ConstraintViolationList",
115115
"@type": "ConstraintViolationList",
116-
"title": "An error occurred",
117-
"description": "This value should be of type unknown.\nqux: This value should be of type string.\nfoo: This value should be of type bool.\nbar: This value should be of type int.\nuuid: This value should be of type uuid.\nrelatedDummy: This value should be of type array|string.\nrelatedDummies: This value should be of type array.",
116+
"hydra:title": "An error occurred",
117+
"hydra:description": "baz: This value should be of type string.\nqux: This value should be of type string.\nfoo: This value should be of type bool.\nbar: This value should be of type int.\nuuid: This value should be of type uuid.\nrelatedDummy: This value should be of type array|string.\nrelatedDummies: This value should be of type array.",
118118
"violations": [
119119
{
120-
"propertyPath": "",
121-
"message": "This value should be of type unknown.",
120+
"propertyPath": "baz",
121+
"message": "This value should be of type string.",
122122
"code": "ba785a8c-82cb-4283-967c-3cf342181b40",
123123
"hint": "Failed to create object because the class misses the \"baz\" property."
124124
},

phpstan.neon.dist

+11
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ parameters:
8282
path: src/JsonApi/Tests/Serializer/ItemNormalizerTest.php
8383
- '#Method GraphQL\\Type\\Definition\\WrappingType::getWrappedType\(\) invoked with 1 parameter, 0 required\.#'
8484
- '#Access to an undefined property GraphQL\\Type\\Definition\\NamedType&GraphQL\\Type\\Definition\\Type::\$name\.#'
85+
-
86+
message: '#Instanceof between Symfony\\Component\\Serializer\\NameConverter\\NameConverterInterface and Symfony\\Component\\Serializer\\NameConverter\\MetadataAwareNameConverter will always evaluate to false\.#'
87+
paths:
88+
- src/GraphQl/Serializer/SerializerContextBuilder.php
89+
- src/GraphQl/Type/FieldsBuilder.php
90+
-
91+
message: '#Instanceof between Symfony\\Component\\Serializer\\NameConverter\\NameConverterInterface\|null and Symfony\\Component\\Serializer\\NameConverter\\MetadataAwareNameConverter will always evaluate to false\.#'
92+
paths:
93+
- src/Serializer/AbstractConstraintViolationListNormalizer.php
94+
- src/Symfony/Validator/Serializer/ValidationExceptionNormalizer.php
95+
8596
# See https://github.com/phpstan/phpstan-symfony/issues/27
8697
-
8798
message: '#^Service "[^"]+" is private.$#'

src/Doctrine/Orm/Extension/ParameterExtension.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Doctrine\Common\ParameterValueExtractorTrait;
1717
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
1818
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
19+
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
1920
use ApiPlatform\Metadata\Operation;
2021
use ApiPlatform\State\ParameterNotFound;
2122
use Doctrine\ORM\QueryBuilder;
@@ -50,9 +51,11 @@ private function applyFilter(QueryBuilder $queryBuilder, QueryNameGeneratorInter
5051
}
5152

5253
$filter = $this->filterLocator->has($filterId) ? $this->filterLocator->get($filterId) : null;
53-
if ($filter instanceof FilterInterface) {
54-
$filter->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation, ['filters' => $values, 'parameter' => $parameter] + $context);
54+
if (!$filter instanceof FilterInterface) {
55+
throw new InvalidArgumentException(\sprintf('Could not find filter "%s" for parameter "%s" in operation "%s" for resource "%s".', $filterId, $parameter->getKey(), $operation?->getShortName(), $resourceClass));
5556
}
57+
58+
$filter->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation, ['filters' => $values, 'parameter' => $parameter] + $context);
5659
}
5760
}
5861

src/Serializer/AbstractItemNormalizer.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,20 @@ protected function instantiateObject(array &$data, string $class, array &$contex
332332
$missingConstructorArguments[] = $constructorParameter->name;
333333
}
334334

335-
$exception = NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name), $data, ['unknown'], $context['deserialization_path'] ?? null, true);
335+
$attributeContext = $this->getAttributeDenormalizationContext($class, $paramName, $context);
336+
$constructorParameterType = 'unknown';
337+
$reflectionType = $constructorParameter->getType();
338+
if ($reflectionType instanceof \ReflectionNamedType) {
339+
$constructorParameterType = $reflectionType->getName();
340+
}
341+
342+
$exception = NotNormalizableValueException::createForUnexpectedDataType(
343+
\sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name),
344+
null,
345+
[$constructorParameterType],
346+
$attributeContext['deserialization_path'] ?? null,
347+
true
348+
);
336349
$context['not_normalizable_value_exceptions'][] = $exception;
337350
}
338351
}

src/Validator/Exception/ValidationException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function __construct(string|ConstraintViolationListInterface $message = '
7373

7474
if ($message instanceof ConstraintViolationListInterface) {
7575
$this->constraintViolationList = $message;
76-
parent::__construct($code ?? $this->__toString(), $previous ?? 0, $errorTitle instanceof \Throwable ? $errorTitle : null);
76+
parent::__construct($this->__toString(), $code ?? 0, $previous);
7777

7878
return;
7979
}

src/Validator/Tests/Exception/ValidationExceptionTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,19 @@ public function testToString(): void
3939
TXT
4040
), $e->__toString());
4141
}
42+
43+
public function testWithPrevious(): void
44+
{
45+
$previous = new \Exception();
46+
$e = new ValidationException(new ConstraintViolationList([
47+
new ConstraintViolation('message 1', '', [], '', '', 'invalid'),
48+
]), null, $previous);
49+
$this->assertInstanceOf(\RuntimeException::class, $e);
50+
51+
$this->assertSame(str_replace(\PHP_EOL, "\n", <<<TXT
52+
message 1
53+
TXT
54+
), $e->__toString());
55+
$this->assertSame($previous, $e->getPrevious());
56+
}
4257
}

0 commit comments

Comments
 (0)