Adobe-Consulting-Services / acs-aem-commons

http://adobe-consulting-services.github.io/acs-aem-commons/
Apache License 2.0
453 stars 600 forks source link

Wrong injector when handover parameter from Sightly to SlingModel #2157

Closed beo4 closed 4 years ago

beo4 commented 4 years ago

Required Information

Expected Behavior

When handing in parameters from htl to SlingModel a String Object is handed to JavaClass

e.g.

@Inject
@org.apache.sling.models.annotations.Optional
Object linkClasses;

corresponding html

data-sly-use.hello="${'de.eggs.core.models.HelloWorldModel' @ linkClasses=['custom-link']}"

Actual Behavior

After installing ACS-Commons > 4.0.0 the handed Object is not a String or String Array anymore, instead a I18N Object is injected

Some more wrong Injection Mapping occure after installing ACS-Commons > 4.0.0 I will provide more later

Steps to Reproduce

Install Project from https://github.com/eggsunimediaGmbH/acs-commons Insert HelloWorld Component

Install ACS-Commons 4.4 Debugging class de.eggs.core.models.HelloWorldModel See that htlParameter property is now I18N class

Links

https://github.com/eggsunimediaGmbH/acs-commons

badvision commented 4 years ago

Is there some way we can convert your debugging class into a proper unit test? That would help a lot!

beo4 commented 4 years ago

Is there some way we can convert your debugging class into a proper unit test? That would help a lot!

I try

beo4 commented 4 years ago

Added a jUnit Test which reproduce the issue in the attached git repo https://github.com/eggsunimediaGmbH/acs-commons/blob/master/core/src/test/java/de/eggs/core/models/HelloWorldModelTest.java

joerghoh commented 4 years ago

Thanks for the testcase.

It seems that the problematic part is the I18NInjector. If you don't register this service in your unittest, everything is fine. This injector has been added in ACS AEM Commons, so it matches with your observation.

For me it seems that this I18NInjector has a higher priority than the other injectors and can also handle any injections of objects, which are assignable from I18N (and of course Object can be assigned from any class).

I would think in 2 different ways now:

Additionally, you can can use the @Source annotation [1] to define which Injector should be used.

[1] https://sling.apache.org/documentation/bundles/models.html#source

beo4 commented 4 years ago

@joerghoh reg 1.) With Object class i can handle String and String[] hand overs, thats why it is there. reg 2.) https://github.com/Adobe-Consulting-Services/acs-aem-commons/issues/2158

joerghoh commented 4 years ago

@beo4 Maybe you can use the @Source annotation in the meantime to fix the issue you face.