@Override
  public RestLiResponseData<CreateResponseEnvelope> buildRestLiResponseData(Request request,
                                                    RoutingResult routingResult,
                                                    Object result,
                                                    Map<String, String> headers,
                                                    List<HttpCookie> cookies)
  {
    CreateResponse createResponse = (CreateResponse) result;
    boolean isGetAfterCreate = createResponse instanceof CreateKVResponse;

    if (createResponse.hasError())
    {
      RestLiServiceException exception = createResponse.getError();
      return new RestLiResponseDataImpl<>(new CreateResponseEnvelope(exception, isGetAfterCreate), headers, cookies);
    }

    Object id = null;
    if (createResponse.hasId())
    {
      id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
      final ProtocolVersion protocolVersion = routingResult.getContext().getRestliProtocolVersion();
      String stringKey = URIParamUtils.encodeKeyForUri(id, UriComponent.Type.PATH_SEGMENT, protocolVersion);
      UriBuilder uribuilder = UriBuilder.fromUri(request.getURI());
      uribuilder.path(stringKey);
      uribuilder.replaceQuery(null);
      if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM))
      {
        // add altkey param to location URI
        uribuilder.queryParam(RestConstants.ALT_KEY_PARAM, routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM));
      }
      headers.put(RestConstants.HEADER_LOCATION, uribuilder.build((Object) null).toString());
      headers.put(HeaderUtil.getIdHeaderName(protocolVersion), URIParamUtils.encodeKeyForHeader(id, protocolVersion));
    }

    // Verify that a null status was not passed into the CreateResponse. If so, this is a developer error.
    if (createResponse.getStatus() == null)
    {
      throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR,
          "Unexpected null encountered. HttpStatus is null inside of a CreateResponse from the resource method: "
              + routingResult.getResourceMethod());
    }

    final ResourceContext resourceContext = routingResult.getContext();

    RecordTemplate idResponse;
    if (createResponse instanceof CreateKVResponse && resourceContext.isReturnEntityRequested())
    {
      RecordTemplate entity = ((CreateKVResponse<?, ?>) createResponse).getEntity();

      // Verify that a null entity was not passed into the CreateKVResponse. If so, this is a developer error.
      if (entity == null)
      {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR,
            "Unexpected null encountered. Entity is null inside of a CreateKVResponse when the entity should be returned. In resource method: " + routingResult.getResourceMethod());
      }

      DataMap entityData = entity.data();

      TimingContextUtil.beginTiming(resourceContext.getRawRequestContext(),
          FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());

      final DataMap data = RestUtils.projectFields(entityData, resourceContext);

      TimingContextUtil.endTiming(resourceContext.getRawRequestContext(),
          FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());

      idResponse = new AnyRecord(data);
      // Ideally, we should set an IdEntityResponse to the envelope. But we are keeping AnyRecord
      // to make sure the runtime object is backwards compatible.
      // idResponse = new IdEntityResponse<>(id, new AnyRecord(data));
    }
    else //Instance of idResponse
    {
      idResponse = new IdResponse<>(id);
    }

    return new RestLiResponseDataImpl<>(new CreateResponseEnvelope(createResponse.getStatus(), idResponse, isGetAfterCreate), headers, cookies);
  }
@Test(dataProvider = "returnEntityData")
  public void testReturnEntityInBuildRestLiResponseData(CreateResponse createResponse, boolean isReturnEntityRequested, boolean expectEntityReturned) throws URISyntaxException
  {
    ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockContext.isReturnEntityRequested()).andReturn(isReturnEntityRequested);
    EasyMock.expect(mockContext.getProjectionMask()).andReturn(null);
    EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
    EasyMock.expect(mockContext.getRawRequestContext()).andReturn(new RequestContext()).anyTimes();
    EasyMock.expect(mockContext.getAlwaysProjectedFields()).andReturn(Collections.emptySet()).anyTimes();
    EasyMock.replay(mockContext);
    RoutingResult routingResult = new RoutingResult(mockContext, null);

    CreateResponseBuilder responseBuilder = new CreateResponseBuilder();
    RestLiResponseData<CreateResponseEnvelope> envelope = responseBuilder.buildRestLiResponseData(new RestRequestBuilder(new URI("/foo")).build(),
        routingResult,
        createResponse,
        Collections.emptyMap(),
        Collections.emptyList());

    RecordTemplate record = envelope.getResponseEnvelope().getRecord();

    if (expectEntityReturned)
    {
      Assert.assertTrue(record instanceof AnyRecord, "Entity in response envelope should be of type AnyRecord.");
      Assert.assertEquals(record, ((CreateKVResponse) createResponse).getEntity(), "Entity in response envelope should match the original.");
      Assert.assertTrue(envelope.getResponseEnvelope().isGetAfterCreate(), "Response envelope should be get after create.");
    }
    else
    {
      Assert.assertTrue(record instanceof IdResponse, "Entity in response envelope should be of type IdResponse.");
      Assert.assertNull(((IdResponse) record).getId(), "IdResponse in response envelope should have same ID.");
    }
  }