Skip to content
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

ObjectTransformer is not available when custom MapperConfigurationInterface is processed #745

Closed
weaverryan opened this issue Aug 10, 2023 · 2 comments
Labels

Comments

@weaverryan
Copy link

Jane version(s) affected: 7.5.3

Description
Hi!

In the AutoMapper bundle, if you create a custom MapperConfigurationInterface and call $metadata->getPropertiesMapping(), the mappings will be generated BEFORE the ObjectTransformer was loaded into the system. The problem comes with how the objects are instantiated:

A) When the container creates theJane\Component\AutoMapper\Transformer\ChainTransformerFactory service, in the compiled container, it eventually calls:

$instance->addTransformerFactory(new \Jane\Component\AutoMapper\Transformer\ObjectTransformerFactory(($container->services['Jane\\Component\\AutoMapper\\AutoMapperInterface'] ?? $container->load('getAutoMapperInterfaceService'))));

In order to create the ObjectTransformerFactory, it needs to load the AutoMapperInterface service so it can be passed to it.

B) But, when the AutoMapperInterface is being created, in the cached container, it will call:

$instance->addMapperConfiguration(new \App\Automapper\UserToUserApiMapperConfiguration());

(where UserToUserApiMapperConfiguration is the custom mapper configuration class).

C) The AutoMapper::addMapperConfiguration() method - https://github.com/janephp/janephp/blob/next/src/Bundle/AutoMapperBundle/AutoMapper.php#L13 - causes the process() method to be called on the custom mapper configuration. However, at this point, the ObjectTransformerFactory hasn't yet finished being added to the system.

This is a minor issue - it can just cause WTF moments. I couldn't figure out why a property was NOT apparently being mapped... but then WOULD be mapped in the end.

How to reproduce

symfony new reproducing_jane_missing_object_transformer
cd reproducing_jane_missing_object_transformer
composer require jane-php/automapper-bundle

Create a mapping config class - throw an exception:

<?php

namespace App;

use Jane\Bundle\AutoMapperBundle\Configuration\MapperConfigurationInterface;
use Jane\Component\AutoMapper\MapperGeneratorMetadataInterface;

class TestingMapperConfiguration implements MapperConfigurationInterface
{
    public function process(MapperGeneratorMetadataInterface $metadata): void
    {
        throw new \Exception();
    }

    public function getSource(): string
    {
        return 'foo';
    }

    public function getTarget(): string
    {
        return 'bar';
    }
}

Then, use the AutoMapper somewhere so that it's not removed from the container:

<?php

namespace App\Controller;

use Jane\Component\AutoMapper\AutoMapperInterface;

class AnyController
{
    public function __construct(AutoMapperInterface $autoMapper)
    {        
    }
}

Finally:

php bin/console cache:clear -vvv

If you look at the stacktrace, you'll see something like:

ContainerTWxKxjp\App_KernelDevDebugContainer->load() at /Users/weaverryan/Sites/os/reproducers/reproducing_jane_missing_object_transformer/var/cache/de_/ContainerTWxKxjp/getChainTransformerFactoryService.php:42

If you look at that line, you'll see it starting the instantiation of the AutoMapperInterface service before the ObjectTransformerFactory is complete, which proves that the custom mapper config class is called before the ObjectTransformerFactory is present.

Possible Solution
One possibility is to inject the AutoMapperInterface dependency into ObjectTransformerFactory lazily in some way so that the chain transformer factory can finish instantiating without triggering the instantiation of the auto mapper.

Cheers!

@weaverryan weaverryan added the bug label Aug 10, 2023
@Korbeil
Copy link
Member

Korbeil commented Sep 22, 2023

Thanks a lot for this detailed issue, sorry I didn't had time to dig into this. I also changed the repository for the AutoMapper component so I'll switch this issue to the new repository.

@Korbeil
Copy link
Member

Korbeil commented Sep 22, 2023

Switched to: jolicode/automapper#4

@Korbeil Korbeil closed this as completed Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants