-
-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(serializer): json violation list normalizer (#5941)
- Loading branch information
Showing
5 changed files
with
127 additions
and
1 deletion.
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
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
44 changes: 44 additions & 0 deletions
44
src/Symfony/Validator/Serializer/ConstraintViolationListNormalizer.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,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Symfony\Validator\Serializer; | ||
|
||
use ApiPlatform\Serializer\AbstractConstraintViolationListNormalizer; | ||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; | ||
|
||
/** | ||
* Converts {@see \Symfony\Component\Validator\ConstraintViolationListInterface} the API Problem spec (RFC 7807). | ||
* | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
final class ConstraintViolationListNormalizer extends AbstractConstraintViolationListNormalizer | ||
{ | ||
public const FORMAT = 'json'; | ||
|
||
private array $defaultContext = []; | ||
|
||
public function __construct(array $serializePayloadFields = null, NameConverterInterface $nameConverter = null, array $defaultContext = []) | ||
{ | ||
parent::__construct($serializePayloadFields, $nameConverter); | ||
|
||
$this->defaultContext = array_merge($this->defaultContext, $defaultContext); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize(mixed $object, string $format = null, array $context = []): array | ||
{ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Issue5912; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\NotExposed; | ||
use ApiPlatform\Metadata\Post; | ||
use Symfony\Component\Validator\Constraints\NotBlank; | ||
|
||
#[ApiResource( | ||
shortName: 'Issue5912', | ||
operations: [ | ||
new NotExposed(), | ||
new Post(), | ||
] | ||
)] | ||
class Dummy | ||
{ | ||
#[NotBlank] | ||
public string $title; | ||
} |