Skip to content
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
11 changes: 6 additions & 5 deletions src/State/Processor/ObjectMapperProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public function __construct(

public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if (!$this->objectMapper || !$operation->canWrite()) {
return $this->decorated->process($data, $operation, $uriVariables, $context);
}

if (!(new \ReflectionClass($operation->getClass()))->getAttributes(Map::class)) {
if (
!$this->objectMapper
|| !$operation->canWrite()
|| !is_a($data, $operation->getClass(), true)
|| !(new \ReflectionClass($operation->getClass()))->getAttributes(Map::class)
) {
return $this->decorated->process($data, $operation, $uriVariables, $context);
}

Expand Down
46 changes: 46 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/MappedResourceWithInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Operation;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Tests\Fixtures\TestBundle\Dto\MappedResouceInput;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[Post(
uriTemplate: '/mapped_resource_with_input',
input: MappedResouceInput::class,
stateOptions: new Options(entityClass: MappedEntity::class),
normalizationContext: [ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX => false],
processor: [self::class, 'process']
)]
#[Map(target: MappedEntity::class)]
final class MappedResourceWithInput
{
#[Map(if: false)]
public ?string $id = null;
public string $username;

public static function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
{
$s = new self();
$s->id = $data->id;
$s->username = $data->name;
Copy link
Contributor

@rvanlaak rvanlaak Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part (writing a value with a name different from it's input name) might only be possible since ObjectMapper's last patch release, and might break pre-7.3.4. Maybe cover this in the composer.json conflicts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I've an issue with this le'ts talk about this at #7504


return $s;
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/TestBundle/Dto/MappedResouceInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Dto;

class MappedResouceInput
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add a test where both these fields are required in the input's constructor, and with coverage on #[Assert\NotBlank] validation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what brings this test to the mapping functionality ?

{
public string $id;
public string $name;
}
33 changes: 32 additions & 1 deletion tests/Functional/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\FirstResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResourceOdm;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResourceWithInput;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResourceWithRelation;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResourceWithRelationRelated;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SecondResource;
Expand All @@ -40,7 +41,15 @@ final class MappingTest extends ApiTestCase
*/
public static function getResources(): array
{
return [MappedResource::class, MappedResourceOdm::class, FirstResource::class, SecondResource::class, MappedResourceWithRelation::class, MappedResourceWithRelationRelated::class];
return [
MappedResource::class,
MappedResourceOdm::class,
FirstResource::class,
SecondResource::class,
MappedResourceWithRelation::class,
MappedResourceWithRelationRelated::class,
MappedResourceWithInput::class,
];
}

public function testShouldMapBetweenResourceAndEntity(): void
Expand Down Expand Up @@ -136,6 +145,28 @@ public function testMapPutAllowCreate(): void
]);
}

public function testShouldNotMapWhenInput(): void
{
if (!$this->getContainer()->has('api_platform.object_mapper')) {
$this->markTestSkipped('ObjectMapper not installed');
}

if ($this->isMongoDB()) {
$this->markTestSkipped('MongoDB not tested');
}

$this->recreateSchema([MappedEntity::class]);
$this->loadFixtures();
$r = self::createClient()->request('POST', 'mapped_resource_with_input', [
'headers' => [
'content-type' => 'application/ld+json',
],
'json' => ['name' => 'test', 'id' => '1'],
]);

$this->assertJsonContains(['username' => 'test']);
}

private function loadFixtures(): void
{
$manager = $this->getManager();
Expand Down
Loading