Re: Flexible adaptable not resulting in instantiation of correct model

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Flexible adaptable not resulting in instantiation of correct model

Liam C
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 <[hidden email]> 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
>
Reply | Threaded
Open this post in threaded view
|

Re: Flexible adaptable not resulting in instantiation of correct model

Vlad Bailescu
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 <[hidden email]> 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 <[hidden email]> 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
> >
>
Reply | Threaded
Open this post in threaded view
|

Re: Flexible adaptable not resulting in instantiation of correct model

Julian Sedding-3
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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
> > >
> >
Reply | Threaded
Open this post in threaded view
|

Re: Flexible adaptable not resulting in instantiation of correct model

Paul Bjorkstrand
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>
> 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 <[hidden email]> 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
> > > >
> > >
>
Reply | Threaded
Open this post in threaded view
|

Re: Flexible adaptable not resulting in instantiation of correct model

Liam C
Thanks Vlad, Julian, Paul, for your input.

@Julian - yes, that was my interpretation of the situation, confirmed
by the fact that adaptation of such adaptables works when the
SlingModelsUseProvider has been shut down.

Paul's implementation looks good to me - I hope it'll be included in
Sling shortly.

Liam

On Wed, 12 Jun 2019 at 21:15, Paul Bjorkstrand
<[hidden email]> wrote:

>
> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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
>> > > >
>> > >