Skip to content

Commit 651571b

Browse files
committed
fix(jsonld): item uri template type
1 parent bea3192 commit 651571b

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ Feature: JSON-LD DTO input and output
381381
Then the response status code should be 200
382382
And the response should be in JSON
383383
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
384+
Then print last JSON response
384385
And the JSON should be valid according to this schema:
385386
"""
386387
{
@@ -402,7 +403,7 @@ Feature: JSON-LD DTO input and output
402403
"required": ["@id", "@type", "foo", "bar"],
403404
"properties": {
404405
"@id": {"pattern": "^/.well-known/genid/.+$"},
405-
"@type": {"pattern": "^DummyCollectionDtoOutput$"},
406+
"@type": {"pattern": "^DummyFooCollectionDto$"},
406407
"foo": {"type": "string"},
407408
"bar": {"type": "integer"}
408409
}

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
}
@@ -115,7 +116,9 @@ public function normalize(mixed $object, ?string $format = null, array $context
115116
$context['output']['iri'] = null;
116117
}
117118

118-
if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
119+
if (isset($context['item_uri_template']) && $this->operationMetadataFactory) {
120+
$context['output']['operation'] = $this->operationMetadataFactory->create($context['item_uri_template']);
121+
} elseif ($this->resourceClassResolver->isResourceClass($resourceClass)) {
119122
$context['output']['operation'] = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
120123
}
121124

@@ -141,6 +144,11 @@ public function normalize(mixed $object, ?string $format = null, array $context
141144
}
142145

143146
$operation = $context['operation'] ?? null;
147+
148+
if ($this->operationMetadataFactory && isset($context['item_uri_template']) && !$operation) {
149+
$operation = $this->operationMetadataFactory->create($context['item_uri_template']);
150+
}
151+
144152
if ($isResourceClass && !$operation) {
145153
$operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
146154
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<argument>%api_platform.serializer.default_context%</argument>
3232
<argument type="service" id="api_platform.security.resource_access_checker" on-invalid="ignore" />
3333
<argument type="service" id="api_platform.http_cache.tag_collector" on-invalid="ignore" />
34+
<argument type="service" id="api_platform.metadata.operation.metadata_factory" on-invalid="ignore" />
3435

3536
<!-- Run before serializer.normalizer.json_serializable -->
3637
<tag name="serializer.normalizer" priority="-890" />

tests/Fixtures/TestBundle/ApiResource/ItemUriTemplateWithCollection/Recipe.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ class Recipe
5151

5252
public ?string $totalTime = null;
5353

54-
public ?string $recipeCategory = null;
55-
5654
public ?string $recipeCuisine = null;
5755

5856
public ?string $suitableForDiet = null;
@@ -66,8 +64,6 @@ public static function provide(Operation $operation, array $uriVariables = [], a
6664
$recipe->prepTime = 'PT15M';
6765
$recipe->cookTime = 'PT30M';
6866
$recipe->totalTime = 'PT45M';
69-
$recipe->recipeYield = '2 servings';
70-
$recipe->recipeCategory = ['Lunch', 'Dinner'];
7167
$recipe->recipeIngredient = ['Ingredient 1', 'Ingredient 2'];
7268
$recipe->recipeInstructions = 'Do these things.';
7369

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' => 'ItemRecipe',
159+
'@type' => 'RecipeCollection',
160160
'@id' => '/item_uri_template_recipes/1',
161161
'name' => 'Dummy Recipe',
162162
],
163163
[
164-
'@type' => 'ItemRecipe',
164+
'@type' => 'RecipeCollection',
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' => 'ItemRecipe',
207+
'@type' => 'Recipe',
208208
'@id' => '/item_uri_template_recipes_state_option/1',
209209
'name' => 'Recipe 0',
210210
],
211211
[
212-
'@type' => 'ItemRecipe',
212+
'@type' => 'Recipe',
213213
'@id' => '/item_uri_template_recipes_state_option/2',
214214
'name' => 'Recipe 1',
215215
],
216216
[
217-
'@type' => 'ItemRecipe',
217+
'@type' => 'Recipe',
218218
'@id' => '/item_uri_template_recipes_state_option/3',
219219
'name' => 'Recipe 2',
220220
],

0 commit comments

Comments
 (0)