Configured headers not used anymore

Created on 29 August 2023, over 1 year ago

Problem/Motivation

After updating to 4.1.x I noticed that the custom headers for domains, cores, hosts are not applying for me anymore. I debugged the issue and it boils down to the getHeader() functions in those entity classes.

public function getHeader(): array {
    $headers = [];
    foreach ($this->get('header') as $header) {
      if (property_exists($header, 'key') && property_exists($header, 'value')) {
        $headers[$header->key] = $header->value;
      }
    }
    return $headers;
  }

The reason is, that the property_exists() returns false in every case, because $header in the loop is type of \Drupal\key_value_field\Plugin\Field\FieldType\KeyValueItem, which do not have object properties named like this, but just the magic getters.

The change seems to have been introduced in 📌 Code cleanup for PHP 8.1, PHPStan and Coding Standards Fixed

Steps to reproduce

  1. Set some header values for core, domain or hosts
  2. Debug the request and you will see that the headers do not end up in the HTTP request
  3. Check the corresponding getHeader function, you will see that the headers are not returned, even tough they are existing

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs work

Version

4.1

Component

Code

Created by

🇩🇪Germany szeidler Berlin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @szeidler
  • @szeidler opened merge request.
  • Status changed to Needs review over 1 year ago
  • 🇩🇪Germany szeidler Berlin

    I created a MR, just straightforward resetting it to what it was before the change.

  • Status changed to Needs work over 1 year ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    That will cause error messages when testing this with PhpStan. There needs to be a way to safely get to those values, especially in PHP 8.1

  • 🇩🇪Germany szeidler Berlin

    Can you verify the bug?

    Might it be possible to do a isset($header->key) check? This should trigger the magic __isset() method in Drupal\Core\Field\FieldItemBase

  • 🇩🇪Germany jurgenhaas Gottmadingen

    How about this fully sanitized method:

      public function getHeader(): array {
        $headers = [];
        /** @var \Drupal\key_value_field\Plugin\Field\FieldType\KeyValueItem $header */
        foreach ($this->get('header') as $header) {
          try {
            $key = $header->get('key')->getValue();
            $value = $header->get('value')->getValue();
            $headers[$key] = $value;
          }
          catch (MissingDataException $e) {
            // We can ignore this.
          }
        }
        return $headers;
      }
    
    
  • 🇩🇪Germany szeidler Berlin

    For a missing property it throws a throw new \InvalidArgumentException("Property $property_name is unknown."); in web/core/lib/Drupal/Core/TypedData/TypedDataManager.php on my end.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    I can't reproduce that, it works just fine here.

Production build 0.71.5 2024