Skip to content

Commit 41f0d19

Browse files
committed
Fix api-platform/admin#93: use a name converter if defined in the Hydra documentation
1 parent 1b650ee commit 41f0d19

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/Bridge/Symfony/Bundle/Resources/config/hydra.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<argument type="service" id="api_platform.operation_method_resolver" />
1414
<argument type="service" id="api_platform.router" />
1515
<argument type="service" id="api_platform.subresource_operation_factory" />
16+
<argument type="service" id="api_platform.name_converter" on-invalid="ignore" />
1617

1718
<tag name="serializer.normalizer" priority="32" />
1819
</service>

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2828
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
2929
use Symfony\Component\PropertyInfo\Type;
30+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
3031
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3132
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
3233

@@ -46,8 +47,9 @@ final class DocumentationNormalizer implements NormalizerInterface
4647
private $operationMethodResolver;
4748
private $urlGenerator;
4849
private $subresourceOperationFactory;
50+
private $nameConverter;
4951

50-
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, UrlGeneratorInterface $urlGenerator, SubresourceOperationFactoryInterface $subresourceOperationFactory = null)
52+
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, UrlGeneratorInterface $urlGenerator, SubresourceOperationFactoryInterface $subresourceOperationFactory = null, NameConverterInterface $nameConverter = null)
5153
{
5254
$this->resourceMetadataFactory = $resourceMetadataFactory;
5355
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
@@ -56,6 +58,7 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
5658
$this->operationMethodResolver = $operationMethodResolver;
5759
$this->urlGenerator = $urlGenerator;
5860
$this->subresourceOperationFactory = $subresourceOperationFactory;
61+
$this->nameConverter = $nameConverter;
5962
}
6063

6164
/**
@@ -194,6 +197,10 @@ private function getHydraProperties(string $resourceClass, ResourceMetadata $res
194197
continue;
195198
}
196199

200+
if ($this->nameConverter) {
201+
$propertyName = $this->nameConverter->normalize($propertyName);
202+
}
203+
197204
$properties[] = $this->getProperty($propertyMetadata, $propertyName, $prefixedShortName, $shortName);
198205
}
199206

tests/Hydra/Serializer/DocumentationNormalizerTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2828
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
2929
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
30+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Serializer\NameConverter\CustomConverter;
3031
use PHPUnit\Framework\TestCase;
3132
use Prophecy\Argument;
3233
use Symfony\Component\PropertyInfo\Type;
@@ -44,7 +45,7 @@ public function testNormalize()
4445
$documentation = new Documentation(new ResourceNameCollection(['dummy' => 'dummy']), $title, $desc, $version, []);
4546

4647
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
47-
$propertyNameCollectionFactoryProphecy->create('dummy', [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['name', 'description', 'relatedDummy']));
48+
$propertyNameCollectionFactoryProphecy->create('dummy', [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['name', 'description', 'nameConverted', 'relatedDummy']));
4849

4950
$dummyMetadata = new ResourceMetadata('dummy', 'dummy', '#dummy', ['get' => ['method' => 'GET', 'hydra_context' => ['hydra:foo' => 'bar', 'hydra:title' => 'foobar']], 'put' => ['method' => 'PUT']], ['get' => ['method' => 'GET'], 'post' => ['method' => 'POST']], []);
5051
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
@@ -54,6 +55,7 @@ public function testNormalize()
5455
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
5556
$propertyMetadataFactoryProphecy->create('dummy', 'name')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'name', true, true, true, true, false, false, null, null, []));
5657
$propertyMetadataFactoryProphecy->create('dummy', 'description')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'description', true, true, true, true, false, false, null, null, ['jsonld_context' => ['@type' => '@id']]));
58+
$propertyMetadataFactoryProphecy->create('dummy', 'nameConverted')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'name converted', true, true, true, true, false, false, null, null, []));
5759
$subresourceMetadata = new SubresourceMetadata('relatedDummy', false);
5860
$propertyMetadataFactoryProphecy->create('dummy', 'relatedDummy')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'dummy', true, null, new Type(Type::BUILTIN_TYPE_OBJECT, false, 'relatedDummy')), 'This is a name.', true, true, true, true, false, false, null, null, [], $subresourceMetadata));
5961

@@ -94,7 +96,9 @@ public function testNormalize()
9496
$resourceClassResolverProphecy->reveal(),
9597
$operationMethodResolverProphecy->reveal(),
9698
$urlGenerator->reveal(),
97-
$subresourceOperationFactoryProphecy->reveal());
99+
$subresourceOperationFactoryProphecy->reveal(),
100+
new CustomConverter()
101+
);
98102

99103
$expected = [
100104
'@context' => [
@@ -168,6 +172,21 @@ public function testNormalize()
168172
'hydra:writable' => true,
169173
'hydra:description' => 'description',
170174
],
175+
[
176+
'@type' => 'hydra:SupportedProperty',
177+
'hydra:property' => [
178+
'@id' => '#dummy/name_converted',
179+
'@type' => 'rdf:Property',
180+
'rdfs:label' => 'name_converted',
181+
'domain' => '#dummy',
182+
'range' => 'xmls:string',
183+
],
184+
'hydra:title' => 'name_converted',
185+
'hydra:required' => false,
186+
'hydra:readable' => true,
187+
'hydra:writable' => true,
188+
'hydra:description' => 'name converted',
189+
],
171190
[
172191
'@type' => 'hydra:SupportedProperty',
173192
'hydra:property' => [

0 commit comments

Comments
 (0)