Problem/Motivation

For views with an exported array, the JSON output includes the following error:

Return type of Drupal\rest_views\RenderableData::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in <b>/var/www/html/web/modules/contrib/rest_views/src/RenderableData.php</b> on line <b>73</b><br />

In PHP 8.1 a tentative return type was added to the method: JsonSerializable::jsonSerialize()

Steps to reproduce

Create a view.
Add a field that will contain multiple rendered entities as an array
View the resulting output

Suggested Solution

Add #[\ReturnTypeWillChange] above line 73, or perhaps more ideally, change line 73 to read
public function jsonSerialize(): array {

Issue fork rest_views-3393017

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

the_double_a created an issue. See original summary.

the_double_a’s picture

Here is a suggested patch.

marios anagnostopoulos’s picture

StatusFileSize
new369 bytes

Adding a patch with the array return type as suggested by the OP

marios anagnostopoulos’s picture

Status: Active » Needs review

Queued for test and changing to needs review.

benjifisher made their first commit to this issue’s fork.

benjifisher’s picture

I have not tested, but I have reviewed this issue. Either of the patches in #2 or #3 should resolve the problem for PHP 8.1+, and either patch should be compatible with lower versions of PHP as well.

Since the return type is known, I prefer to add a return type rather than an annotation to skip the check.

When implementing an interface in PHP, it is always allowed to specify a more restrictive return type than what the interface says. So a return type of array should work with PHP 8.1+ (where the return type is mixed) and with earlier versions (no return type).

I have added a MR based on the patch in #3, and I am hiding the two patches.

nicxvan made their first commit to this issue’s fork.

nicxvan’s picture

Status: Needs review » Fixed

This looks good, thanks for the review and MR conversion!

nicxvan’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

nicxvan’s picture