@Override
  public D buildRestLiResponseData(Request request,
      RoutingResult routingResult,
      Object object,
      Map<String, String> headers,
      List<HttpCookie> cookies)
  {
    if (object instanceof List)
    {
      @SuppressWarnings({"unchecked"})
      /** constrained by {@link com.linkedin.restli.internal.server.model.RestLiAnnotationReader#validateFinderMethod(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor, com.linkedin.restli.internal.server.model.ResourceModel)} */
      List<? extends RecordTemplate> result = (List<? extends RecordTemplate>) object;

      return buildRestLiResponseData(request, routingResult, result, PageIncrement.RELATIVE, null, null, headers, cookies);
    }
    else
    {
      @SuppressWarnings({"unchecked"})
      /** constrained by {@link com.linkedin.restli.internal.server.model.RestLiAnnotationReader#validateFinderMethod(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor, com.linkedin.restli.internal.server.model.ResourceModel)} */
      CollectionResult<? extends RecordTemplate, ? extends RecordTemplate> collectionResult =
          (CollectionResult<? extends RecordTemplate, ? extends RecordTemplate>) object;

      //Verify that a null wasn't passed into the collection result. If so, this is a developer error.
      if (collectionResult.getElements() == null)
      {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR,
            "Unexpected null encountered. Null elements List inside of CollectionResult returned by the resource method: "
                + routingResult.getResourceMethod());
      }

      return buildRestLiResponseData(request, routingResult, collectionResult.getElements(),
                                     collectionResult.getPageIncrement(), collectionResult.getMetadata(),
                                     collectionResult.getTotal(), headers, cookies);
    }
  }
@Test
  @SuppressWarnings("unchecked")
  public <D extends RestLiResponseData<? extends CollectionResponseEnvelope>> void testProjectionInBuildRestliResponseData() throws URISyntaxException {

    for (Map.Entry<ResourceMethod, CollectionResponseBuilder<?>> entry: BUILDERS.entrySet())
    {
      ResourceMethod resourceMethod = entry.getKey();
      MaskTree maskTree = new MaskTree();
      maskTree.addOperation(new PathSpec("fruitsField"), MaskOperation.POSITIVE_MASK_OP);

      ServerResourceContext mockContext =
          getMockResourceContext(maskTree, null, null, ProjectionMode.AUTOMATIC, ProjectionMode.AUTOMATIC);
      RoutingResult routingResult = new RoutingResult(mockContext, getMockResourceMethodDescriptor());

      List<RecordTemplate> values = new ArrayList<>();
      Foo value = new Foo().setStringField("value").setFruitsField(Fruits.APPLE);
      values.add(value);

      CollectionResponseBuilder<D> responseBuilder = (CollectionResponseBuilder<D>) entry.getValue();
      D responseData = responseBuilder.buildRestLiResponseData(getRestRequest(), routingResult, values,
                                                                                Collections.emptyMap(),
                                                                                Collections.emptyList());
      RecordTemplate record = responseData.getResponseEnvelope().getCollectionResponse().get(0);
      Assert.assertEquals(record.data().size(), 1);
      Assert.assertEquals(record.data().get("fruitsField"), Fruits.APPLE.toString());
    }
  }