-
-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(validation): normalize constraint violation list (#5866)
- Loading branch information
Showing
11 changed files
with
328 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Hal\Serializer; | ||
|
||
use ApiPlatform\Serializer\AbstractConstraintViolationListNormalizer; | ||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; | ||
|
||
/** | ||
* Converts {@see \Symfony\Component\Validator\ConstraintViolationListInterface} to a Hal error representation. | ||
*/ | ||
final class ConstraintViolationListNormalizer extends AbstractConstraintViolationListNormalizer | ||
{ | ||
public const FORMAT = 'json'; | ||
|
||
public function __construct(array $serializePayloadFields = null, NameConverterInterface $nameConverter = null) | ||
{ | ||
parent::__construct($serializePayloadFields, $nameConverter); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null | ||
{ | ||
return $this->getViolations($object); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
tests/Fixtures/TestBundle/Document/DummyValidationSerializedName.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fixtures\TestBundle\Document; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Post; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
use Symfony\Component\Serializer\Annotation\SerializedName; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
#[ApiResource(operations: [ | ||
new GetCollection(), | ||
new Post(uriTemplate: 'dummy_validation_serialized_name'), | ||
] | ||
)] | ||
#[ODM\Document] | ||
class DummyValidationSerializedName | ||
{ | ||
/** | ||
* @var int|null The id | ||
*/ | ||
#[ODM\Id(strategy: 'INCREMENT', type: 'int')] | ||
private ?int $id = null; | ||
/** | ||
* @var string|null The dummy title | ||
*/ | ||
#[ODM\Field(type: 'string', nullable: true)] | ||
#[Assert\NotNull()] | ||
#[SerializedName('test')] | ||
private ?string $title = null; | ||
/** | ||
* @var string The dummy code | ||
*/ | ||
#[ODM\Field(type: 'string')] | ||
private string $code; | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId(int $id): self | ||
{ | ||
$this->id = $id; | ||
|
||
return $this; | ||
} | ||
|
||
public function getTitle(): ?string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function setTitle(?string $title): self | ||
{ | ||
$this->title = $title; | ||
|
||
return $this; | ||
} | ||
|
||
public function getCode(): ?string | ||
{ | ||
return $this->code; | ||
} | ||
|
||
public function setCode(string $code): self | ||
{ | ||
$this->code = $code; | ||
|
||
return $this; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
tests/Fixtures/TestBundle/Entity/DummyValidationSerializedName.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Post; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Serializer\Annotation\SerializedName; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
#[ApiResource(operations: [ | ||
new GetCollection(), | ||
new Post(uriTemplate: 'dummy_validation_serialized_name'), | ||
] | ||
)] | ||
#[ORM\Entity] | ||
class DummyValidationSerializedName | ||
{ | ||
/** | ||
* @var int|null The id | ||
*/ | ||
#[ORM\Column(type: 'integer')] | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
private ?int $id = null; | ||
/** | ||
* @var string|null The dummy title | ||
*/ | ||
#[ORM\Column(nullable: true)] | ||
#[Assert\NotNull()] | ||
#[SerializedName('test')] | ||
private ?string $title = null; | ||
/** | ||
* @var string The dummy code | ||
*/ | ||
#[ORM\Column] | ||
private string $code; | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId(int $id): self | ||
{ | ||
$this->id = $id; | ||
|
||
return $this; | ||
} | ||
|
||
public function getTitle(): ?string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function setTitle(?string $title): self | ||
{ | ||
$this->title = $title; | ||
|
||
return $this; | ||
} | ||
|
||
public function getCode(): ?string | ||
{ | ||
return $this->code; | ||
} | ||
|
||
public function setCode(string $code): self | ||
{ | ||
$this->code = $code; | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.