Skip to content

Commit 6a8cf7d

Browse files
committed
add entity path as link and not as property
1 parent 48eab35 commit 6a8cf7d

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

api/config/services.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ services:
8686

8787
App\Serializer\Normalizer\ContentTypeNormalizer:
8888
decorates: 'api_platform.hal.normalizer.item'
89-
arguments:
90-
- '@.inner'
91-
- '@api_platform.route_name_resolver'
9289

9390
App\Serializer\Normalizer\UriTemplateNormalizer:
9491
decorates: 'api_platform.hal.normalizer.entrypoint'

api/src/Entity/Day.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function getDayNumber(): int {
9595
* @return ScheduleEntry[]
9696
*/
9797
#[ApiProperty(example: '/schedule_entries?period=%2Fperiods%2F1a2b3c4d&start%5Bstrictly_before%5D=2022-01-03T00%3A00%3A00%2B00%3A00&end%5Bafter%5D=2022-01-02T00%3A00%3A00%2B00%3A00')]
98-
#[RelatedCollectionLink('scheduleEntry', ['period' => 'period', 'start[strictly_before]' => 'end', 'end[after]' => 'start'])]
98+
#[RelatedCollectionLink(ScheduleEntry::class, ['period' => 'period', 'start[strictly_before]' => 'end', 'end[after]' => 'start'])]
9999
#[Groups(['read'])]
100100
public function getScheduleEntries(): array {
101101
return array_filter(

api/src/Metadata/Resource/Factory/UriTemplateFactory.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,21 @@ public function __construct(
3535
* @return array contains the templated URI (or null when not successful), as well as a boolean flag which
3636
* indicates whether any template parameters are present in the URI
3737
*/
38-
public function create(string $shortName): array {
38+
public function createFromShortname(string $shortName): array {
3939
$resourceClass = $this->resourceNameMapping[lcfirst($shortName)] ?? null;
40+
41+
return $this->createFromResourceClass($resourceClass);
42+
}
43+
44+
/**
45+
* Create an URI template based on the allowed parameters for the specified entity.
46+
*
47+
* @throws ResourceClassNotFoundException
48+
*
49+
* @return array contains the templated URI (or null when not successful), as well as a boolean flag which
50+
* indicates whether any template parameters are present in the URI
51+
*/
52+
public function createFromResourceClass(string $resourceClass): array {
4053
if (!$resourceClass) {
4154
return [null, false];
4255
}

api/src/Serializer/Normalizer/ContentTypeNormalizer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
namespace App\Serializer\Normalizer;
44

5-
use ApiPlatform\Core\Api\OperationType;
6-
use ApiPlatform\Core\Api\UrlGeneratorInterface;
7-
use ApiPlatform\Core\Bridge\Symfony\Routing\RouteNameResolverInterface;
85
use App\Entity\ContentType;
9-
use Symfony\Component\Routing\RouterInterface;
6+
use App\Metadata\Resource\Factory\UriTemplateFactory;
107
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
118
use Symfony\Component\Serializer\SerializerAwareInterface;
129
use Symfony\Component\Serializer\SerializerInterface;
@@ -17,8 +14,7 @@
1714
class ContentTypeNormalizer implements NormalizerInterface, SerializerAwareInterface {
1815
public function __construct(
1916
private NormalizerInterface $decorated,
20-
private RouteNameResolverInterface $routeNameResolver,
21-
private RouterInterface $router,
17+
private UriTemplateFactory $uriTemplateFactory,
2218
) {
2319
}
2420

@@ -30,7 +26,11 @@ public function normalize($object, $format = null, array $context = []) {
3026
$data = $this->decorated->normalize($object, $format, $context);
3127

3228
if ($object instanceof ContentType && isset($data['entityClass'])) {
33-
$data['entityPath'] = $this->router->generate($this->routeNameResolver->getRouteName($data['entityClass'], OperationType::COLLECTION), [], UrlGeneratorInterface::ABS_PATH);
29+
[$uriTemplate, $templated] = $this->uriTemplateFactory->createFromResourceClass($data['entityClass']);
30+
$data['_links']['contentNodes']['href'] = $uriTemplate;
31+
$data['_links']['contentNodes']['templated'] = $templated;
32+
33+
unset($data['entityClass']);
3434
}
3535

3636
return $data;

api/src/Serializer/Normalizer/RelatedCollectionLinkNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function getRelatedCollectionHref($object, $rel, array $context = []): st
138138
if ($annotation = $this->getRelatedCollectionLinkAnnotation($resourceClass, $rel)) {
139139
// If there is an explicit annotation, there is no need to inspect the Doctrine metadata
140140
$params = $this->extractUriParams($object, $annotation->getParams());
141-
[$uriTemplate] = $this->uriTemplateFactory->create($annotation->getRelatedEntity());
141+
[$uriTemplate] = $this->uriTemplateFactory->createFromResourceClass($annotation->getRelatedEntity());
142142

143143
return $this->uriTemplate->expand($uriTemplate, $params);
144144
}

api/src/Serializer/Normalizer/UriTemplateNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function normalize($object, $format = null, array $context = []) {
3131
continue;
3232
}
3333

34-
[$uriTemplate, $templated] = $this->uriTemplateFactory->create($rel);
34+
[$uriTemplate, $templated] = $this->uriTemplateFactory->createFromShortname($rel);
3535
if (!$uriTemplate) {
3636
continue;
3737
}

0 commit comments

Comments
 (0)