-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor #15780 [API] Add BC layer for transition from :read to :index a…
…nd :show serialization groups (TheMilek) This PR was merged into the 1.13 branch. Discussion ---------- | Q | A | |-----------------|--------------------------------------------------------------| | Branch? | 1.13 <!-- see the comment below --> | | Bug fix? | no | | New feature? | no | | BC breaks? | no | | Deprecations? | yes <!-- don't forget to update the UPGRADE-*.md file --> | | License | MIT | <!-- - Bug fixes must be submitted against the 1.12 branch - Features and deprecations must be submitted against the 1.13 branch - Make sure that the correct base branch is set To be sure you are not breaking any Backward Compatibilities, check the documentation: https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html --> BC layer for #15770 Commits ------- Add BC layer for transition from :read to :index and :show serialization groups Add parameters that allow skipping adding read ord index and show serialization groups Add information to upgrade file
- Loading branch information
Showing
7 changed files
with
226 additions
and
0 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
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
83 changes: 83 additions & 0 deletions
83
src/Sylius/Bundle/ApiBundle/SerializerContextBuilder/ReadOperationContextBuilder.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,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\SerializerContextBuilder; | ||
|
||
use ApiPlatform\Serializer\SerializerContextBuilderInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class ReadOperationContextBuilder implements SerializerContextBuilderInterface | ||
{ | ||
public function __construct( | ||
private SerializerContextBuilderInterface $decorated, | ||
private bool $skipAddingReadGroup, | ||
private bool $skipAddingIndexAndShowGroups, | ||
) { | ||
} | ||
|
||
/** | ||
* @param array<mixed> $extractedAttributes | ||
* | ||
* @return array<mixed> | ||
*/ | ||
public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array | ||
{ | ||
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes); | ||
|
||
$groups = $context['groups'] ?? []; | ||
$groups = is_string($groups) ? [$groups] : $groups; | ||
|
||
if ($groups === []) { | ||
return $context; | ||
} | ||
|
||
foreach ($groups as $group) { | ||
if ($this->shouldReadGroupBeAdded($group) && !$this->skipAddingReadGroup) { | ||
$readGroup = str_replace([':show', ':index'], ':read', $group); | ||
|
||
if (in_array($readGroup, $groups, true)) { | ||
continue; | ||
} | ||
|
||
$groups[] = $readGroup; | ||
} | ||
|
||
if ($this->shouldIndexAndShowGroupsBeAdded($group) && !$this->skipAddingIndexAndShowGroups) { | ||
$indexGroup = str_replace(':read', ':index', $group); | ||
$showGroup = str_replace(':read', ':show', $group); | ||
|
||
if (!in_array($indexGroup, $groups, true)) { | ||
$groups[] = $indexGroup; | ||
} | ||
|
||
if (!in_array($showGroup, $groups, true)) { | ||
$groups[] = $showGroup; | ||
} | ||
} | ||
} | ||
|
||
$context['groups'] = $groups; | ||
|
||
return $context; | ||
} | ||
|
||
private function shouldReadGroupBeAdded(string $group): bool | ||
{ | ||
return str_ends_with($group, ':show') || str_ends_with($group, ':index'); | ||
} | ||
|
||
private function shouldIndexAndShowGroupsBeAdded(string $group): bool | ||
{ | ||
return str_ends_with($group, ':read'); | ||
} | ||
} |
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
83 changes: 83 additions & 0 deletions
83
...Sylius/Bundle/ApiBundle/spec/SerializerContextBuilder/ReadOperationContextBuilderSpec.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,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Bundle\ApiBundle\SerializerContextBuilder; | ||
|
||
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class ReadOperationContextBuilderSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
SerializerContextBuilderInterface $decoratedSerializerContextBuilder, | ||
): void { | ||
$this->beConstructedWith($decoratedSerializerContextBuilder, false, false); | ||
} | ||
|
||
function it_updates_an_context_with_index_and_show_serialization_groups_if_only_read_provided( | ||
Request $request, | ||
SerializerContextBuilderInterface $decoratedSerializerContextBuilder, | ||
): void { | ||
$decoratedSerializerContextBuilder->createFromRequest($request, true, [])->willReturn([ | ||
'groups' => ['foo:read'], | ||
]); | ||
|
||
$this->createFromRequest($request, true, [])->shouldReturn([ | ||
'groups' => ['foo:read', 'foo:index', 'foo:show'], | ||
]); | ||
} | ||
|
||
function it_updates_an_context_with_read_serialization_groups_if_only_index_and_show_provided( | ||
Request $request, | ||
SerializerContextBuilderInterface $decoratedSerializerContextBuilder, | ||
): void { | ||
$decoratedSerializerContextBuilder->createFromRequest($request, true, [])->willReturn([ | ||
'groups' => ['foo:read'], | ||
]); | ||
|
||
$this->createFromRequest($request, true, [])->shouldReturn([ | ||
'groups' => ['foo:read', 'foo:index', 'foo:show'], | ||
]); | ||
} | ||
|
||
function it_does_not_update_context_with_read_group_if_skip_adding_read_parameter_is_set_to_true( | ||
Request $request, | ||
SerializerContextBuilderInterface $decoratedSerializerContextBuilder, | ||
): void { | ||
$this->beConstructedWith($decoratedSerializerContextBuilder, true, false); | ||
|
||
$decoratedSerializerContextBuilder->createFromRequest($request, true, [])->willReturn([ | ||
'groups' => ['foo:show'], | ||
]); | ||
|
||
$this->createFromRequest($request, true, [])->shouldReturn([ | ||
'groups' => ['foo:show'], | ||
]); | ||
} | ||
|
||
function it_does_not_update_context_with_show_and_index_group_if_skip_adding_show_and_index_is_set_to_true( | ||
Request $request, | ||
SerializerContextBuilderInterface $decoratedSerializerContextBuilder, | ||
): void { | ||
$this->beConstructedWith($decoratedSerializerContextBuilder, false, true); | ||
|
||
$decoratedSerializerContextBuilder->createFromRequest($request, true, [])->willReturn([ | ||
'groups' => ['foo:read'], | ||
]); | ||
|
||
$this->createFromRequest($request, true, [])->shouldReturn([ | ||
'groups' => ['foo:read'], | ||
]); | ||
} | ||
} |