Skip to content

Fix api-platform/admin#93: use a name converter if defined in the Hydra documentation #1919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
1 change: 1 addition & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/hydra.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<argument type="service" id="api_platform.operation_method_resolver" />
<argument type="service" id="api_platform.router" />
<argument type="service" id="api_platform.subresource_operation_factory" />
<argument type="service" id="api_platform.name_converter" on-invalid="ignore" />

<tag name="serializer.normalizer" priority="32" />
</service>
Expand Down
9 changes: 8 additions & 1 deletion src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

Expand All @@ -46,8 +47,9 @@ final class DocumentationNormalizer implements NormalizerInterface
private $operationMethodResolver;
private $urlGenerator;
private $subresourceOperationFactory;
private $nameConverter;

public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, UrlGeneratorInterface $urlGenerator, SubresourceOperationFactoryInterface $subresourceOperationFactory = null)
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, UrlGeneratorInterface $urlGenerator, SubresourceOperationFactoryInterface $subresourceOperationFactory = null, NameConverterInterface $nameConverter = null)
{
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
Expand All @@ -56,6 +58,7 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
$this->operationMethodResolver = $operationMethodResolver;
$this->urlGenerator = $urlGenerator;
$this->subresourceOperationFactory = $subresourceOperationFactory;
$this->nameConverter = $nameConverter;
}

/**
Expand Down Expand Up @@ -194,6 +197,10 @@ private function getHydraProperties(string $resourceClass, ResourceMetadata $res
continue;
}

if ($this->nameConverter) {
$propertyName = $this->nameConverter->normalize($propertyName);
}

$properties[] = $this->getProperty($propertyMetadata, $propertyName, $prefixedShortName, $shortName);
}

Expand Down
23 changes: 21 additions & 2 deletions tests/Hydra/Serializer/DocumentationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Serializer\NameConverter\CustomConverter;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\PropertyInfo\Type;
Expand All @@ -44,7 +45,7 @@ public function testNormalize()
$documentation = new Documentation(new ResourceNameCollection(['dummy' => 'dummy']), $title, $desc, $version, []);

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

$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']], []);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
Expand All @@ -54,6 +55,7 @@ public function testNormalize()
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create('dummy', 'name')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'name', true, true, true, true, false, false, null, null, []));
$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']]));
$propertyMetadataFactoryProphecy->create('dummy', 'nameConverted')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'name converted', true, true, true, true, false, false, null, null, []));
$subresourceMetadata = new SubresourceMetadata('relatedDummy', false);
$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));

Expand Down Expand Up @@ -94,7 +96,9 @@ public function testNormalize()
$resourceClassResolverProphecy->reveal(),
$operationMethodResolverProphecy->reveal(),
$urlGenerator->reveal(),
$subresourceOperationFactoryProphecy->reveal());
$subresourceOperationFactoryProphecy->reveal(),
new CustomConverter()
);

$expected = [
'@context' => [
Expand Down Expand Up @@ -168,6 +172,21 @@ public function testNormalize()
'hydra:writable' => true,
'hydra:description' => 'description',
],
[
'@type' => 'hydra:SupportedProperty',
'hydra:property' => [
'@id' => '#dummy/name_converted',
'@type' => 'rdf:Property',
'rdfs:label' => 'name_converted',
'domain' => '#dummy',
'range' => 'xmls:string',
],
'hydra:title' => 'name_converted',
'hydra:required' => false,
'hydra:readable' => true,
'hydra:writable' => true,
'hydra:description' => 'name converted',
],
[
'@type' => 'hydra:SupportedProperty',
'hydra:property' => [
Expand Down