ecotoneframework / ecotone-dev

Ecotone Framework Development - This is Monorepo which contains all official public modules
https://docs.ecotone.tech
Other
37 stars 16 forks source link

Attribute used in an CPP property is disallowed in attribute resolving callstack of command handlers #336

Closed tandev closed 3 months ago

tandev commented 3 months ago

Ecotone version(s) affected: 1.224.0

Description

The following issue occurs on warmup, when using an Attribute which should be allowed in this place in CPP

Uncaught Error: Attribute "Symfony\Component\DependencyInjection\Attribute\AutowireLocator" cannot target property (allowed targets: parameter)

This attribute(Symfony\Component\DependencyInjection\Attribute\AutowireLocator) for example is allowed by symfony to be used for properties in a CPP definition

 public function __construct(
        #[AutowireLocator(FooLocatedService::class)]
        private ContainerInterface $foos,
    ) {
    }

So i think this should be allowed by ecotone too

How to reproduce
I set an example symfony application with ecotone integration where this can reproduced at cache warmup https://github.com/tandev/ecotone-attribute-resolving/tree/main

dgafka commented 3 months ago

Hey @tandev,

This bug comes from how attribute is defined: image

Where in your usage, you're using promoted properties, therefore to allow for such usage, it should be done like this to be correct: image

Ecotone does do nothing magically here, it just initializes this Attribute, where it's not possible due to incorrect definition.
I am not sure, how does it work on the Symfony side thought. Maybe they do not initialize it at all?

jlabedo commented 3 months ago

@dgafka , it is because of a promoted property (a parameter that is also a property). I am working on a fix as I think this is valid.

jlabedo commented 3 months ago

I remember I had this error int he past, and fixed it with a try catch around attribute instanciation in ecotone, but I can't find it anymore. I will see if there is a more elegant way of doing that.

dgafka commented 3 months ago

@jlabedo ah I see. Maybe we intitialize it in context of property (ClassDefinition)? If so, we could check if this Attribute has TARGET_PROPERTY option, and only then add initialize and add it?

jlabedo commented 3 months ago

I tried if ($attribute->getTarget() & \Attribute::TARGET_PROPERTY) but the target is always property (even if not an allowed target) Maybe we could use reflection on the attribute itself to get its class "\Attribute" attribute... and then get its targets. But seems a bit complicated in comparison with a documented try catch...

dgafka commented 3 months ago

Hmm, as long as we can catch this specific error, that should also do a trick, and will be less time consuming process than check of each attribute