Skip to content

Commit 0f6e177

Browse files
committed
fix
1 parent 16069f7 commit 0f6e177

3 files changed

Lines changed: 74 additions & 38 deletions

File tree

src/Hydra/Serializer/CollectionNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class CollectionNormalizer extends AbstractCollectionNormalizer
4444
self::PRESERVE_COLLECTION_KEYS => false,
4545
];
4646

47-
public function __construct(private readonly ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, private readonly IriConverterInterface $iriConverter, private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, array $defaultContext = [])
47+
public function __construct(private readonly ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, private readonly IriConverterInterface $iriConverter, array $defaultContext = [], private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null)
4848
{
4949
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
5050

@@ -86,7 +86,7 @@ protected function getPaginationData(iterable $object, array $context = []): arr
8686
}
8787

8888
if (!empty($allHydraOperations)) {
89-
$data[$hydraPrefix.'supportedOperation'] = $allHydraOperations;
89+
$data[$hydraPrefix.'operation'] = $allHydraOperations;
9090
}
9191
}
9292

src/Hydra/Tests/Serializer/CollectionNormalizerTest.php

Lines changed: 70 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public function testNormalizeResourceCollectionWithoutPrefix(): void
452452
], $actual);
453453
}
454454

455-
public function testNormalizeCollectionWithHydraOperations(): void
455+
public function testNormalizeResourceCollectionWithHydraOperations(): void
456456
{
457457
$fooOne = new Foo();
458458
$fooOne->id = 1;
@@ -492,8 +492,8 @@ public function testNormalizeCollectionWithHydraOperations(): void
492492
$contextBuilderProphecy->reveal(),
493493
$resourceClassResolverProphecy->reveal(),
494494
$iriConverterProphecy->reveal(),
495-
$resourceMetadataCollectionFactoryProphecy->reveal(),
496-
['hydra_prefix' => false, 'hydra_operations' => true]
495+
['hydra_prefix' => false, 'hydra_operations' => true],
496+
$resourceMetadataCollectionFactoryProphecy->reveal()
497497
);
498498
$normalizer->setNormalizer($delegateNormalizerProphecy->reveal());
499499

@@ -502,22 +502,41 @@ public function testNormalizeCollectionWithHydraOperations(): void
502502
'resource_class' => Foo::class,
503503
]);
504504

505-
$this->assertArrayHasKey('supportedOperation', $actual);
506-
$this->assertIsArray($actual['supportedOperation']);
507-
$this->assertNotEmpty($actual['supportedOperation']);
508-
509-
$methods = array_map(static fn ($op) => $op['method'] ?? null, $actual['supportedOperation']);
510-
$this->assertContains('GET', $methods);
511-
$this->assertContains('POST', $methods);
512-
513-
$this->assertArrayHasKey('@context', $actual);
514-
$this->assertArrayHasKey('@id', $actual);
515-
$this->assertArrayHasKey('@type', $actual);
516-
$this->assertArrayHasKey('member', $actual);
517-
$this->assertArrayHasKey('totalItems', $actual);
505+
$this->assertEquals([
506+
'@context' => '/contexts/Foo',
507+
'@id' => '/foos',
508+
'@type' => 'Collection',
509+
'member' => [
510+
$normalizedFooOne,
511+
],
512+
'totalItems' => 1,
513+
'operation' => [
514+
[
515+
'@type' => [
516+
'Operation',
517+
'schema:FindAction',
518+
],
519+
'description' => 'Retrieves the collection of Foo resources.',
520+
'method' => 'GET',
521+
'returns' => 'Collection',
522+
'title' => 'getFooCollection',
523+
],
524+
[
525+
'@type' => [
526+
'Operation',
527+
'schema:CreateAction',
528+
],
529+
'description' => 'Creates a Foo resource.',
530+
'expects' => 'Foo',
531+
'method' => 'POST',
532+
'returns' => 'Foo',
533+
'title' => 'postFoo',
534+
],
535+
],
536+
], $actual);
518537
}
519538

520-
public function testNormalizeCollectionWithMultipleApiResourcesAndHydraOperations(): void
539+
public function testNormalizeResourceCollectionWithHydraOperationsMultipleApiResource(): void
521540
{
522541
$fooOne = new Foo();
523542
$fooOne->id = 1;
@@ -560,8 +579,8 @@ public function testNormalizeCollectionWithMultipleApiResourcesAndHydraOperation
560579
$contextBuilderProphecy->reveal(),
561580
$resourceClassResolverProphecy->reveal(),
562581
$iriConverterProphecy->reveal(),
563-
$resourceMetadataCollectionFactoryProphecy->reveal(),
564-
['hydra_prefix' => false, 'hydra_operations' => true]
582+
['hydra_prefix' => false, 'hydra_operations' => true],
583+
$resourceMetadataCollectionFactoryProphecy->reveal()
565584
);
566585
$normalizer->setNormalizer($delegateNormalizerProphecy->reveal());
567586

@@ -570,20 +589,37 @@ public function testNormalizeCollectionWithMultipleApiResourcesAndHydraOperation
570589
'resource_class' => Foo::class,
571590
]);
572591

573-
$this->assertArrayHasKey('supportedOperation', $actual);
574-
$this->assertIsArray($actual['supportedOperation']);
575-
576-
$this->assertCount(2, $actual['supportedOperation']);
577-
578-
$methods = array_map(static fn ($op) => $op['method'] ?? null, $actual['supportedOperation']);
579-
$this->assertContains('GET', $methods);
580-
$this->assertContains('POST', $methods);
581-
582-
$this->assertArrayHasKey('@context', $actual);
583-
$this->assertArrayHasKey('@id', $actual);
584-
$this->assertArrayHasKey('@type', $actual);
585-
$this->assertEquals('Collection', $actual['@type']);
586-
$this->assertArrayHasKey('member', $actual);
587-
$this->assertArrayHasKey('totalItems', $actual);
592+
$this->assertEquals([
593+
'@context' => '/contexts/Foo',
594+
'@id' => '/foos',
595+
'@type' => 'Collection',
596+
'member' => [
597+
$normalizedFooOne,
598+
],
599+
'totalItems' => 1,
600+
'operation' => [
601+
[
602+
'@type' => [
603+
'Operation',
604+
'schema:FindAction',
605+
],
606+
'description' => 'Retrieves the collection of Foo resources.',
607+
'method' => 'GET',
608+
'returns' => 'Collection',
609+
'title' => 'getFooCollection',
610+
],
611+
[
612+
'@type' => [
613+
'Operation',
614+
'schema:CreateAction',
615+
],
616+
'description' => 'Creates a Foo resource.',
617+
'expects' => 'Foo',
618+
'method' => 'POST',
619+
'returns' => 'Foo',
620+
'title' => 'postFoo',
621+
],
622+
],
623+
], $actual);
588624
}
589625
}

src/Laravel/ApiPlatformProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,8 @@ public function register(): void
919919
$app->make(ContextBuilderInterface::class),
920920
$app->make(ResourceClassResolverInterface::class),
921921
$app->make(IriConverterInterface::class),
922-
$app->make(ResourceMetadataCollectionFactoryInterface::class),
923-
$defaultContext
922+
$defaultContext,
923+
$app->make(ResourceMetadataCollectionFactoryInterface::class)
924924
),
925925
$app->make(ResourceMetadataCollectionFactoryInterface::class),
926926
$app->make(ResourceClassResolverInterface::class),

0 commit comments

Comments
 (0)