Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,16 @@ private function getClass(string $resourceClass, ApiResource $resourceMetadata,
*/
private function getPropertyMetadataFactoryContext(ApiResource $resourceMetadata): array
{
$normalizationGroups = $resourceMetadata->getNormalizationContext()[AbstractNormalizer::GROUPS] ?? null;
$denormalizationGroups = $resourceMetadata->getDenormalizationContext()[AbstractNormalizer::GROUPS] ?? null;
$normalizationGroups = ($resourceMetadata->getNormalizationContext() ?? [])[AbstractNormalizer::GROUPS] ?? null;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably just type somewhere that our groups should be array shaped. I think there are other places in the code were we rely on that behavior.

$denormalizationGroups = ($resourceMetadata->getDenormalizationContext() ?? [])[AbstractNormalizer::GROUPS] ?? null;

if (\is_string($normalizationGroups)) {
$normalizationGroups = [$normalizationGroups];
}
if (\is_string($denormalizationGroups)) {
$denormalizationGroups = [$denormalizationGroups];
}

$propertyContext = [
'normalization_groups' => $normalizationGroups,
'denormalization_groups' => $denormalizationGroups,
Expand Down
48 changes: 48 additions & 0 deletions src/Hydra/Tests/Serializer/DocumentationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1170,4 +1170,52 @@ public function testNormalizeNoEntrypointAndHideHydraOperation(): void

$this->assertEquals($expected, $documentationNormalizer->normalize($documentation, null, [ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX => false]));
}

public function testNormalizeWithStringGroups(): void
{
$title = 'Test Api';
$desc = 'test';
$version = '0.0.0';
$documentation = new Documentation(new ResourceNameCollection(['dummy' => 'dummy']), $title, $desc, $version);

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create('dummy', Argument::type('array'))->shouldBeCalled()->willReturn(new PropertyNameCollection(['name']));

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create('dummy', 'name', Argument::type('array'))->shouldBeCalled()->willReturn(
(new ApiProperty())->withNativeType(Type::string())->withDescription('name')->withReadable(true)->withWritable(true)->withReadableLink(true)->withWritableLink(true)
);

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('dummy')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('dummy', [
(new ApiResource())
->withShortName('dummy')
->withOperations(new Operations([
'get' => (new Get())->withShortName('dummy'),
]))
->withNormalizationContext(['groups' => 'read'])
->withDenormalizationContext(['groups' => 'write']),
]));

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->isResourceClass(Argument::type('string'))->willReturn(false);

$urlGenerator = $this->prophesize(UrlGeneratorInterface::class);
$urlGenerator->generate('api_entrypoint')->willReturn('/');
$urlGenerator->generate('api_doc', ['_format' => 'jsonld'])->willReturn('/doc');
$urlGenerator->generate('api_doc', ['_format' => 'jsonld'], 0)->willReturn('/doc');

$documentationNormalizer = new DocumentationNormalizer(
$resourceMetadataFactoryProphecy->reveal(),
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal(),
$resourceClassResolverProphecy->reveal(),
$urlGenerator->reveal()
);

$doc = $documentationNormalizer->normalize($documentation);

$this->assertSame('#dummy', $doc['hydra:supportedClass'][0]['@id']);
$this->assertNotEmpty($doc['hydra:supportedClass'][0]['hydra:supportedProperty']);
}
}
Loading