Uploaded image for project: 'Sling'
  1. Sling
  2. SLING-10212

Prioritize model with no `resourceType` when no resource type matches

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • Sling Models
    • None

    Description

      I have found that the behaviour of the Model Adapter is not as I would expect when no model implementation matches the adaptable objects resources resource type. 

       

      My analysis is based on stepping through the adapter to debug an issue.
      I would appreciate if someone with a more intimate understanding of Sling Models could verify this behaviour. 

       

      The issue arises when there are multiple implementations, and one of those implementations does not declare a `resourceType`.
      For example:

       

      public interface SomeModel {
         // ...
      }

       

      @Model(adaptables = SlingHttpServletRequest.class, adapters = SomeModel.class, resourceType = "project/A")
      public class A_Impl implements SomeModel {
         // ...
      }

       

      @Model(adaptables = SlingHttpServletRequest.class, adapters = SomeModel.class, resourceType = "project/B") 
      public class B_Impl implements SomeModel {
        // ...
      }
      

       

      @Model(adaptables = SlingHttpServletRequest.class, adapters = SomeModel.class) 
      public class C_Impl implements SomeModel {
        // ...
      }
      

       

      Now, let us assume that we are adapting a request for which the resource type is "X". 
      Neither `project/A` or `project/B` have a parent resource type of "X".

      The adapter will first run through the `ResourceTypeBasedResourcePicker`, which will not produce any model implementation suitable for the resource type "X".
      The adapter will then fall back on the "FirstImplementationPicker", and now, if the implementations are in the order specified above, the implementation `A_Impl` will be chosen - despite the fact that the `resourceType` does not match.

      I would expect that `C_Impl` would be chosen instead.
      Although `C_Impl` does not match a `resourceType`, it at least does not have the wrong `resourceType`.

      A possible solution to implement this is to add another ImplementationPicker (e.g. "DefaultImplementationPicker") that is positioned between the `ResourceTypeBasedResourcePicker` and the `FirstImplementationPicker`. This new implementation picker would only return implementations that do not define any `resourceType` at all. 

      Thus the order of precedence for model implementation selection would be:

      1. The model implementation with the closest matching `resourceType`
      2. A model implementation with no `resourceType`
      3. The first model implementation

       

       

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            kylegio Kyle Giovannetti
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: