Skip to content

Commit

Permalink
[RequestMapper]: Fix for validation of nested objects, when it is not…
Browse files Browse the repository at this point in the history
… present in request
  • Loading branch information
fractalzombie committed Nov 22, 2021
1 parent aa9d74d commit c22b79b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
4 changes: 3 additions & 1 deletion Data/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use FRZB\Component\RequestMapper\Utils\StringUtil;
use JetBrains\PhpStorm\Immutable;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\Validator\ConstraintViolation;

#[Immutable]
Expand All @@ -18,10 +19,11 @@ final class Error
public function __construct(string $type, string $field, string $message)
{
$this->type = $type;
$this->field = StringUtil::removeBrackets($field);
$this->field = $field;
$this->message = $message;
}

#[Pure]
public static function fromConstraint(ConstraintViolation $violation): self
{
return new self($violation->getConstraint()::class, $violation->getPropertyPath(), $violation->getMessage());
Expand Down
5 changes: 3 additions & 2 deletions Extractor/ParametersExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace FRZB\Component\RequestMapper\Extractor;

use FRZB\Component\DependencyInjection\Attribute\AsService;
use FRZB\Component\RequestMapper\Utils\ClassUtil;
use FRZB\Component\RequestMapper\Utils\SerializerUtil;
use Symfony\Component\Validator\Constraints\Collection;

Expand Down Expand Up @@ -38,8 +39,8 @@ private function mapProperties(array $properties, array $parameters, ?Collection

foreach ($properties as $serializedName => [$propertyName, $propertyType, $isAllowsNull]) {
$value = $parameters[$serializedName] ?? null;
$isComplexType = \is_array($value) && class_exists($propertyType);
$mapped[$serializedName] = $isComplexType ? $this->extract($propertyType, $value) : $value;
$isComplexType = ClassUtil::isNotBuiltinAndExists($propertyType);
$mapped[$serializedName] = $isComplexType ? $this->extract($propertyType, $value ?? []) : $value;
}

return $mapped;
Expand Down
10 changes: 5 additions & 5 deletions Tests/Func/Converter/RequestConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function caseProvider(): iterable
$attribute = new ParamConverter(parameterClass: CreateUserRequest::class);
$request = RequestHelper::makeRequest(method: Request::METHOD_POST);
$errors = [
new Error(NotBlank::class, 'name', 'This value should not be blank.'),
new Error(NotBlank::class, '[name]', 'This value should not be blank.'),
];

yield 'Converter data with empty params' => [
Expand All @@ -92,10 +92,10 @@ public function caseProvider(): iterable
$attribute = new ParamConverter(parameterClass: CreateUserRequest::class);
$request = RequestHelper::makeRequest(method: Request::METHOD_POST, params: $params);
$errors = [
new Error(Type::class, 'name', 'This value should be of type string.'),
new Error(Uuid::class, 'userId', 'This is not a valid UUID.'),
new Error(Type::class, 'userId', 'This value should be of type string.'),
new Error(Type::class, 'amount', 'This value should be of type float.'),
new Error(Type::class, '[name]', 'This value should be of type string.'),
new Error(Uuid::class, '[userId]', 'This is not a valid UUID.'),
new Error(Type::class, '[userId]', 'This value should be of type string.'),
new Error(Type::class, '[amount]', 'This value should be of type float.'),
];

yield 'Converter data with invalid params' => [
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "frzb/request-mapper",
"type": "library",
"version": "v1.2.3",
"description": "The RequestMapper component allows you to serialize JSON request to Request class and validate it",
"keywords": [
"library",
Expand Down
44 changes: 23 additions & 21 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c22b79b

Please sign in to comment.