Skip to content

Makes url generation strategy default value configurable #751

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/Api/IriConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface
* Gets the IRI associated with the given resource collection.
*
* @param string $resourceClass
* @param string $referenceType
* @param int $referenceType
*
* @throws InvalidArgumentException
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private function handleConfig(ContainerBuilder $container, array $config, array
$container->setParameter('api_platform.title', $config['title']);
$container->setParameter('api_platform.description', $config['description']);
$container->setParameter('api_platform.version', $config['version']);
$container->setParameter('api_platform.default_reference_type', $config['default_reference_type']);
$container->setParameter('api_platform.formats', $formats);
$container->setParameter('api_platform.error_formats', $errorFormats);
$container->setParameter('api_platform.collection.order', $config['collection']['order']);
Expand Down
23 changes: 23 additions & 0 deletions src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection;

use ApiPlatform\Core\Api\UrlGeneratorInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand All @@ -37,6 +38,28 @@ public function getConfigTreeBuilder()
->scalarNode('version')->defaultValue('0.0.0')->info('The version of the API.')->end()
->scalarNode('default_operation_path_resolver')->defaultValue('api_platform.operation_path_resolver.underscore')->info('Specify the default operation path resolver to use for generating resources operations path.')->end()
->scalarNode('name_converter')->defaultNull()->info('Specify a name converter to use.')->end()
->scalarNode('default_reference_type')
->beforeNormalization()
->ifString()
->then(function ($config) {
switch ($config) {
case 'absolute_url':
$defaultReferenceType = UrlGeneratorInterface::ABS_URL;
break;
case 'relative_path':
$defaultReferenceType = UrlGeneratorInterface::REL_PATH;
break;
case 'network_path':
$defaultReferenceType = UrlGeneratorInterface::NET_PATH;
break;
default:
$defaultReferenceType = UrlGeneratorInterface::ABS_PATH;
}

return $defaultReferenceType;
})
->end()
->end()
->booleanNode('enable_fos_user')->defaultValue(false)->info('Enable the FOSUserBundle integration.')->end()
->booleanNode('enable_nelmio_api_doc')->defaultValue(false)->info('Enable the Nelmio Api doc integration.')->end()
->booleanNode('enable_swagger')->defaultValue(true)->info('Enable the Swagger documentation and export.')->end()
Expand Down
1 change: 1 addition & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/hal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="api_platform.iri_converter" />
<argument type="service" id="api_platform.router" />
<argument>%api_platform.default_reference_type%</argument>

<tag name="serializer.normalizer" priority="32" />
</service>
Expand Down
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 @@ -37,6 +37,7 @@
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="api_platform.iri_converter" />
<argument type="service" id="api_platform.router" />
<argument>%api_platform.default_reference_type%</argument>

<tag name="serializer.normalizer" priority="32" />
</service>
Expand Down
8 changes: 5 additions & 3 deletions src/Hal/Serializer/EntrypointNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ final class EntrypointNormalizer implements NormalizerInterface
private $resourceMetadataFactory;
private $iriConverter;
private $urlGenerator;
private $defaultReferenceType;

public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, IriConverterInterface $iriConverter, UrlGeneratorInterface $urlGenerator)
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, IriConverterInterface $iriConverter, UrlGeneratorInterface $urlGenerator, int $defaultReferenceType = UrlGeneratorInterface::ABS_PATH)
{
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->iriConverter = $iriConverter;
$this->urlGenerator = $urlGenerator;
$this->defaultReferenceType = $defaultReferenceType;
}

/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = [])
{
$entrypoint = ['_links' => ['self' => ['href' => $this->urlGenerator->generate('api_entrypoint')]]];
$entrypoint = ['_links' => ['self' => ['href' => $this->urlGenerator->generate('api_entrypoint', [], $this->defaultReferenceType)]]];

foreach ($object->getResourceNameCollection() as $resourceClass) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
Expand All @@ -52,7 +54,7 @@ public function normalize($object, $format = null, array $context = [])
continue;
}
try {
$entrypoint['_links'][lcfirst($resourceMetadata->getShortName())]['href'] = $this->iriConverter->getIriFromResourceClass($resourceClass);
$entrypoint['_links'][lcfirst($resourceMetadata->getShortName())]['href'] = $this->iriConverter->getIriFromResourceClass($resourceClass, $this->defaultReferenceType);
} catch (InvalidArgumentException $ex) {
// Ignore resources without GET operations
}
Expand Down
7 changes: 4 additions & 3 deletions src/Hal/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\Api\UrlGeneratorInterface;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
Expand All @@ -34,9 +35,9 @@ final class ItemNormalizer extends AbstractItemNormalizer

