diff --git a/composer.json b/composer.json index 4480f7ece51..bb3ef3071d3 100644 --- a/composer.json +++ b/composer.json @@ -163,6 +163,7 @@ "symfony/cache": "^6.4 || ^7.0", "symfony/config": "^6.4 || ^7.0", "symfony/console": "^6.4 || ^7.0", + "symfony/object-mapper": "v7.3.0-RC1", "symfony/css-selector": "^6.4 || ^7.0", "symfony/dependency-injection": "^6.4 || ^7.0", "symfony/doctrine-bridge": "^6.4.2 || ^7.1", @@ -172,7 +173,7 @@ "symfony/expression-language": "^6.4 || ^7.0", "symfony/finder": "^6.4 || ^7.0", "symfony/form": "^6.4 || ^7.0", - "symfony/framework-bundle": "^6.4 || ^7.0", + "symfony/framework-bundle": "v7.3.0-RC1", "symfony/http-client": "^6.4 || ^7.0", "symfony/intl": "^6.4 || ^7.0", "symfony/maker-bundle": "^1.24", @@ -212,5 +213,6 @@ "type": "library", "repositories": [ {"type": "vcs", "url": "https://github.com/soyuka/phpunit"} - ] + ], + "minimum-stability": "RC" } diff --git a/src/Laravel/ApiPlatformDeferredProvider.php b/src/Laravel/ApiPlatformDeferredProvider.php index 18e549f06f5..f9f75ee877c 100644 --- a/src/Laravel/ApiPlatformDeferredProvider.php +++ b/src/Laravel/ApiPlatformDeferredProvider.php @@ -80,7 +80,10 @@ use ApiPlatform\State\Pagination\Pagination; use ApiPlatform\State\ParameterProviderInterface; use ApiPlatform\State\ProcessorInterface; +use ApiPlatform\State\Processor\ObjectMapperProcessor; +use ApiPlatform\State\Provider\ObjectMapperProvider; use ApiPlatform\State\Provider\ParameterProvider; +use ApiPlatform\State\Provider\ReadProvider; use ApiPlatform\State\Provider\SecurityParameterProvider; use ApiPlatform\State\ProviderInterface; use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerInterface; @@ -89,6 +92,12 @@ use Illuminate\Support\ServiceProvider; use Negotiation\Negotiator; use Psr\Log\LoggerInterface; +use Symfony\Component\ObjectMapper\ConditionCallableInterface; +use Symfony\Component\ObjectMapper\Metadata\ReflectionObjectMapperMetadataFactory; +use Symfony\Component\ObjectMapper\ObjectMapper; +use Symfony\Component\ObjectMapper\ObjectMapperInterface; +use Symfony\Component\ObjectMapper\TransformCallableInterface; +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; use Symfony\Component\Serializer\NameConverter\NameConverterInterface; @@ -190,6 +199,34 @@ public function register(): void $this->autoconfigure($classes, ProviderInterface::class, [ItemProvider::class, CollectionProvider::class, ErrorProvider::class]); + $this->app->singleton(ObjectMapperInterface::class, function (Application $app) { + return new ObjectMapper( + new ReflectionObjectMapperMetadataFactory(), + $app->make(PropertyAccessorInterface::class), + new ServiceLocator(iterator_to_array($app->tagged(TransformCallableInterface::class))), + new ServiceLocator(iterator_to_array($app->tagged(ConditionCallableInterface::class))), + ); + }); + + $this->autoconfigure($classes, TransformCallableInterface::class, []); + $this->autoconfigure($classes, ConditionCallableInterface::class, []); + + $this->app->extend(ReadProvider::class, function (ReadProvider $service, Application $app) { + return new ObjectMapperProvider( + $app->make(ObjectMapperInterface::class), + $service + ); + }); + + $this->app->extend(CallableProcessor::class, function (CallableProcessor $service, Application $app) { + return new ObjectMapperProcessor( + $app->make(ObjectMapperInterface::class), + $service + ); + }); + + $this->autoconfigure($classes, ProviderInterface::class, [ItemProvider::class, CollectionProvider::class, ErrorProvider::class]); + $this->app->singleton(ResourceMetadataCollectionFactoryInterface::class, function (Application $app) { /** @var ConfigRepository $config */ $config = $app['config']; @@ -355,6 +392,7 @@ public function provides(): array 'api_platform.graphql.state_provider.parameter', FieldsBuilderEnumInterface::class, ExceptionHandlerInterface::class, + ObjectMapperInterface::class ]; } } diff --git a/src/Serializer/SerializerContextBuilder.php b/src/Serializer/SerializerContextBuilder.php index ecc58b333b8..6adf27fa075 100644 --- a/src/Serializer/SerializerContextBuilder.php +++ b/src/Serializer/SerializerContextBuilder.php @@ -84,10 +84,6 @@ public function createFromRequest(Request $request, bool $normalization, ?array } } - if (null === $context['output'] && $this->getStateOptionsClass($operation)) { - $context['force_resource_class'] = $operation->getClass(); - } - if ($this->debug && isset($context['groups']) && $operation instanceof ErrorOperation) { if (!\is_array($context['groups'])) { $context['groups'] = (array) $context['groups']; diff --git a/src/State/Processor/ObjectMapperProcessor.php b/src/State/Processor/ObjectMapperProcessor.php new file mode 100644 index 00000000000..7c3762d277f --- /dev/null +++ b/src/State/Processor/ObjectMapperProcessor.php @@ -0,0 +1,38 @@ + + * + * 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\Processor; + +use ApiPlatform\Metadata\Operation; +use ApiPlatform\State\ProcessorInterface; +use Doctrine\Persistence\ManagerRegistry; +use Symfony\Component\ObjectMapper\Attribute\Map; +use Symfony\Component\ObjectMapper\ObjectMapperInterface; + +final class ObjectMapperProcessor implements ProcessorInterface +{ + public function __construct( + private readonly ObjectMapperInterface $objectMapper, + private readonly ProcessorInterface $decorated, + ) { + } + + public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): object|array|null + { + if (!(new \ReflectionClass($operation->getClass()))->getAttributes(Map::class)) { + return $this->decorated->process($data, $operation, $uriVariables, $context); + } + + return $this->objectMapper->map($this->decorated->process($this->objectMapper->map($data, $context['data'] ?? null), $operation, $uriVariables, $context)); + } +} diff --git a/src/State/Provider/ObjectMapperProvider.php b/src/State/Provider/ObjectMapperProvider.php new file mode 100644 index 00000000000..51f51b8be20 --- /dev/null +++ b/src/State/Provider/ObjectMapperProvider.php @@ -0,0 +1,60 @@ + + * + * 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\Provider; + +use ApiPlatform\Doctrine\Odm\State\Options as OdmOptions; +use ApiPlatform\Doctrine\Orm\State\Options; +use ApiPlatform\Metadata\Operation; +use ApiPlatform\State\Pagination\ArrayPaginator; +use ApiPlatform\State\Pagination\PaginatorInterface; +use ApiPlatform\State\ProviderInterface; +use Symfony\Component\ObjectMapper\Attribute\Map; +use Symfony\Component\ObjectMapper\ObjectMapperInterface; + +final class ObjectMapperProvider implements ProviderInterface +{ + public function __construct( + private readonly ObjectMapperInterface $objectMapper, + private readonly ProviderInterface $decorated, + ) { + } + + public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null + { + $data = $this->decorated->provide($operation, $uriVariables, $context); + + if (!\is_object($data)) { + return $data; + } + + $entityClass = $operation->getClass(); + if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) { + $entityClass = $options->getEntityClass(); + } + + if (($options = $operation->getStateOptions()) && $options instanceof OdmOptions && $options->getDocumentClass()) { + $entityClass = $options->getDocumentClass(); + } + + if (!(new \ReflectionClass($entityClass))->getAttributes(Map::class)) { + return $data; + } + + if ($data instanceof PaginatorInterface) { + return new ArrayPaginator(array_map(fn ($v) => $this->objectMapper->map($v), iterator_to_array($data)), 0, \count($data)); + } + + return $this->objectMapper->map($data); + } +} diff --git a/src/State/composer.json b/src/State/composer.json index 2552c3826f1..ea7cda8823e 100644 --- a/src/State/composer.json +++ b/src/State/composer.json @@ -30,7 +30,8 @@ "php": ">=8.2", "api-platform/metadata": "^4.1.11", "psr/container": "^1.0 || ^2.0", - "symfony/http-kernel": "^6.4 || ^7.0" + "symfony/http-kernel": "^6.4 || ^7.0", + "symfony/object-mapper": "^7.3.0-RC1" }, "require-dev": { "phpunit/phpunit": "11.5.x-dev", diff --git a/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php b/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php index 9f7a1e5f019..8c35c2152d0 100644 --- a/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php +++ b/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php @@ -59,6 +59,7 @@ use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\Finder\Finder; use Symfony\Component\HttpClient\ScopingHttpClient; +use Symfony\Component\ObjectMapper\Attribute\Map; use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; use Symfony\Component\Uid\AbstractUid; use Symfony\Component\Validator\Validator\ValidatorInterface; @@ -169,6 +170,9 @@ public function load(array $configs, ContainerBuilder $container): void $this->registerArgumentResolverConfiguration($loader); $this->registerLinkSecurityConfiguration($loader, $config); + if (class_exists(Map::class)) { + $loader->load('state/object_mapper.xml'); + } $container->registerForAutoconfiguration(FilterInterface::class) ->addTag('api_platform.filter'); $container->registerForAutoconfiguration(ProviderInterface::class) diff --git a/src/Symfony/Bundle/Resources/config/state/object_mapper.xml b/src/Symfony/Bundle/Resources/config/state/object_mapper.xml new file mode 100644 index 00000000000..6b269b99345 --- /dev/null +++ b/src/Symfony/Bundle/Resources/config/state/object_mapper.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + diff --git a/src/Symfony/Controller/MainController.php b/src/Symfony/Controller/MainController.php index 8c691bf654d..8e5afa07b42 100644 --- a/src/Symfony/Controller/MainController.php +++ b/src/Symfony/Controller/MainController.php @@ -109,9 +109,6 @@ public function __invoke(Request $request): Response } } - $context['previous_data'] = $request->attributes->get('previous_data'); - $context['data'] = $request->attributes->get('data'); - if (null === $operation->canWrite()) { $operation = $operation->withWrite(!$request->isMethodSafe()); } @@ -120,6 +117,9 @@ public function __invoke(Request $request): Response $operation = $operation->withSerialize(true); } + $context['previous_data'] = $request->attributes->get('previous_data'); + $context['data'] = $request->attributes->get('data'); + return $this->processor->process($body, $operation, $uriVariables, $context); } } diff --git a/src/Symfony/composer.json b/src/Symfony/composer.json index 090750c80fe..3ac8a2a0894 100644 --- a/src/Symfony/composer.json +++ b/src/Symfony/composer.json @@ -54,6 +54,7 @@ "api-platform/parameter-validator": "^3.1", "phpspec/prophecy-phpunit": "^2.2", "phpunit/phpunit": "11.5.x-dev", + "symfony/object-mapper": "v7.3.0-RC1", "symfony/expression-language": "^6.4 || ^7.0", "symfony/intl": "^6.4 || ^7.0", "symfony/mercure-bundle": "*", diff --git a/tests/Fixtures/TestBundle/ApiResource/MappedResource.php b/tests/Fixtures/TestBundle/ApiResource/MappedResource.php new file mode 100644 index 00000000000..72a8a72fcbc --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/MappedResource.php @@ -0,0 +1,42 @@ + + * + * 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\Tests\Fixtures\TestBundle\ApiResource; + +use ApiPlatform\Doctrine\Orm\State\Options; +use ApiPlatform\JsonLd\ContextBuilder; +use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedEntity; +use Symfony\Component\ObjectMapper\Attribute\Map; + +#[ApiResource(stateOptions: new Options(entityClass: MappedEntity::class), normalizationContext: [ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX => false])] +#[Map(target: MappedEntity::class)] +final class MappedResource +{ + #[Map(if: false)] + public ?string $id = null; + + #[Map(target: 'firstName', transform: [self::class, 'toFirstName'])] + #[Map(target: 'lastName', transform: [self::class, 'toLastName'])] + public string $username; + + public static function toFirstName(string $v): string + { + return explode(' ', $v)[0] ?? null; + } + + public static function toLastName(string $v): string + { + return explode(' ', $v)[1] ?? null; + } +} diff --git a/tests/Fixtures/TestBundle/Entity/MappedEntity.php b/tests/Fixtures/TestBundle/Entity/MappedEntity.php new file mode 100644 index 00000000000..e58eda80279 --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/MappedEntity.php @@ -0,0 +1,69 @@ + + * + * 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\Tests\Fixtures\TestBundle\Entity; + +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResource; +use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\ObjectMapper\Attribute\Map; + +/** + * MappedEntity to MappedResource. + */ +#[ORM\Entity] +#[Map(target: MappedResource::class)] +class MappedEntity +{ + #[ORM\Column(type: 'integer')] + #[ORM\Id] + #[ORM\GeneratedValue(strategy: 'AUTO')] + private ?int $id = null; + + #[ORM\Column] + #[Map(if: false)] + private string $firstName; + + #[Map(target: 'username', transform: [self::class, 'toUsername'])] + #[ORM\Column] + private string $lastName; + + public static function toUsername($value, $object): string + { + return $object->getFirstName().' '.$object->getLastName(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function setLastName(string $name): void + { + $this->lastName = $name; + } + + public function getLastName(): string + { + return $this->lastName; + } + + public function setFirstName(string $name): void + { + $this->firstName = $name; + } + + public function getFirstName(): string + { + return $this->firstName; + } +} diff --git a/tests/Functional/MappingTest.php b/tests/Functional/MappingTest.php new file mode 100644 index 00000000000..2f1e94555cf --- /dev/null +++ b/tests/Functional/MappingTest.php @@ -0,0 +1,82 @@ + + * + * 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\Tests\Functional; + +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResource; +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedEntity; +use ApiPlatform\Tests\RecreateSchemaTrait; +use ApiPlatform\Tests\SetupClassResourcesTrait; + +final class MappingTest extends ApiTestCase +{ + use RecreateSchemaTrait; + use SetupClassResourcesTrait; + + protected static ?bool $alwaysBootKernel = false; + + /** + * @return class-string[] + */ + public static function getResources(): array + { + return [MappedResource::class]; + } + + public function testShouldMapBetweenResourceAndEntity(): void + { + $this->recreateSchema([MappedEntity::class]); + $this->loadFixtures(); + self::createClient()->request('GET', 'mapped_resources'); + $this->assertJsonContains(['member' => [ + ['username' => 'B0 A0'], + ['username' => 'B1 A1'], + ['username' => 'B2 A2'], + ]]); + + $r = self::createClient()->request('POST', 'mapped_resources', ['json' => ['username' => 'so yuka']]); + $this->assertJsonContains(['username' => 'so yuka']); + + $manager = $this->getManager(); + $repo = $manager->getRepository(MappedEntity::class); + $id = $r->toArray()['id']; + $persisted = $repo->findOneBy(['id' => $id]); + $this->assertSame('so', $persisted->getFirstName()); + $this->assertSame('yuka', $persisted->getLastName()); + + $uri = $r->toArray()['@id']; + self::createClient()->request('GET', $uri); + $this->assertJsonContains(['username' => 'so yuka']); + + $r = self::createClient()->request('PATCH', $uri, ['json' => ['username' => 'ba zar'], 'headers' => ['content-type' => 'application/merge-patch+json']]); + $this->assertJsonContains(['username' => 'ba zar', 'id' => $id]); + + self::createClient()->request('DELETE', $uri); + $this->assertNull($repo->findOneBy(['id' => $id])); + } + + private function loadFixtures(): void + { + $manager = $this->getManager(); + + for ($i = 0; $i < 10; ++$i) { + $e = new MappedEntity(); + $e->setLastName('A'.$i); + $e->setFirstName('B'.$i); + $manager->persist($e); + } + + $manager->flush(); + } +}