Reading this thread (and looking at the code) I agree with Julian - it is surprising that it does not work with the SlingModelsUseProvider, since it used to work with the JavaUseProvider. This piqued my interest, so I created a minimal implementation of this in [1].
[1] https://github.com/apache/sling-org-apache-sling-scripting-sightly-models-provider/pull/1 Paul Bjorkstrand On Wed, Jun 12, 2019 at 3:09 AM Julian Sedding <[email protected]> wrote: > Hi Liam > > Based on the fact that the @Self injection should provide a Resource > object, I assume your model is adaptable from a Resource (it would > help to provide the class including its @Model annotation). > > The way this feature is implemented, I assume that it does not work > for adapting Resource objects. The reason is that the > SlingModelsUseProvider can handle the adaption before the > JavaUseProvider, but the implementation is only in the > JavaUseProvider. > > The tests for SLING-6504 implement an adapter factory by hand (i.e. > don't use @Model), which is (I assume) why the SlingModelsUseProvider > doesn't handle it and the JavaUseProvider get's a chance. > > Therefore, a workaround would be to implement an adapter factory by > hand. I agree that that kind of defies the point of Sling Models. > Another workaround might be to restrict your Sling Model to a specific > resource type. > > I suggets you open a JIRA ticket and maybe you can provide a PR to > enhance the SlingModelsUseProvider to support the "adaptable" argument > as well. > > @Vlad: I'm not sure that the feature is "not designed" to work with > Sling Models (after all Sling Models is just a convenient way to > register an adapter factory). I would be of the opinion that this is a > bug (or at least a limitation/oversight in the implementation). It > clearly is a violation of the principle of least surprise, which I > believe should be avoided. > > Regards > Julian > > On Wed, Jun 12, 2019 at 8:44 AM Vlad Bailescu <[email protected]> wrote: > > > > Hi Liam, > > > > AFAIK, the mentioned pattern is not designed to work with Sling Models. > It > > simply uses the adaptTo pattern to transform the adaptable to the given > > class. > > > > Best, > > Vlad > > > > > > On Tue, Jun 11, 2019 at 9:00 PM Liam C <[email protected]> > wrote: > > > > > Hello, > > > > > > Is anyone able to replicate this, or to confirm that they have been > > > able to get something similar to { 'com.example.ModelClass' @ > > > adaptable=item} working in their projects, please? > > > > > > Failing this, is there some workaround that could be used within > > > Sightly/HTL to adapt a specified resource to a Model class? Although > > > it is possible to access resource properties (e.g. item.myProperty), > > > some business logic is contained within models that requires access > > > via getters rather than via the resource properties. > > > > > > Thanks for any guidance. > > > > > > Liam > > > > > > On 2019/04/01 12:13:43, Liam C <[email protected]> wrote: > > > > Hello everyone, > > > > > > > > In SLING-6504 (https://issues.apache.org/jira/browse/SLING-6504), > > > > functionality was added to allow adapting a resource that is > different > > > > to the underlying page resource. Feike Visser's example > > > > ( > > > > https://github.com/heervisscher/htl-examples/commit/93666b5eebdeb36c25adf2e310f55049ec3767ce > > > ) > > > > suggests that it is possible and simple, but I haven't managed to > > > > succesfully implement it. In my experience so far, the adaptable is > > > > ignored. > > > > > > > > If I use the following code in my Sling Model: > > > > > > > > @Self > > > > Resource adaptable; > > > > public Resource getAdaptableSelf() > > > > { > > > > return adaptable; > > > > } > > > > > > > > ...it reveals that the adaptable that reaches the model is null. > > > > > > > > I wondered if this might have to do with the fact that the code in > > > > SLING-6504 modified the JavaUseProvider, but the > > > > SlingModelsUseProvider handles the instantiation before the > > > > JavaUseProvider can be reached. > > > > > > > > Can anyone confirm whether this option is still working in Sling 11? > > > > > > > > Thanks, > > > > > > > > Liam > > > > > > > >
