Sling Adapters cache overly eager


This might be a Sling issue, let me know if that's a better place to put this.

When performing an adaptTo on a AemContext request, a cache entry is created for the Adapted type, and nothing seems to invalidate this cache.

For example,


Stefan Seifert
October 28, 2019, 4:41 PM

you can give it a try with 1.2.1-SNAPSHOT - if it solves the problem i can do a release.

workaround may be to split the unit tests in multiple ones, and making sure each one contains only one adaptTo call from the request for the same type.

David Gonzalez
October 25, 2019, 8:17 PM

@Stefan Seifert - Reported this at

is there a reasonable way around this in the interim? Is the only way to have 2 AemContexts?

Henry Kuijpers
October 24, 2019, 1:32 PM

You’re not going crazy, there is indeed no way to create an issue there. You should instead log the issue in the Apache Sling Jira tracker.

David Gonzalez
October 24, 2019, 1:21 PM

Thanks, Stefan - am I going crazy, or is there no way to log Issues on ? I don't see the Issues option at all next to the “Code” tab (and I am logged in)

Stefan Seifert
October 23, 2019, 11:11 AM

you're right, this is puzzling for simple testing use cases like in your example.

please note that the Adaptable interface does not define whether the impl behind it should cache exceptions or not.

lot's of implementations e.g. the most Resource implementations extend SlingAdaptable which always applies a caching of the results.

adapting from SlingHttpServletRequest is a special case - in the Sling Engine it is one of the few places which does not extend SlingAdaptable and thus does not cache the results. however, the mock implementation of SlingHttpServletRequest used in the mocks extends from SlingAdaptable an thus does the caching as well.

we might remove this caching from the mocked SlingHttpServletRequest to be as close to sling as possible - but if you adapt from resources you will still have the same problem (same behavior as in a running instance).

maybe you want to create a SLING ticket and/or a PR on




David Gonzalez




Affects versions