private $componentsCache = [];

public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null)
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, int $defaultReferenceType = UrlGeneratorInterface::ABS_PATH)
{
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter);
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $defaultReferenceType);
}

/**
Expand All @@ -59,7 +60,7 @@ public function normalize($object, $format = null, array $context = [])
return $rawData;
}

$data = ['_links' => ['self' => ['href' => $this->iriConverter->getIriFromItem($object)]]];
$data = ['_links' => ['self' => ['href' => $this->iriConverter->getIriFromItem($object, $this->defaultReferenceType)]]];
$components = $this->getComponents($object, $format, $context);
$data = $this->populateRelation($data, $object, $format, $context, $components, 'links');
$data = $this->populateRelation($data, $object, $format, $context, $components, 'embedded');
Expand Down
3 changes: 2 additions & 1 deletion src/Hydra/Serializer/CollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\Api\UrlGeneratorInterface;
use ApiPlatform\Core\DataProvider\PaginatorInterface;
use ApiPlatform\Core\JsonLd\ContextBuilderInterface;
use ApiPlatform\Core\JsonLd\Serializer\JsonLdContextTrait;
Expand Down Expand Up @@ -72,7 +73,7 @@ public function normalize($object, $format = null, array $context = [])
$data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
$context = $this->initContext($resourceClass, $context);

$data['@id'] = $this->iriConverter->getIriFromResourceClass($resourceClass);
$data['@id'] = $this->iriConverter->getIriFromResourceClass($resourceClass, UrlGeneratorInterface::ABS_URL);
$data['@type'] = 'hydra:Collection';

$data['hydra:member'] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Hydra/Serializer/ConstraintViolationListNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function normalize($object, $format = null, array $context = [])
}

return [
'@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'ConstraintViolationList']),
'@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'ConstraintViolationList']), //todo config url
'@type' => 'ConstraintViolationList',
'hydra:title' => $context['title'] ?? 'An error occurred',
'hydra:description' => $messages ? implode("\n", $messages) : (string) $object,
Expand Down
6 changes: 3 additions & 3 deletions src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ private function getProperty(PropertyMetadata $propertyMetadata, string $propert
*/
private function computeDoc(Documentation $object, array $classes) : array
{
$doc = ['@context' => $this->getContext(), '@id' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT])];
$doc = ['@context' => $this->getContext(), '@id' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT])]; //todo config url

if ('' !== $object->getTitle()) {
$doc['hydra:title'] = $object->getTitle();
Expand All @@ -463,7 +463,7 @@ private function computeDoc(Documentation $object, array $classes) : array
$doc['hydra:description'] = $object->getDescription();
}

$doc['hydra:entrypoint'] = $this->urlGenerator->generate('api_entrypoint');
$doc['hydra:entrypoint'] = $this->urlGenerator->generate('api_entrypoint'); //todo config url
$doc['hydra:supportedClass'] = $classes;

return $doc;
Expand All @@ -477,7 +477,7 @@ private function computeDoc(Documentation $object, array $classes) : array
private function getContext() : array
{
return [
'@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
'@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#', //todo config url
'hydra' => ContextBuilderInterface::HYDRA_NS,
'rdf' => ContextBuilderInterface::RDF_NS,
'rdfs' => ContextBuilderInterface::RDFS_NS,
Expand Down
10 changes: 6 additions & 4 deletions src/Hydra/Serializer/EntrypointNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ final class EntrypointNormalizer implements NormalizerInterface
private $resourceMetadataFactory;
private $iriConverter;
private $urlGenerator;
private $defaultReferenceType;

public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, IriConverterInterface $iriConverter, UrlGeneratorInterface $urlGenerator)
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, IriConverterInterface $iriConverter, UrlGeneratorInterface $urlGenerator, int $defaultReferenceType = UrlGeneratorInterface::ABS_PATH)
{
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->iriConverter = $iriConverter;
$this->urlGenerator = $urlGenerator;
$this->defaultReferenceType = $defaultReferenceType;
}

