diff --git a/src/State/CreateProvider.php b/src/State/CreateProvider.php index 0b639205c91..ec4394a19a4 100644 --- a/src/State/CreateProvider.php +++ b/src/State/CreateProvider.php @@ -58,7 +58,8 @@ public function provide(Operation $operation, array $uriVariables = [], array $c } $relation = $this->decorated->provide(new Get(uriVariables: $relationUriVariables, class: $relationClass), $uriVariables); - $resource = new ($operation->getClass()); + $refl = new \ReflectionClass($operation->getClass()); + $resource = $refl->newInstanceWithoutConstructor(); $this->propertyAccessor->setValue($resource, $key, $relation); return $resource; diff --git a/src/State/ObjectProvider.php b/src/State/ObjectProvider.php new file mode 100644 index 00000000000..18b9e8558be --- /dev/null +++ b/src/State/ObjectProvider.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\State; + +use ApiPlatform\Metadata\Operation; + +/** + * An ItemProvider that just create a new object. + * + * @author Antoine Bluchet + * + * @experimental + */ +final class ObjectProvider implements ProviderInterface +{ + public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object + { + $refl = new \ReflectionClass($operation->getClass()); + + return $refl->newInstanceWithoutConstructor(); + } +} diff --git a/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php b/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php index ccf4ff912e8..fcc023abb68 100644 --- a/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php +++ b/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php @@ -128,6 +128,10 @@ public function load(array $configs, ContainerBuilder $container): void ->addTag('api_platform.state_provider'); $container->registerForAutoconfiguration(ProcessorInterface::class) ->addTag('api_platform.state_processor'); + + if (!$container->has('api_platform.state.item_provider')) { + $container->setAlias('api_platform.state.item_provider', 'api_platform.state_provider.object'); + } } private function registerCommonConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader, array $formats, array $patchFormats, array $errorFormats): void diff --git a/src/Symfony/Bundle/Resources/config/state.xml b/src/Symfony/Bundle/Resources/config/state.xml index ec59d47706a..f0558f89ef0 100644 --- a/src/Symfony/Bundle/Resources/config/state.xml +++ b/src/Symfony/Bundle/Resources/config/state.xml @@ -41,12 +41,18 @@ - + - + + - + + + + + +