Calling normalize without account context generates a warning.

Created on 24 May 2024, 7 months ago
Updated 11 June 2024, 6 months ago

Problem/Motivation

When working on a test for parent issue i ran into warnings when normalizing. It seems that the normalizer assumes $context['account'] is always set to check access. That feels wrong.

    // Use EntityTestComputedField since ComputedTestCacheableStringItemList has a max age of 800
    $baseMaxAge = 800;
    $entity = EntityTestComputedField::create([]);
    $entity->save();
    $resource_type = $this->resourceTypeRepository->get($entity->getEntityTypeId(), $entity->bundle());
    $resource_object = ResourceObject::createFromEntity($resource_type, $entity);

    $resource_normalization = $this->serializer
      ->normalize($resource_object, 'api_json', ['account' => NULL])
      ->withCacheableDependency((new CacheableMetadata()));

The code in ResourceObjectNormalizer::168

$field_access_result = $field->access('view', $context['account'], TRUE);

Steps to reproduce

Proposed resolution

Since NULL is a valid option for for the account we can just return null if account is not set in the context.

$field_access_result = $field->access('view', $context['account'] ?? NULL, TRUE);

This is what now happens, but without the warning.

Remaining tasks

Update the code

User interface changes

n.a.

API changes

n.a.

Data model changes

n.a.

Release notes snippet

n.a.

📌 Task
Status

Fixed

Version

10.3

Component
JSON API 

Last updated 6 days ago

Created by

🇳🇱Netherlands bbrala Netherlands

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024