/**
Expand All @@ -44,8 +46,8 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
public function normalize($object, $format = null, array $context = [])
{
$entrypoint = [
'@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Entrypoint']),
'@id' => $this->urlGenerator->generate('api_entrypoint'),
'@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Entrypoint'], $this->defaultReferenceType),
'@id' => $this->urlGenerator->generate('api_entrypoint', [], $this->defaultReferenceType),
'@type' => 'Entrypoint',
];

Expand All @@ -56,7 +58,7 @@ public function normalize($object, $format = null, array $context = [])
continue;
}
try {
$entrypoint[lcfirst($resourceMetadata->getShortName())] = $this->iriConverter->getIriFromResourceClass($resourceClass);
$entrypoint[lcfirst($resourceMetadata->getShortName())] = $this->iriConverter->getIriFromResourceClass($resourceClass, $this->defaultReferenceType);
} catch (InvalidArgumentException $ex) {
// Ignore resources without GET operations
}
Expand Down
2 changes: 1 addition & 1 deletion src/Hydra/Serializer/ErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function normalize($object, $format = null, array $context = [])
}

$data = [
'@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Error']),
'@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Error']), //todo config url
'@type' => 'Error',
'hydra:title' => $context['title'] ?? 'An error occurred',
'hydra:description' => $message ?? (string) $object,
Expand Down
2 changes: 1 addition & 1 deletion src/JsonLd/Action/ContextAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __invoke(string $shortName) : array
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);

if ($shortName === $resourceMetadata->getShortName()) {
return ['@context' => $this->contextBuilder->getResourceContext($resourceClass)];
return ['@context' => $this->contextBuilder->getResourceContext($resourceClass)]; //todo config url
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/JsonLd/ContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(ResourceNameCollectionFactoryInterface $resourceName
public function getBaseContext(int $referenceType = UrlGeneratorInterface::ABS_URL) : array
{
return [
'@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
'@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#', //todo config url
'hydra' => self::HYDRA_NS,
];
}
Expand Down
7 changes: 4 additions & 3 deletions src/JsonLd/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\Api\UrlGeneratorInterface;
use ApiPlatform\Core\JsonLd\ContextBuilderInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
Expand All @@ -37,9 +38,9 @@ final class ItemNormalizer extends AbstractItemNormalizer
private $resourceMetadataFactory;
private $contextBuilder;

public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, ContextBuilderInterface $contextBuilder, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null)
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, ContextBuilderInterface $contextBuilder, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, int $defaultReferenceType = UrlGeneratorInterface::ABS_PATH)
{
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter);
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $defaultReferenceType);

$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->contextBuilder = $contextBuilder;
Expand Down Expand Up @@ -67,7 +68,7 @@ public function normalize($object, $format = null, array $context = [])
return $rawData;
}

$data['@id'] = $this->iriConverter->getIriFromItem($object);
$data['@id'] = $this->iriConverter->getIriFromItem($object, $this->defaultReferenceType);
$data['@type'] = ($iri = $resourceMetadata->getIri()) ? $iri : $resourceMetadata->getShortName();

return array_merge($data, $rawData);
Expand Down
4 changes: 2 additions & 2 deletions src/JsonLd/Serializer/JsonLdContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ private function addJsonLdContext(ContextBuilderInterface $contextBuilder, strin
$context['jsonld_has_context'] = true;

if (isset($context['jsonld_embed_context'])) {
$data['@context'] = $contextBuilder->getResourceContext($resourceClass);
$data['@context'] = $contextBuilder->getResourceContext($resourceClass); //todo config url

return $data;
}

$data['@context'] = $contextBuilder->getResourceContextUri($resourceClass);
$data['@context'] = $contextBuilder->getResourceContextUri($resourceClass); //todo config url

return $data;
}
Expand Down
Loading