You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "Henry Kuijpers (Jira)" <ji...@apache.org> on 2022/08/19 14:54:00 UTC

[jira] [Comment Edited] (SLING-9036) Sling Models: SlingHttpServletRequestWrapper.adaptTo() unwraps before adapting

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

Henry Kuijpers edited comment on SLING-9036 at 8/19/22 2:53 PM:
----------------------------------------------------------------

[~kwin] Failing test case here: https://github.com/henrykuijpers/sling-org-apache-sling-api/blob/feature/SLING-9036-fix-unwrapping-in-adaptto/src/test/java/org/apache/sling/api/wrappers/SlingHttpServletRequestWrapperTest.java

{code}
org.junit.ComparisonFailure: The model should have considered the overwritten path 
Expected :/test/1234
Actual   :/test
{code}


was (Author: henry kuijpers):
[~kwin] Failing test case here: https://github.com/henrykuijpers/sling-org-apache-sling-api/commit/f8bdb9c82fb4eafbdefb23aa8c43401648735a12

{code}
org.junit.ComparisonFailure: The model should have considered the overwritten path 
Expected :/test/1234
Actual   :Failure
{code}

> Sling Models: SlingHttpServletRequestWrapper.adaptTo() unwraps before adapting
> ------------------------------------------------------------------------------
>
>                 Key: SLING-9036
>                 URL: https://issues.apache.org/jira/browse/SLING-9036
>             Project: Sling
>          Issue Type: New Feature
>          Components: Sling Models
>            Reporter: Henry Kuijpers
>            Priority: Major
>             Fix For: Models Implementation 1.5.4
>
>
> Sling Model:
> {code:java}
> @Model(adaptables = SlingHttpServletRequest.class, adapters = MySlingModel.class)
> class MySlingModel { 
>   @Inject
>   public MySlingModel(@Self SlingHttpServletRequest req) {
>     logger.log(req.getResourceBundle().getClass().getName());   
>   }
> }
> {code}
> Calling code:
> {code:java}
> // req obtained via JSP/HTL
> final SlingHttpServletRequest myWrappedReq = new SlingHttpServletRequestWrapper(req) {
>   @Override
>   public ResourceBundle getResourceBundle() {
>     return new MyCustomResourceBundle();
>   }
> };
>  
> final MySlingModel myModel = myWrappedReq.adaptTo(MySlingModel.class);
> {code}
> I would expect the log file to contain "MyCustomResourceBundle". Instead it contains "NullResourceBundle". 
> This is because the request is being "unwrapped" when doing the adaptTo() call: It keeps on delegating to the wrapped request. This should not have happened, i.e. how can we otherwise overwrite (parts of) requests and resources? 
> See also: [https://github.com/apache/sling-org-apache-sling-api/blob/master/src/main/java/org/apache/sling/api/wrappers/SlingHttpServletRequestWrapper.java#L147]



--
This message was sent by Atlassian Jira
(v8.20.10#820010)