@SuppressWarnings("unchecked")
  public CompletableFuture<Void> onResponse(final FilterRequestContext requestContext,
                                            final FilterResponseContext responseContext)
  {
    RestLiResponseData<?> responseData = responseContext.getResponseData();

    if (responseData.getResponseEnvelope().isErrorResponse())
    {
      return CompletableFuture.completedFuture(null);
    }

    if (shouldValidateOnResponse(requestContext))
    {

      ResourceMethod method = requestContext.getMethodType();
      RestLiDataValidator validator = createResponseRestLiDataValidator(requestContext);

      switch (method)
      {
        case GET:
          validateSingleResponse(validator, ((GetResponseEnvelope) responseData.getResponseEnvelope()).getRecord());
          break;
        case CREATE:
          if (requestContext.isReturnEntityMethod() && requestContext.isReturnEntityRequested())
          {
            validateSingleResponse(validator, ((CreateResponseEnvelope) responseData.getResponseEnvelope()).getRecord());
          }
          break;
        case PARTIAL_UPDATE:
          if (requestContext.isReturnEntityMethod() && requestContext.isReturnEntityRequested())
          {
            validateSingleResponse(validator, ((PartialUpdateResponseEnvelope) responseData.getResponseEnvelope()).getRecord());
          }
          break;
        case GET_ALL:
          validateCollectionResponse(validator, ((GetAllResponseEnvelope) responseData.getResponseEnvelope()).getCollectionResponse());
          break;
        case FINDER:
          validateCollectionResponse(validator, ((FinderResponseEnvelope) responseData.getResponseEnvelope()).getCollectionResponse());
          break;
        case BATCH_FINDER:
          validateBatchCollectionResponse(validator, ((BatchFinderResponseEnvelope) responseData.getResponseEnvelope()).getItems());
          break;
        case BATCH_GET:
          validateBatchResponse(validator, ((BatchGetResponseEnvelope) responseData.getResponseEnvelope()).getBatchResponseMap());
          break;
        case BATCH_CREATE:
          if (requestContext.isReturnEntityMethod() && requestContext.isReturnEntityRequested())
          {
            validateCreateCollectionResponse(validator, ((BatchCreateResponseEnvelope) responseData.getResponseEnvelope()).getCreateResponses());
          }
          break;
        case BATCH_PARTIAL_UPDATE:
          if (requestContext.isReturnEntityMethod() && requestContext.isReturnEntityRequested())
          {
            validateBatchResponse(validator, ((BatchPartialUpdateResponseEnvelope) responseData.getResponseEnvelope()).getBatchResponseMap());
          }
          break;
      }
    }

    return CompletableFuture.completedFuture(null);
  }
@Test(dataProvider = "invalidResponses")
  @SuppressWarnings({"unchecked"})
  public void testSkipResponseValidation(ResourceModel resourceModel, ResourceMethod resourceMethod,
      RestLiRequestData restLiRequestData, RestLiResponseData<RestLiResponseEnvelope> responseData)
  {
    when(filterRequestContext.getRequestHeaders()).thenReturn(
        Collections.singletonMap(RestConstants.HEADER_SKIP_RESPONSE_VALIDATION, "true"));
    when(filterRequestContext.getRequestData()).thenReturn(restLiRequestData);
    when(filterRequestContext.getMethodType()).thenReturn(resourceMethod);
    when(filterRequestContext.getFilterResourceModel()).thenReturn(new FilterResourceModelImpl(resourceModel));
    when(filterRequestContext.getRestliProtocolVersion()).thenReturn(AllProtocolVersions.LATEST_PROTOCOL_VERSION);
    when(filterResponseContext.getResponseData()).thenReturn((RestLiResponseData) responseData);

    RestLiValidationFilter validationFilter =
        new RestLiValidationFilter(Collections.emptyList(), new MockValidationErrorHandler());

    try
    {
      validationFilter.onResponse(filterRequestContext, filterResponseContext);
    }
    catch (RestLiServiceException ex)
    {
      Assert.fail("An unexpected exception was thrown on response in the validation filter.", ex);
    }
  }