-
-
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(serializer): dynamic groups should not be cached (#5207)
fixes #5202
- Loading branch information
Showing
5 changed files
with
74 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@!mongodb | ||
Feature: Dynamic serialization context | ||
In order to customize the Resource representation dynamically | ||
As a developer | ||
I should be able to add and remove groups | ||
|
||
@createSchema | ||
Scenario: | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "GET" request to "/relation_group_impact_on_collections/1" | ||
And the JSON node "related.title" should be equal to "foo" |
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
51 changes: 51 additions & 0 deletions
51
tests/Fixtures/TestBundle/Serializer/Normalizer/AddGroupNormalizer.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,51 @@ | ||
<?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\Serializer\Normalizer; | ||
|
||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelationGroupImpactOnCollection; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
final class AddGroupNormalizer implements NormalizerAwareInterface, NormalizerInterface | ||
{ | ||
use NormalizerAwareTrait; | ||
|
||
private const ALREADY_CALLED = 'RELATED_GROUP_IMPACT_ON_COLLECTION_NORMALIZER_ALREADY_CALLED'; | ||
|
||
public function normalize($object, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject | ||
{ | ||
$context[self::ALREADY_CALLED] = true; | ||
if (!($operation = $context['operation'] ?? null)) { | ||
return $this->normalizer->normalize($object, $format, $context); | ||
} | ||
|
||
if ($operation instanceof Get && '/custom_normalizer_relation_group_impact_on_collection' === $operation->getUriTemplate()) { | ||
$context['groups'] = ['related']; | ||
} | ||
|
||
return $this->normalizer->normalize($object, $format, $context); | ||
} | ||
|
||
public function supportsNormalization($data, $format = null, array $context = []): bool | ||
{ | ||
// Make sure we're not called twice | ||
if (isset($context[self::ALREADY_CALLED])) { | ||
return false; | ||
} | ||
|
||
return $data instanceof RelationGroupImpactOnCollection; | ||
} | ||
} |
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