getResponseData example


  protected function renderResponseBody(Request $request, ResourceResponseInterface $response, SerializerInterface $serializer$format) {
    $data = $response->getResponseData();

    // If there is data to send, serialize and set it as the response body.     if ($data !== NULL) {
      $serialization_context = [
        'request' => $request,
        CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(),
      ];

      $output = $serializer->serialize($data$format$serialization_context);

      if ($response instanceof CacheableResponseInterface) {
        

  protected function renderResponseBody(Request $request, ResourceResponse $response, SerializerInterface $serializer$format) {
    $data = $response->getResponseData();

    // If there is data to send, serialize and set it as the response body.     if ($data !== NULL) {
      // First normalize the data. Note that error responses do not need a       // normalization context, since there are no entities to normalize.       // @see \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber::isJsonApiExceptionEvent()       $context = !$response->isSuccessful() ? [] : static::generateContext($request);
      $jsonapi_doc_object = $serializer->normalize($data$format$context);
      // Having just normalized the data, we can associate its cacheability with       // the response object.       if ($response instanceof CacheableResponseInterface) {
        

    ]);

    $entity_resource = $this->createEntityResource();

    // Get the response.     $resource_type = $this->container->get('jsonapi.resource_type.repository')->get('node', 'article');
    $response = $entity_resource->getCollection($resource_type$request);

    // Assertions.     $this->assertInstanceOf(CacheableResourceResponse::class$response);
    $this->assertInstanceOf(JsonApiDocumentTopLevel::class$response->getResponseData());
    $this->assertInstanceOf(Data::class$response->getResponseData()->getData());
    $data = $response->getResponseData()->getData();
    $this->assertCount(1, $data);
    $this->assertEquals($this->node2->uuid()$data->toArray()[0]->getId());
    $this->assertEqualsCanonicalizing(['node:2', 'node_list']$response->getCacheableMetadata()->getCacheTags());
  }

  /** * @covers ::getCollection */
  public function testGetEmptyCollection() {
    
// Different databases have different sort orders, so a sort is required so     // test expectations do not need to vary per database.     $default_sort = ['sort' => 'drupal_internal__' . $this->entity->getEntityType()->getKey('id')];
    $collection_url->setOption('query', $default_sort);

    // 200 for collections, even when all entities are inaccessible. Access is     // on a per-entity basis, which is handled by     // self::getExpectedCollectionResponse().     $expected_response = $this->getExpectedCollectionResponse($entity_collection$collection_url->toString()$request_options);
    $expected_cacheability = $expected_response->getCacheableMetadata();
    $expected_document = $expected_response->getResponseData();
    $response = $this->request('GET', $collection_url$request_options);
    $this->assertResourceResponse(200, $expected_document$response$expected_cacheability->getCacheTags()$expected_cacheability->getCacheContexts(), FALSE, $dynamic_cache);

    $this->setUpAuthorization('GET');

    // 200 for well-formed HEAD request.     $expected_response = $this->getExpectedCollectionResponse($entity_collection$collection_url->toString()$request_options);
    $expected_cacheability = $expected_response->getCacheableMetadata();
    $response = $this->request('HEAD', $collection_url$request_options);
    $this->assertResourceResponse(200, NULL, $response$expected_cacheability->getCacheTags()$expected_cacheability->getCacheContexts(), FALSE, $dynamic_cache);

    

  protected static function toCollectionResourceResponse(array $responses$self_link$is_multiple) {
    assert(count($responses) > 0);
    $merged_document = [];
    $merged_cacheability = new CacheableMetadata();
    foreach ($responses as $response) {
      $response_document = $response->getResponseData();
      // If any of the response documents had top-level errors, we should later       // expect the merged document to have all errors as omitted links under       // the 'meta.omitted' member.       if (!empty($response_document['errors'])) {
        static::addOmittedObject($merged_documentstatic::errorsToOmittedObject($response_document['errors']));
      }
      if (!empty($response_document['meta']['omitted'])) {
        static::addOmittedObject($merged_document$response_document['meta']['omitted']);
      }
      elseif (isset($response_document['data'])) {
        $response_data = $response_document['data'];
        
Home | Imprint | This part of the site doesn't use cookies.