Skip to content

Commit 390056f

Browse files
authored
fix(jsonld)!: item uri template type (#7518)
1 parent 33600bf commit 390056f

File tree

5 files changed

+19
-10
lines changed

5 files changed

+19
-10
lines changed

features/hydra/item_uri_template.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ Feature: Exposing a collection of objects should use the specified operation to
146146
"hydra:member":[
147147
{
148148
"@id":"/item_referenced_in_collection/a",
149-
"@type":"CollectionReferencingItem",
149+
"@type":"ItemReferencedInCollection",
150150
"id":"a",
151151
"name":"hello"
152152
},
153153
{
154154
"@id":"/item_referenced_in_collection/b",
155-
"@type":"CollectionReferencingItem",
155+
"@type":"ItemReferencedInCollection",
156156
"id":"b",
157157
"name":"you"
158158
}

features/jsonld/input_output.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ Feature: JSON-LD DTO input and output
402402
"required": ["@id", "@type", "foo", "bar"],
403403
"properties": {
404404
"@id": {"pattern": "^/.well-known/genid/.+$"},
405-
"@type": {"pattern": "^DummyCollectionDtoOutput$"},
405+
"@type": {"pattern": "^DummyFooCollectionDto$"},
406406
"foo": {"type": "string"},
407407
"bar": {"type": "integer"}
408408
}

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
1919
use ApiPlatform\Metadata\HttpOperation;
2020
use ApiPlatform\Metadata\IriConverterInterface;
21+
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
2122
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2223
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2324
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
@@ -70,7 +71,7 @@ final class ItemNormalizer extends AbstractItemNormalizer
7071
'@vocab',
7172
];
7273

73-
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, private readonly ContextBuilderInterface $contextBuilder, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null)
74+
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, private readonly ContextBuilderInterface $contextBuilder, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null, private ?OperationMetadataFactoryInterface $operationMetadataFactory = null)
7475
{
7576
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
7677
}
@@ -118,7 +119,9 @@ public function normalize(mixed $object, ?string $format = null, array $context
118119
$context['output']['iri'] = null;
119120
}
120121

121-
if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
122+
if (isset($context['item_uri_template']) && $this->operationMetadataFactory) {
123+
$context['output']['operation'] = $this->operationMetadataFactory->create($context['item_uri_template']);
124+
} elseif ($this->resourceClassResolver->isResourceClass($resourceClass)) {
122125
$context['output']['operation'] = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
123126
}
124127

@@ -144,6 +147,11 @@ public function normalize(mixed $object, ?string $format = null, array $context
144147
}
145148

146149
$operation = $context['operation'] ?? null;
150+
151+
if ($this->operationMetadataFactory && isset($context['item_uri_template']) && !$operation) {
152+
$operation = $this->operationMetadataFactory->create($context['item_uri_template']);
153+
}
154+
147155
if ($isResourceClass && !$operation) {
148156
$operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
149157
}

src/Symfony/Bundle/Resources/config/jsonld.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
'%api_platform.serializer.default_context%',
4343
service('api_platform.security.resource_access_checker')->ignoreOnInvalid(),
4444
service('api_platform.http_cache.tag_collector')->ignoreOnInvalid(),
45+
service('api_platform.metadata.operation.metadata_factory')->ignoreOnInvalid()
4546
])
4647
->tag('serializer.normalizer', ['priority' => -890]);
4748

tests/Functional/JsonLdTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ public function testItemUriTemplate(): void
156156
$this->assertJsonContains([
157157
'member' => [
158158
[
159-
'@type' => 'RecipeCollection',
159+
'@type' => 'ItemRecipe',
160160
'@id' => '/item_uri_template_recipes/1',
161161
'name' => 'Dummy Recipe',
162162
],
163163
[
164-
'@type' => 'RecipeCollection',
164+
'@type' => 'ItemRecipe',
165165
'@id' => '/item_uri_template_recipes/2',
166166
'name' => 'Dummy Recipe 2',
167167
],
@@ -204,17 +204,17 @@ public function testItemUriTemplateWithStateOption(): void
204204
$this->assertJsonContains([
205205
'member' => [
206206
[
207-
'@type' => 'Recipe',
207+
'@type' => 'ItemRecipe',
208208
'@id' => '/item_uri_template_recipes_state_option/1',
209209
'name' => 'Recipe 0',
210210
],
211211
[
212-
'@type' => 'Recipe',
212+
'@type' => 'ItemRecipe',
213213
'@id' => '/item_uri_template_recipes_state_option/2',
214214
'name' => 'Recipe 1',
215215
],
216216
[
217-
'@type' => 'Recipe',
217+
'@type' => 'ItemRecipe',
218218
'@id' => '/item_uri_template_recipes_state_option/3',
219219
'name' => 'Recipe 2',
220220
],

0 commit comments

Comments
 (0)