From 5d7fb4d8ab05113461d577751d6ae08487aea726 Mon Sep 17 00:00:00 2001 From: Giorgio Premi Date: Tue, 31 Mar 2026 14:40:35 +0200 Subject: [PATCH] feat(symfony): composer dependency JSON+LD/Hydra is now optional --- .../ApiPlatformExtension.php | 8 ++++++ .../DependencyInjection/Configuration.php | 27 ++++++++++++++----- src/Symfony/composer.json | 2 -- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php b/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php index 01c696c4e7..1c0f4da539 100644 --- a/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php +++ b/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php @@ -711,6 +711,14 @@ private function registerJsonLdHydraConfiguration(ContainerBuilder $container, a return; } + if (!InstalledVersions::isInstalled('api-platform/jsonld')) { + throw new \LogicException('JSON+LD support cannot be enabled as the JSON+LD component is not installed. Try running "composer require api-platform/jsonld".'); + } + + if (!InstalledVersions::isInstalled('api-platform/hydra')) { + throw new \LogicException('JSON+LD support cannot be enabled as the Hydra component is not installed. Try running "composer require api-platform/hydra".'); + } + if ($config['use_symfony_listeners']) { $loader->load('symfony/jsonld.php'); } else { diff --git a/src/Symfony/Bundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/DependencyInjection/Configuration.php index 911de422fd..41d58dca20 100644 --- a/src/Symfony/Bundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/DependencyInjection/Configuration.php @@ -20,6 +20,7 @@ use ApiPlatform\Metadata\Post; use ApiPlatform\Metadata\Put; use ApiPlatform\Symfony\Controller\MainController; +use Composer\InstalledVersions; use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle; use Doctrine\ORM\EntityManagerInterface; @@ -57,6 +58,8 @@ public function getConfigTreeBuilder(): TreeBuilder $treeBuilder = new TreeBuilder('api_platform'); $rootNode = $treeBuilder->getRootNode(); + $jsonLdInstalled = InstalledVersions::isInstalled('api-platform/jsonld') && InstalledVersions::isInstalled('api-platform/hydra'); + $rootNode ->beforeNormalization() ->ifTrue(static function ($v) { @@ -191,15 +194,17 @@ public function getConfigTreeBuilder(): TreeBuilder $this->addExceptionToStatusSection($rootNode); - $this->addFormatSection($rootNode, 'formats', [ - 'jsonld' => ['mime_types' => ['application/ld+json']], + $this->addFormatSection($rootNode, 'formats', $jsonLdInstalled ? [ + 'jsonld' => ['mime_types' => ['application/ld+json']] + ] : [ + 'json' => ['mime_types' => ['application/json']] ]); $this->addFormatSection($rootNode, 'patch_formats', [ 'json' => ['mime_types' => ['application/merge-patch+json']], ]); - $defaultDocFormats = [ - 'jsonld' => ['mime_types' => ['application/ld+json']], + $defaultDocFormats = $jsonLdInstalled ? ['jsonld' => ['mime_types' => ['application/ld+json']]] : []; + $defaultDocFormats += [ 'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']], 'html' => ['mime_types' => ['text/html']], ]; @@ -210,11 +215,19 @@ public function getConfigTreeBuilder(): TreeBuilder $this->addFormatSection($rootNode, 'docs_formats', $defaultDocFormats); - $this->addFormatSection($rootNode, 'error_formats', [ - 'jsonld' => ['mime_types' => ['application/ld+json']], + $defaultDocFormats = $jsonLdInstalled ? ['jsonld' => ['mime_types' => ['application/ld+json']]] : []; + $defaultDocFormats += [ + 'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']], + 'html' => ['mime_types' => ['text/html']], + ]; + + $defaultErrorFormats = $jsonLdInstalled ? ['jsonld' => ['mime_types' => ['application/ld+json']]] : []; + $defaultErrorFormats += [ 'jsonproblem' => ['mime_types' => ['application/problem+json']], 'json' => ['mime_types' => ['application/problem+json', 'application/json']], - ]); + ]; + $this->addFormatSection($rootNode, 'error_formats', $defaultErrorFormats); + $rootNode ->children() ->arrayNode('jsonschema_formats') diff --git a/src/Symfony/composer.json b/src/Symfony/composer.json index a0cd861b8a..00739b6dfd 100644 --- a/src/Symfony/composer.json +++ b/src/Symfony/composer.json @@ -32,8 +32,6 @@ "api-platform/documentation": "^4.3", "api-platform/http-cache": "^4.3", "api-platform/json-schema": "^4.3", - "api-platform/jsonld": "^4.3", - "api-platform/hydra": "^4.3", "api-platform/metadata": "^4.3", "api-platform/serializer": "^4.3", "api-platform/state": "^4.3",