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

@ChildResource Collection<MyModel> in combination with @PostConstruct Boolean method causes the injection of an empty collection, instead of a collection with all successful items

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • Sling Models Impl 1.4.0
    • None
    • Sling Models
    • None

    Description

      Let's say we have a Sling Model with a parameter:

      @ChildResource(name = "links", injectionStrategy = OPTIONAL) final Collection<Link> links)
      

      And Link is implemented as follows:

      @Model(adaptables = Resource.class, adapters = LinkModel.class)
      public class Link {
          private final String url;
      
          @Inject
          public LinkModel(@ValueMapValue(name = "url", injectionStrategy = InjectionStrategy.OPTIONAL) final String url) {
              this.url = url;
          }
      
          @PostConstruct
          public boolean activate() {
              // Returning false here indicates that the model is not valid (null will be returned, instead of the instantiated model)
              return StringUtils.isNotEmpty(url);
          }
      }
      

      In case the content structure looks like this:

      links/item0/jcr:primaryType=nt:unstructured
      links/item0/url=test0
      links/item1/jcr:primaryType=nt:unstructured
      links/item2/jcr:primaryType=nt:unstructured
      links/item2/url=test2
      

      I would expect the following to be the value of the @ChildResource parameter, when adapting:
      links=[url=test0, url=test2]

      However, an empty collection is injected instead.

      I believe this is because of the following lines:
      https://github.com/apache/sling-org-apache-sling-models-impl/blob/a348e50f9321af27dea513d0367b813d3afce9e2/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java#L1044

      The logic here seems to be that if one of the various items cannot be adapted, an empty collection is returned, instead of a collection ommitting the item that could not be adapted.

      Attachments

        Activity

          People

            Unassigned Unassigned
            Henry Kuijpers Henry Kuijpers
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: