Skip to content

feat(symfony): object mapper with state options #6801

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

Merged
merged 1 commit into from
Jun 29, 2025

Conversation

soyuka
Copy link
Member

@soyuka soyuka commented Nov 15, 2024

Q A
Branch? main
License MIT

Still a few things left to be done but this is a first approach. This makes API Platform compatible with the Object Mapper component: symfony/symfony#51741

Demo:

composer create-project symfony/skeleton test-objectmapper2
cd test-objectmapper2
composer req debug api-platform/symfony api-platform/doctrine-orm make orm symfony/asset 
nvim .env # uncomment sqlite
nvim composer.json # change extra.symfony to 7.3.*
composer config minimum-stability RC
composer req symfony/object-mapper:^7.3.0-RC1 symfony/type-info:v7.3.0-RC1 symfony/framework-bundle:v7.3.0-RC1 -W 
# clone my pr https://github.com/api-platform/core/pull/6801 to a parent directory:
# example with gh in a api-platform/core clone: `gh pr checkout 6801`
composer link ../core-6801 # if link is not available `composer global require soyuka/pmu`
bin/console make:entity # create an entity Book with id/title
bin/console d:s:u --force 
frankenphp php-server --listen :8080 -r public

Example:

<?php

namespace App\Entity;

use App\ApiResource\Book as AppBook;
use App\Repository\BookRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ORM\Entity(repositoryClass: BookRepository::class)]
#[Map(target: AppBook::class)]
class Book
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    #[Map(transform: 'ucfirst')] // example of transformation to the dto
    private ?string $title = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): static
    {
        $this->title = $title;

        return $this;
    }
}
<?php

namespace App\ApiResource;

use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Book as BookEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ApiResource(stateOptions: new Options(entityClass: BookEntity::class))]
#[Map(target: BookEntity::class)]
class Book
{
    #[Map(if: false)] // do not map the id from the dto to the entity
    public string $id;
    #[Map(transform: 'strtolower')] // example of transformation
    public string $title;
}

Copy link

stale bot commented Jan 15, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Copy link
Contributor

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

I think these provider and processor are Doctrine-specific. They should be moved to the Doctrine namespace and we can consider injecting the ObjectManager of ORM or ODM to retrieve the attached object.

Unless the new symfony/mapper component is able to use a factory to create the Entity class from the DTO. It would use the Id field of the DTO to retreive the attached entity from the entity manager.

@soyuka soyuka force-pushed the feat/automapper branch 2 times, most recently from ff46774 to 280ee15 Compare May 27, 2025 15:57
@soyuka
Copy link
Member Author

soyuka commented May 28, 2025

I'll fix the tests once the component is released

@soyuka soyuka marked this pull request as ready for review May 28, 2025 08:25
@soyuka soyuka force-pushed the feat/automapper branch 2 times, most recently from d0d8e5a to f05422c Compare June 27, 2025 12:23
@soyuka soyuka force-pushed the feat/automapper branch 9 times, most recently from 2b296b5 to dfb5691 Compare June 29, 2025 12:12
@soyuka soyuka force-pushed the feat/automapper branch from dfb5691 to 6491987 Compare June 29, 2025 12:56
@soyuka soyuka merged commit a42034d into api-platform:main Jun 29, 2025
109 of 114 checks passed
@soyuka soyuka deleted the feat/automapper branch June 29, 2025 13:09
return $this->decorated->process($data, $operation, $uriVariables, $context);
}

return $this->objectMapper->map($this->decorated->process($this->objectMapper->map($data), $operation, $uriVariables, $context));

Choose a reason for hiding this comment

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

Hi @soyuka , thanks for your work on this!

The map method is called without the $target parameter, even though we could extract the expected output class via:

$outputClass = $operation->getOutput()['class'] ?? null;

Would it make sense to pass this as the $target to improve mapping clarity ?

Same idea could apply to the mapping for ORM entity conversion — the target class could be extracted from stateOptions.

Copy link
Member Author

Choose a reason for hiding this comment

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

IMO the mapping should be defined with the Map attribute, as you could Map and have a different output.

Choose a reason for hiding this comment

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

To ensure the output matches the generated OpenAPI documentation, wouldn’t it make more sense to use the class specified in output, if it has been provided?
In cases where multiple target classes are possible, how should we determine which one should be used?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed I need to think about this but its best that your resource is your output. Could you please open a new issue with your use case ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants