[ 
https://issues.apache.org/jira/browse/SLING-10738?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17402274#comment-17402274
 ] 

Chris Shaw commented on SLING-10738:
------------------------------------

OK...

I think I have stumbled upon a solution that works but need it validated by 
someone who understands wrapped model requests better than I do. Below is what 
I got both standard and SPA content working with.
{code:java}
try {
    Resource resource = request.getResource();
    Page currentPage = resource.adaptTo(Page.class);

    // If the 'LayoutContainer' instance is either 'null' or it has no items 
available then we can
    // attempt to get a new instance using a wrapped request and adapting it to 
'LayoutContainer'.
    if (layoutContainer == null || layoutContainer.getItems().size() == 0) {
        layoutContainer = modelFactory.getModelFromWrappedRequest(
                createHierarchyServletRequest(request, currentPage, 
currentPage),
                resource,
                LayoutContainer.class);
    }
} catch (Exception e) {
    logger.info("Unable to bind the current page request to 'LayoutContainer'", 
e);
}
{code}
I have tested this for around 30 minutes using both authoring and client load 
and it doesn't appear to interrupt performance but I'm curious as to whether 
using a wrapped request from within a wrapped request will end up causing 
issues elsewhere in the chain?

The solution holistically feels like a massive hack just to add custom 
properties into the JSON output.

> Sling Model inheritance breaks with component exporter
> ------------------------------------------------------
>
>                 Key: SLING-10738
>                 URL: https://issues.apache.org/jira/browse/SLING-10738
>             Project: Sling
>          Issue Type: Bug
>          Components: Sling Models
>            Reporter: Chris Shaw
>            Priority: Major
>
> Hi,
> I'm currently supporting a project where I need to extend the LayoutContainer 
> class in AEM Core Components. Completing this process was easy and 
> straightforward when solely dealing solely with Sling Model Delegation. 
> Shortly after confirming this behaviour was working correctly, I added 
> Adobe's *ContainerExporter.class* to the *@Model* adapters and noticed thatĀ 
> Sling Model Delegation stopped working.
> I am raising this issue here first because the same method of extending 
> models in AEM Core Components works without issue except for the 
> LayoutContainer model. Below is the basic code before adding the exporters.
> {code:java}
> @Model(
>         adaptables = SlingHttpServletRequest.class,
>         adapters = {LayoutContainer.class},
>         resourceType = CustomContainerImpl.RESOURCE_TYPE,
>         defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
> )
> public class CustomContainerImpl implements LayoutContainer {
>     protected static final String RESOURCE_TYPE = 
> "project/components/custom-container";
>     @Self
>     @Via(type = ResourceSuperType.class)
>     private LayoutContainer layoutContainer;
>     
>     // ... implementation
> }
> {code}
> With this code, no issues at a page level are observed and all inherited 
> behaviours work as expected including appending *.model.json* to the page URL.
> Once I move beyond this and apply the exporter adapters, *.model.json* no 
> longer works correctly andĀ Sling Model Delegation breaks completely.
> {code:java}
> @Model(
>         adaptables = SlingHttpServletRequest.class,
>         adapters = {LayoutContainer.class, ComponentExporter.class, 
> ContainerExporter.class},
>         resourceType = CustomContainerImpl.RESOURCE_TYPE,
>         defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
> )
> public class CustomContainerImpl implements LayoutContainer {
>     protected static final String RESOURCE_TYPE = 
> "project/components/custom-container";
>     @Self
>     @Via(type = ResourceSuperType.class)
>     private LayoutContainer layoutContainer;
>     
>     // ... implementation
> }
> {code}
> Note that the adapters are: *ComponentExporter.class* and 
> *ContainerExporter.class*.
> What can be observed are 2 key issues:
>  # The *layoutContainer* property always returns *null*
>  # The previous JSON structure containing *root* elements is always empty for 
> child pages
> As a workaround to at least ensure the inheritance works, I have reverted to 
> using a *ModelFactory* instance which works from an authoring perspective but 
> doesn't solve the JSON output issue.
> {code:java}
> layoutContainer = modelFactory.getModelFromWrappedRequest(
>     request,
>     request.getResource(),
>     LayoutContainer.class);{code}
> It would be great to get insight into this as I was to be 100% sure Sling is 
> not to blame before raising an issue with Adobe.
> Thanks for your time!



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to