Skip to content

Commit c2c2c7b

Browse files
committed
feat: add 'hydra_operations' option (replaced with 'hydra_item_operations_in_collection')
1 parent cfad5d4 commit c2c2c7b

6 files changed

Lines changed: 9 additions & 9 deletions

File tree

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public function normalize(mixed $data, ?string $format = null, array $context =
117117
// TODO: we should not remove the resource_class in the normalizeRawCollection as we would find out anyway that it's not the same as the requested one
118118
$previousResourceClass = $context['resource_class'] ?? null;
119119
$metadata = [];
120-
$isRootResource = !isset($context['jsonld_has_context']);
121120
if ($isResourceClass = $this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
122121
$resourceClass = $this->resourceClassResolver->getResourceClass($data, $previousResourceClass);
123122
if (isset($context['operation']) && $context['operation'] instanceof HttpOperation && $context['operation']->getClass() !== $resourceClass) {
@@ -192,11 +191,10 @@ public function normalize(mixed $data, ?string $format = null, array $context =
192191
$metadata['@type'] = 1 === \count($types) ? $types[0] : $types;
193192
}
194193

195-
if ($isResourceClass && !is_a($resourceClass, ErrorResourceInterface::class, true) && $isRootResource) {
196-
$isCollectionRoute = $context['api_collection_sub_level'] ?? false;
197-
$showItemHydraOperationsInCollection = $context['hydra_item_operations_in_collection'] ?? false;
194+
if ($isResourceClass && !is_a($resourceClass, ErrorResourceInterface::class, true)) {
195+
$showOperations = $context['hydra_operations'] ?? false;
198196

199-
if (!$isCollectionRoute || $showItemHydraOperationsInCollection) {
197+
if ($showOperations) {
200198
$hydraOperations = $this->getHydraOperations(
201199
false,
202200
$this->resourceMetadataCollectionFactory->create($resourceClass)[0],

src/Laravel/config/api-platform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149

150150
'serializer' => [
151151
'hydra_prefix' => false,
152-
'hydra_item_operations_in_collection' => false,
152+
'hydra_operations' => false,
153153
// 'datetime_format' => \DateTimeInterface::RFC3339,
154154
],
155155

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ private function registerCommonConfiguration(ContainerBuilder $container, array
334334

335335
$defaultContext = [
336336
'hydra_prefix' => $config['serializer']['hydra_prefix'],
337-
'hydra_item_operations_in_collection' => $config['serializer']['hydra_item_operations_in_collection'],
337+
'hydra_operations' => $config['serializer']['hydra_operations'],
338338
] + ($container->hasParameter('serializer.default_context') ? $container->getParameter('serializer.default_context') : []);
339339

340340
$container->setParameter('api_platform.serializer.default_context', $defaultContext);

src/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public function getConfigTreeBuilder(): TreeBuilder
172172
->addDefaultsIfNotSet()
173173
->children()
174174
->booleanNode('hydra_prefix')->defaultFalse()->info('Use the "hydra:" prefix.')->end()
175-
->booleanNode('hydra_item_operations_in_collection')->defaultFalse()->info('Include Hydra item operations on each member of a collection response. Disabled by default to reduce payload size.')->end()
175+
->booleanNode('hydra_operations')->defaultFalse()->info('Add the "operation" field to Hydra responses. Disabled by default to avoid breaking changes.')->end()
176176
->end()
177177
->end()
178178
->end();

tests/Fixtures/app/config/config_common.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ api_platform:
3838
Made with love
3939
enable_swagger: true
4040
enable_swagger_ui: true
41+
serializer:
42+
hydra_operations: false
4143
formats:
4244
jsonld: ['application/ld+json']
4345
jsonhal: ['application/hal+json']

tests/Symfony/Bundle/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm
243243
'enable_link_security' => true,
244244
'serializer' => [
245245
'hydra_prefix' => false,
246-
'hydra_item_operations_in_collection' => false,
246+
'hydra_operations' => false,
247247
],
248248
'enable_phpdoc_parser' => true,
249249
'mcp' => [

0 commit comments

Comments
 (0)